HBaseTimelineMetricStore extends AbstractService implements TimelineMetricStore { static Map<Long, Double> updateValuesAsRate(Map<Long, Double> metricValues, boolean isDiff) { Long prevTime = null; Double prevVal = null; long step; Double diff; for(Iterator<Map.Entry<Long, Double>> it = metricValues.entrySet().iterator(); it.hasNext(); ) { Map.Entry<Long, Double> timeValueEntry = it.next(); Long currTime = timeValueEntry.getKey(); Double currVal = timeValueEntry.getValue(); if (prevTime != null) { step = currTime - prevTime; diff = currVal - prevVal; Double rate = isDiff ? diff : (diff / TimeUnit.MILLISECONDS.toSeconds(step)); timeValueEntry.setValue(rate); } else { it.remove(); } prevTime = currTime; prevVal = currVal; } return metricValues; } HBaseTimelineMetricStore(TimelineMetricConfiguration configuration); @Override TimelineMetrics getTimelineMetrics(List<String> metricNames,
      List<String> hostnames, String applicationId, String instanceId,
      Long startTime, Long endTime, Precision precision, Integer limit,
      boolean groupedByHosts, TopNConfig topNConfig, String seriesAggregateFunction); @Override TimelinePutResponse putMetrics(TimelineMetrics metrics); @Override TimelinePutResponse putContainerMetrics(List<ContainerMetric> metrics); @Override Map<String, List<TimelineMetricMetadata>> getTimelineMetricMetadata(String query); @Override Map<String, Set<String>> getHostAppsMetadata(); @Override List<String> getLiveInstances();  }
@Test public void testRateCalculationOnMetricsWithEqualValues() throws Exception { Map<Long, Double> metricValues = new TreeMap<>(); metricValues.put(1454016368371L, 1011.25); metricValues.put(1454016428371L, 1011.25); metricValues.put(1454016488371L, 1011.25); metricValues.put(1454016548371L, 1011.25); metricValues.put(1454016608371L, 1011.25); metricValues.put(1454016668371L, 1011.25); metricValues.put(1454016728371L, 1011.25); Map<Long, Double> rates = HBaseTimelineMetricStore.updateValuesAsRate(new TreeMap<>(metricValues), false); for (Map.Entry<Long, Double> rateEntry : rates.entrySet()) { Assert.assertEquals("Rate should be zero, key = " + rateEntry.getKey() + ", value = " + rateEntry.getValue(), 0.0, rateEntry.getValue()); } }
@Test public void testDiffCalculation() throws Exception { Map<Long, Double> metricValues = new TreeMap<>(); metricValues.put(1454016368371L, 1011.25); metricValues.put(1454016428371L, 1010.25); metricValues.put(1454016488371L, 1012.25); metricValues.put(1454016548371L, 1010.25); metricValues.put(1454016608371L, 1010.25); Map<Long, Double> rates = HBaseTimelineMetricStore.updateValuesAsRate(new TreeMap<>(metricValues), true); Assert.assertTrue(rates.size()==4); Assert.assertTrue(rates.containsValue(-1.0)); Assert.assertTrue(rates.containsValue(2.0)); Assert.assertTrue(rates.containsValue(0.0)); }