001    /*
002     * Copyright (c) 2007-2015 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    /** Class FlowStats collects {@link cascading.flow.Flow} specific statistics. */
035    public class FlowStats extends CascadingStats
036      {
037      final Flow flow;
038      final List<FlowStepStats> flowStepStatsList = new ArrayList<FlowStepStats>();
039    
040      public FlowStats( Flow flow, ClientState clientState )
041        {
042        super( flow.getName(), clientState );
043        this.flow = flow;
044        }
045    
046      public Map<Object, Object> getFlowProperties()
047        {
048        return flow.getConfigAsProperties();
049        }
050    
051      public String getAppID()
052        {
053        return AppProps.getApplicationID( getFlowProperties() );
054        }
055    
056      public String getAppName()
057        {
058        return AppProps.getApplicationName( getFlowProperties() );
059        }
060    
061      @Override
062      public String getID()
063        {
064        return flow.getID();
065        }
066    
067      @Override
068      public synchronized void recordInfo()
069        {
070        clientState.recordFlow( flow );
071        }
072    
073      public void addStepStats( FlowStepStats flowStepStats )
074        {
075        flowStepStatsList.add( flowStepStats );
076        }
077    
078      /**
079       * Method getStepStats returns the stepStats owned by this FlowStats.
080       *
081       * @return the stepStats (type List<StepStats>) of this FlowStats object.
082       */
083      public List<FlowStepStats> getFlowStepStats()
084        {
085        return flowStepStatsList;
086        }
087    
088      /**
089       * Method getStepsCount returns the number of steps this Flow executed.
090       *
091       * @return the stepsCount (type int) of this FlowStats object.
092       */
093      public int getStepsCount()
094        {
095        return flowStepStatsList.size();
096        }
097    
098      @Override
099      public Collection<String> getCounterGroups()
100        {
101        Set<String> results = new HashSet<String>();
102    
103        for( FlowStepStats flowStepStats : getFlowStepStats() )
104          results.addAll( flowStepStats.getCounterGroups() );
105    
106        return results;
107        }
108    
109      @Override
110      public Collection<String> getCounterGroupsMatching( String regex )
111        {
112        Set<String> results = new HashSet<String>();
113    
114        for( FlowStepStats flowStepStats : getFlowStepStats() )
115          results.addAll( flowStepStats.getCounterGroupsMatching( regex ) );
116    
117        return results;
118        }
119    
120      @Override
121      public Collection<String> getCountersFor( String group )
122        {
123        Set<String> results = new HashSet<String>();
124    
125        for( FlowStepStats flowStepStats : getFlowStepStats() )
126          results.addAll( flowStepStats.getCountersFor( group ) );
127    
128        return results;
129        }
130    
131      @Override
132      public long getCounterValue( Enum counter )
133        {
134        long value = 0;
135    
136        for( FlowStepStats flowStepStats : getFlowStepStats() )
137          value += flowStepStats.getCounterValue( counter );
138    
139        return value;
140        }
141    
142      @Override
143      public long getCounterValue( String group, String counter )
144        {
145        long value = 0;
146    
147        for( FlowStepStats flowStepStats : getFlowStepStats() )
148          value += flowStepStats.getCounterValue( group, counter );
149    
150        return value;
151        }
152    
153      @Override
154      public void captureDetail()
155        {
156        for( FlowStepStats flowStepStats : getFlowStepStats() )
157          flowStepStats.captureDetail();
158        }
159    
160      @Override
161      public Collection getChildren()
162        {
163        return getFlowStepStats();
164        }
165    
166      @Override
167      protected String getStatsString()
168        {
169        return super.getStatsString() + ", stepsCount=" + getStepsCount();
170        }
171    
172      @Override
173      public String toString()
174        {
175        return "Flow{" + getStatsString() + '}';
176        }
177    
178      @Override
179      public int hashCode()
180        {
181        return getID().hashCode();
182        }
183    
184      @Override
185      public boolean equals( Object object )
186        {
187        if( this == object )
188          return true;
189        if( object == null || !( object instanceof FlowStats ) )
190          return false;
191    
192        return getID().equals( ( (FlowStats) object ).getID() );
193        }
194      }