001    /*
002     * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved.
003     *
004     * Project and contact information: http://www.cascading.org/
005     *
006     * This file is part of the Cascading project.
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing, software
015     * distributed under the License is distributed on an "AS IS" BASIS,
016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017     * See the License for the specific language governing permissions and
018     * limitations under the License.
019     */
020    
021    package cascading.stats;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.HashSet;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Set;
029    
030    import cascading.flow.Flow;
031    import cascading.management.state.ClientState;
032    import cascading.property.AppProps;
033    
034    
035    /** Class FlowStats collects {@link cascading.flow.Flow} specific statistics. */
036    public class FlowStats extends CascadingStats
037      {
038      final Flow flow;
039      final List<FlowStepStats> flowStepStatsList = new ArrayList<FlowStepStats>();
040    
041      public FlowStats( Flow flow, ClientState clientState )
042        {
043        super( flow.getName(), clientState );
044        this.flow = flow;
045        }
046    
047      public Map<Object, Object> getFlowProperties()
048        {
049        return flow.getConfigAsProperties();
050        }
051    
052      public String getAppID()
053        {
054        return AppProps.getApplicationID( getFlowProperties() );
055        }
056    
057      public String getAppName()
058        {
059        return AppProps.getApplicationName( getFlowProperties() );
060        }
061    
062      @Override
063      public String getID()
064        {
065        return flow.getID();
066        }
067    
068      @Override
069      public synchronized void recordInfo()
070        {
071        clientState.recordFlow( flow );
072        }
073    
074      public void addStepStats( FlowStepStats flowStepStats )
075        {
076        flowStepStatsList.add( flowStepStats );
077        }
078    
079      /**
080       * Method getStepStats returns the stepStats owned by this FlowStats.
081       *
082       * @return the stepStats (type List<StepStats>) of this FlowStats object.
083       */
084      public List<FlowStepStats> getFlowStepStats()
085        {
086        return flowStepStatsList;
087        }
088    
089      /**
090       * Method getStepsCount returns the number of steps this Flow executed.
091       *
092       * @return the stepsCount (type int) of this FlowStats object.
093       */
094      public int getStepsCount()
095        {
096        return flowStepStatsList.size();
097        }
098    
099      @Override
100      public Collection<String> getCounterGroups()
101        {
102        Set<String> results = new HashSet<String>();
103    
104        for( FlowStepStats flowStepStats : flowStepStatsList )
105          results.addAll( flowStepStats.getCounterGroups() );
106    
107        return results;
108        }
109    
110      @Override
111      public Collection<String> getCounterGroupsMatching( String regex )
112        {
113        Set<String> results = new HashSet<String>();
114    
115        for( FlowStepStats flowStepStats : flowStepStatsList )
116          results.addAll( flowStepStats.getCounterGroupsMatching( regex ) );
117    
118        return results;
119        }
120    
121      @Override
122      public Collection<String> getCountersFor( String group )
123        {
124        Set<String> results = new HashSet<String>();
125    
126        for( FlowStepStats flowStepStats : flowStepStatsList )
127          results.addAll( flowStepStats.getCountersFor( group ) );
128    
129        return results;
130        }
131    
132      @Override
133      public long getCounterValue( Enum counter )
134        {
135        long value = 0;
136    
137        for( FlowStepStats flowStepStats : flowStepStatsList )
138          value += flowStepStats.getCounterValue( counter );
139    
140        return value;
141        }
142    
143      @Override
144      public long getCounterValue( String group, String counter )
145        {
146        long value = 0;
147    
148        for( FlowStepStats flowStepStats : flowStepStatsList )
149          value += flowStepStats.getCounterValue( group, counter );
150    
151        return value;
152        }
153    
154      @Override
155      public void captureDetail()
156        {
157        for( FlowStepStats flowStepStats : flowStepStatsList )
158          flowStepStats.captureDetail();
159        }
160    
161      @Override
162      public Collection getChildren()
163        {
164        return getFlowStepStats();
165        }
166    
167      @Override
168      protected String getStatsString()
169        {
170        return super.getStatsString() + ", stepsCount=" + getStepsCount();
171        }
172    
173      @Override
174      public String toString()
175        {
176        return "Flow{" + getStatsString() + '}';
177        }
178      }