/**
 * Copyright (C) 2009 - 2009 by OpenGamma Inc.
 * 
 * Please see distribution for license.
 */

package com.opengamma.engine.view.calcnode.stats;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Maintains ever increasing tallies of the reported metrics. 
 */
public class TotallingStatisticsGatherer implements StatisticsGatherer {

  private final ConcurrentMap<String, CalculationNodeStatistics> _nodeStatistics = new ConcurrentHashMap<String, CalculationNodeStatistics>();

  protected CalculationNodeStatistics getOrCreateNodeStatistics(final String nodeId) {
    CalculationNodeStatistics stats = _nodeStatistics.get(nodeId);
    if (stats == null) {
      stats = new CalculationNodeStatistics(nodeId);
      CalculationNodeStatistics newStats = _nodeStatistics.putIfAbsent(nodeId, stats);
      if (newStats != null) {
        stats = newStats;
      }
    }
    return stats;
  }

  @Override
  public void jobCompleted(String nodeId, long executionTime, long duration) {
    getOrCreateNodeStatistics(nodeId).recordSuccessfulJob(executionTime, duration);
  }

  @Override
  public void jobFailed(String nodeId, long duration) {
    getOrCreateNodeStatistics(nodeId).recordUnsuccessfulJob(duration);
  }

  public Map<String, CalculationNodeStatistics> getNodeStatistics() {
    return _nodeStatistics;
  }

}

@@ -161,19 +161,48 @@
 va.util.
-Map
+ArrayList;%0Aimport java.util.List
 ;%0Aimport
@@ -1320,28 +1320,21 @@
  public 
-Map%3CString, 
+List%3C
 Calculat
@@ -1385,16 +1385,57 @@
  return 
+new ArrayList%3CCalculationNodeStatistics%3E(
 _nodeSta
@@ -1441,16 +1441,26 @@
 atistics
+.values())
 ;%0A  %7D%0A%0A%7D

Return the statistics set as a list rather than a map