IntegralTransform implements Transform { @Override public List<Metric> transform(QueryContext context, List<Metric> metrics) { SystemAssert.requireArgument(metrics != null, "Cannot transform null metric/metrics"); List<Metric> result = new ArrayList<>(metrics.size()); for (Metric metric : metrics) { Map<Long, Double> sortedDatapoints = new TreeMap<>(); Double prevSum = 0.0; sortedDatapoints.putAll(metric.getDatapoints()); for (Entry<Long, Double> entry : sortedDatapoints.entrySet()) { prevSum += entry.getValue(); sortedDatapoints.put(entry.getKey(), prevSum); } metric.setDatapoints(sortedDatapoints); result.add(metric); } return result; }  @Override List<Metric> transform(QueryContext context, List<Metric> metrics); @Override List<Metric> transform(QueryContext queryContext, List<Metric> metrics, List<String> constants); @Override String getResultScopeName(); @Override List<Metric> transform(QueryContext queryContext, List<Metric>... listOfList);  }
@Test public void test_shouldReturnIntegralForOneMetric() { Transform integralTransform = new MetricMappingTransform(new IntegralValueMapping()); Metric m1 = new Metric("test_scope", "test_metric"); Map<Long, Double> dp = new HashMap<Long, Double>(); dp.put(2L, 3.0); dp.put(4L, 5.0); dp.put(5L, 2.2); m1.setDatapoints(dp); List<Metric> metrics = Arrays.asList(m1); List<Metric> result = integralTransform.transform(null, metrics); assertThat(result, equalTo(metrics)); Map<Long, Double> expectedDps = new HashMap<Long, Double>(); expectedDps.put(2L, 3.0); expectedDps.put(4L, 8.0); expectedDps.put(5L, 10.2); assertThat(result.get(0).getDatapoints(), equalTo(expectedDps)); }
@Test public void transform_shouldReturnIntegralForTwoMetrics() { Transform integralTransform = new MetricMappingTransform(new IntegralValueMapping()); Metric m1 = new Metric("test_scope", "test_metric"); HashMap<Long, Double> dp1 = new HashMap<Long, Double>(); dp1.put(2L, 4.0); dp1.put(3L, 6.0); m1.setDatapoints(dp1); Metric m2 = new Metric("test_scope", "test_metric"); HashMap<Long, Double> dp2 = new HashMap<Long, Double>(); dp2.put(3L, 5.0); dp2.put(9L, 2.0); dp2.put(2L, 6.0); m2.setDatapoints(dp2); List<Metric> metrics = Arrays.asList(m1, m2); List<Metric> results = integralTransform.transform(null, metrics); assertThat(results, equalTo(metrics)); Map<Long, Double> expectedDps1 = new HashMap<Long, Double>(); expectedDps1.put(2L, 4.0); expectedDps1.put(3L, 10.0); assertThat(results.get(0).getDatapoints(), equalTo(expectedDps1)); Map<Long, Double> expectedDps2 = new HashMap<Long, Double>(); expectedDps2.put(2L, 6.0); expectedDps2.put(3L, 11.0); expectedDps2.put(9L, 13.0); assertThat(results.get(1).getDatapoints(), equalTo(expectedDps2)); }
@Test public void test_shouldReturnIntegralForOneMetricHavingNull() { Transform integralTransform = new MetricMappingTransform(new IntegralValueMapping()); Metric m1 = new Metric("test_scope", "test_metric"); Map<Long, Double> dp = new HashMap<Long, Double>(); dp.put(2L, 3.0); dp.put(4L, 5.0); dp.put(5L, null); m1.setDatapoints(dp); List<Metric> metrics = Arrays.asList(m1); List<Metric> result = integralTransform.transform(null, metrics); assertThat(result, equalTo(metrics)); Map<Long, Double> expectedDps = new HashMap<Long, Double>(); expectedDps.put(2L, 3.0); expectedDps.put(4L, 8.0); expectedDps.put(5L, 8.0); assertThat(result.get(0).getDatapoints(), equalTo(expectedDps)); }