List of usage examples for java.util IntSummaryStatistics getAverage
public final double getAverage()
From source file:eu.cloudwave.wp5.feedbackhandler.aggregations.strategies.SimpleRequestAggregationStrategyImpl.java
/** * {@inheritDoc}/*from www. j a v a 2s. c om*/ */ @Override public RequestAggregationValues aggregate(RequestCollector requests) { IntSummaryStatistics stats = requests.getReqTimestamps().stream() .collect(Collectors.groupingBy( timestamp -> DateUtils.round(new Date(timestamp), timestampAggregation), Collectors.counting())) .values().stream().mapToInt(p -> toInt(p)).summaryStatistics(); return new RequestAggregationValuesImpl(stats.getMin(), stats.getMax(), stats.getAverage(), stats.getSum(), stats.getCount()); }
From source file:eu.cloudwave.wp5.feedbackhandler.aggregations.strategies.RequestAggregationStrategyImpl.java
/** * {@inheritDoc}/*from w w w. ja va2 s . com*/ */ @Override public RequestAggregationValues aggregate(RequestCollector requests) { double expectedCount = getExpectedNumberOfMeasurementValueGroups(); /* * Group by aggregation interval and create summary statistics with min, avg, max and count */ Collection<Long> groupedByAggregationInterval = requests.getReqTimestamps().stream() .collect(Collectors.groupingBy( timestamp -> DateUtils.round(new Date(timestamp), timestampAggregation), Collectors.counting())) .values(); int calculatedCount = groupedByAggregationInterval.size(); try { if (calculatedCount != 0) { // use integer summaryStatistics to get min, avg, max IntSummaryStatistics stats = groupedByAggregationInterval.stream().mapToInt(p -> toInt(p)) .summaryStatistics(); // no time range selected, just return int summary if (expectedCount == 0) { return new RequestAggregationValuesImpl(stats.getMin(), stats.getMax(), stats.getAverage(), stats.getSum(), stats.getCount()); } else { // if calculated count != expected count --> adjust minimum, average and count value if (Double.compare(calculatedCount, expectedCount) != 0) { double newAverage = (double) (stats.getSum() / expectedCount); return new RequestAggregationValuesImpl(0, stats.getMax(), newAverage, stats.getSum(), (int) expectedCount); } else { return new RequestAggregationValuesImpl(stats.getMin(), stats.getMax(), stats.getAverage(), stats.getSum(), (int) expectedCount); } } } else { return new RequestAggregationValuesImpl(0, 0, 0, 0, 0); } } catch (ArithmeticException e) { System.out.println(e.getMessage()); return new RequestAggregationValuesImpl(0, 0, 0, 0, 0); } }