    001/*
    002 * Copyright (c) 2007-2017 Xplenty, 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.process;
    022
    023import java.util.Collection;
    024import java.util.Collections;
    025import java.util.HashSet;
    026import java.util.Map;
    027import java.util.Set;
    028
    029import cascading.flow.process.ProcessFlowStep;
    030import cascading.management.state.ClientState;
    031import cascading.stats.FlowStepStats;
    032
    033/**
    034 * ProcessStepStats is an implementation of FlowStepStats used in non-hadoop based flows like ProcessFlow.
    035 */
    036public class ProcessStepStats extends FlowStepStats
    037  {
    038  /**
    039   * The counters of the step as a Map.
    040   */
    041  private final Map<String, Map<String, Long>> counters;
    042  private final long lastFetch;
    043
    044  /**
    045   * Constructs a new ProcessStepStats instance.
    046   *
    047   * @param clientState
    048   * @param counters
    049   * @param step
    050   */
    051  public ProcessStepStats( ClientState clientState, Map<String, Map<String, Long>> counters, ProcessFlowStep step )
    052    {
    053    super( step, clientState );
    054    this.counters = counters;
    055
    056    lastFetch = this.counters != null ? System.currentTimeMillis() : -1;
    057    }
    058
    059  @Override
    060  public void recordChildStats()
    061    {
    062
    063    }
    064
    065  @Override
    066  public String getProcessStepID()
    067    {
    068    return null;
    069    }
    070
    071  @Override
    072  public long getLastSuccessfulCounterFetchTime()
    073    {
    074    return lastFetch;
    075    }
    076
    077  @Override
    078  public Collection<String> getCounterGroups()
    079    {
    080    return counters.keySet();
    081    }
    082
    083  @Override
    084  public Collection<String> getCountersFor( String group )
    085    {
    086    Map<String, Long> groupCollection = counters.get( group );
    087
    088    if( groupCollection == null )
    089      return Collections.emptySet();
    090
    091    return groupCollection.keySet();
    092    }
    093
    094  @Override
    095  public long getCounterValue( Enum counter )
    096    {
    097    Map<String, Long> counterMap = counters.get( counter.getDeclaringClass().getName() );
    098
    099    String counterString = counter.toString();
    100
    101    if( counterMap == null || !counterMap.containsKey( counterString ) )
    102      return 0;
    103
    104    return counterMap.get( counterString );
    105    }
    106
    107  @Override
    108  public long getCounterValue( String group, String counter )
    109    {
    110    Map<String, Long> counterMap = counters.get( group );
    111
    112    if( counterMap == null || !counterMap.containsKey( counter ) )
    113      return 0;
    114
    115    return counterMap.get( counter );
    116    }
    117
    118  @Override
    119  public void captureDetail()
    120    {
    121
    122    }
    123
    124  @Override
    125  public void captureDetail( Type depth )
    126    {
    127
    128    }
    129
    130  @Override
    131  public Collection<String> getCounterGroupsMatching( String regex )
    132    {
    133    Collection<String> counters = getCounterGroups();
    134
    135    Set<String> results = new HashSet<String>();
    136
    137    for( String counter : counters )
    138      {
    139      if( counter.matches( regex ) )
    140        results.add( counter );
    141      }
    142
    143    return Collections.unmodifiableCollection( results );
    144    }
    145
    146  @Override
    147  public Collection getChildren()
    148    {
    149    return Collections.emptyList();
    150    }
    151  }