CounterUpdateAggregators { public static List<CounterUpdate> aggregate(List<CounterUpdate> counterUpdates) { if (counterUpdates == null || counterUpdates.isEmpty()) { return counterUpdates; } CounterUpdate first = counterUpdates.get(0); String kind = getCounterUpdateKind(first); if (aggregators.containsKey(kind)) { return Collections.singletonList(aggregators.get(kind).aggregate(counterUpdates)); } return counterUpdates; }  static List<CounterUpdate> aggregate(List<CounterUpdate> counterUpdates);  }
@Test public void testAggregateSum() { List<CounterUpdate> sumUpdates = new ArrayList<>(); for (int i = 0; i < 10; i++) { sumUpdates.add( new CounterUpdate() .setStructuredNameAndMetadata( new CounterStructuredNameAndMetadata() .setMetadata(new CounterMetadata().setKind(Kind.SUM.toString()))) .setInteger(longToSplitInt((long) i))); } List<CounterUpdate> aggregated = CounterUpdateAggregators.aggregate(sumUpdates); assertEquals(1, aggregated.size()); CounterUpdate combined = aggregated.get(0); assertEquals(45L, splitIntToLong(combined.getInteger())); }
@Test public void testAggregateMean() { List<CounterUpdate> meanUpdates = new ArrayList<>(); for (int i = 0; i < 10; i++) { meanUpdates.add( new CounterUpdate() .setStructuredNameAndMetadata( new CounterStructuredNameAndMetadata() .setMetadata(new CounterMetadata().setKind(Kind.MEAN.toString()))) .setIntegerMean( new IntegerMean().setSum(longToSplitInt((long) i)).setCount(longToSplitInt(1L)))); } List<CounterUpdate> aggregated = CounterUpdateAggregators.aggregate(meanUpdates); assertEquals(1, aggregated.size()); CounterUpdate combined = aggregated.get(0); assertEquals(45L, splitIntToLong(combined.getIntegerMean().getSum())); assertEquals(10L, splitIntToLong(combined.getIntegerMean().getCount())); }
@Test public void testAggregateDistribution() { List<CounterUpdate> distributionUpdates = new ArrayList<>(); for (int i = 0; i < 10; i++) { distributionUpdates.add( new CounterUpdate() .setStructuredNameAndMetadata( new CounterStructuredNameAndMetadata() .setMetadata(new CounterMetadata().setKind(Kind.DISTRIBUTION.toString()))) .setDistribution( new DistributionUpdate() .setSum(longToSplitInt((long) i)) .setMax(longToSplitInt((long) i)) .setMin(longToSplitInt((long) i)) .setCount(longToSplitInt((long) 1)))); } List<CounterUpdate> aggregated = CounterUpdateAggregators.aggregate(distributionUpdates); assertEquals(1, aggregated.size()); CounterUpdate combined = aggregated.get(0); assertEquals(45L, splitIntToLong(combined.getDistribution().getSum())); assertEquals(10L, splitIntToLong(combined.getDistribution().getCount())); assertEquals(9L, splitIntToLong(combined.getDistribution().getMax())); assertEquals(0L, splitIntToLong(combined.getDistribution().getMin())); }