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
021package cascading.stats;
022
023import java.util.Collection;
024import java.util.HashSet;
025import java.util.LinkedHashMap;
026import java.util.Map;
027import java.util.Set;
028
029import cascading.cascade.Cascade;
030import cascading.management.state.ClientState;
031import cascading.util.ProcessLogger;
032
033/** Class CascadeStats collects {@link Cascade} specific statistics. */
034public class CascadeStats extends CascadingStats<FlowStats>
035  {
036  private Cascade cascade;
037  /** Field flowStatsList */
038  final Map<String, FlowStats> flowStatsMap = new LinkedHashMap<>(); // maintain order
039
040  public CascadeStats( Cascade cascade, ClientState clientState )
041    {
042    super( cascade.getName(), clientState );
043    this.cascade = cascade;
044    }
045
046  @Override
047  protected ProcessLogger getProcessLogger()
048    {
049    if( cascade != null && cascade instanceof ProcessLogger )
050      return (ProcessLogger) cascade;
051
052    return ProcessLogger.NULL;
053    }
054
055  @Override
056  public String getID()
057    {
058    return cascade.getID();
059    }
060
061  @Override
062  public Type getType()
063    {
064    return Type.CASCADE;
065    }
066
067  public Cascade getCascade()
068    {
069    return cascade;
070    }
071
072  @Override
073  public synchronized void recordInfo()
074    {
075    clientState.recordCascade( cascade );
076    }
077
078  /**
079   * Method addFlowStats add a child {@link cascading.flow.Flow} {2link FlowStats} instance.
080   *
081   * @param flowStats of type FlowStats
082   */
083  public void addFlowStats( FlowStats flowStats )
084    {
085    flowStatsMap.put( flowStats.getID(), flowStats );
086    }
087
088  /**
089   * Method getFlowCount returns the number of {@link cascading.flow.Flow}s executed by the Cascade.
090   *
091   * @return the flowCount (type int) of this CascadeStats object.
092   */
093  public int getFlowCount()
094    {
095    return flowStatsMap.size();
096    }
097
098  @Override
099  public long getLastSuccessfulCounterFetchTime()
100    {
101    long max = -1;
102
103    for( FlowStats flowStats : flowStatsMap.values() )
104      max = Math.max( max, flowStats.getLastSuccessfulCounterFetchTime() );
105
106    return max;
107    }
108
109  @Override
110  public Collection<String> getCounterGroups()
111    {
112    Set<String> results = new HashSet<String>();
113
114    for( FlowStats flowStats : flowStatsMap.values() )
115      results.addAll( flowStats.getCounterGroups() );
116
117    return results;
118    }
119
120  @Override
121  public Collection<String> getCounterGroupsMatching( String regex )
122    {
123    Set<String> results = new HashSet<String>();
124
125    for( FlowStats flowStats : flowStatsMap.values() )
126      results.addAll( flowStats.getCounterGroupsMatching( regex ) );
127
128    return results;
129    }
130
131  @Override
132  public Collection<String> getCountersFor( String group )
133    {
134    Set<String> results = new HashSet<String>();
135
136    for( FlowStats flowStats : flowStatsMap.values() )
137      results.addAll( flowStats.getCountersFor( group ) );
138
139    return results;
140    }
141
142  @Override
143  public long getCounterValue( Enum counter )
144    {
145    long value = 0;
146
147    for( FlowStats flowStats : flowStatsMap.values() )
148      value += flowStats.getCounterValue( counter );
149
150    return value;
151    }
152
153  @Override
154  public long getCounterValue( String group, String counter )
155    {
156    long value = 0;
157
158    for( FlowStats flowStats : flowStatsMap.values() )
159      value += flowStats.getCounterValue( group, counter );
160
161    return value;
162    }
163
164  @Override
165  public void captureDetail( Type depth )
166    {
167    if( !getType().isChild( depth ) )
168      return;
169
170    for( FlowStats flowStats : flowStatsMap.values() )
171      flowStats.captureDetail( depth );
172    }
173
174  @Override
175  public Collection<FlowStats> getChildren()
176    {
177    return flowStatsMap.values();
178    }
179
180  @Override
181  public FlowStats getChildWith( String id )
182    {
183    return flowStatsMap.get( id );
184    }
185
186  @Override
187  public String toString()
188    {
189    return "Cascade{" + "flowStatsList=" + flowStatsMap.values() + '}';
190    }
191  }