RollingTimeWindowMeanEnricher extends AbstractTypeTransformingEnricher<T,Double> { public ConfidenceQualifiedNumber getAverage(long fromTime, long graceAllowed) { if (timestamps.isEmpty()) { return lastAverage = new ConfidenceQualifiedNumber(lastAverage.value, 0.0d); } long firstTimestamp = -1; Iterator<Long> ti = timestamps.iterator(); while (ti.hasNext()) { firstTimestamp = ti.next(); if (firstTimestamp>0) break; } if (firstTimestamp<=0) { return lastAverage = new ConfidenceQualifiedNumber(values.get(values.size()-1).doubleValue(), 0.0d); } long lastTimestamp = timestamps.get(timestamps.size()-1); long now = fromTime; if (lastTimestamp > fromTime - graceAllowed) { now = lastTimestamp; } pruneValues(now); long windowStart = Math.max(now-timePeriod.toMilliseconds(), firstTimestamp); long windowEnd = Math.max(now-timePeriod.toMilliseconds(), lastTimestamp); Double confidence = ((double)(windowEnd - windowStart)) / timePeriod.toMilliseconds(); if (confidence <= 0.0000001d) { double lastValue = values.get(values.size()-1).doubleValue(); return lastAverage = new ConfidenceQualifiedNumber(lastValue, 0.0d); } long start = windowStart; long end; double weightedAverage = 0.0d; Iterator<T> valuesIter = values.iterator(); Iterator<Long> timestampsIter = timestamps.iterator(); while (valuesIter.hasNext()) { Number val = valuesIter.next(); Long timestamp = timestampsIter.next(); if (val!=null && timestamp >= start) { end = timestamp; weightedAverage += ((end - start) / (confidence * timePeriod.toMilliseconds())) * val.doubleValue(); start = timestamp; } } return lastAverage = new ConfidenceQualifiedNumber(weightedAverage, confidence); } RollingTimeWindowMeanEnricher(); @Deprecated  RollingTimeWindowMeanEnricher(Entity producer, AttributeSensor<T> source, 
        AttributeSensor<Double> target, Duration timePeriod); @Override void init(); @Override void onEvent(SensorEvent<T> event); void onEvent(SensorEvent<T> event, long eventTime); ConfidenceQualifiedNumber getAverage(long fromTime, long graceAllowed); static ConfigKey<Double> CONFIDENCE_REQUIRED_TO_PUBLISH; }
@Test public void testDefaultAverageWhenEmpty() { average = averager.getAverage(0, 0); assertEquals(average.value, 0d); assertEquals(average.confidence, 0.0d); }