ThirdEyeMovingAverageFunction implements ThirdEyeFunction { @Override public MetricTimeSeries apply(StarTreeConfig config, ThirdEyeQuery query, MetricTimeSeries timeSeries) { List<MetricType> metricTypes = new ArrayList<>(); for (String metricName : timeSeries.getSchema().getNames()) { metricTypes.add(MetricType.DOUBLE); } List<String> metricNames = timeSeries.getSchema().getNames(); MetricTimeSeries movingAverage = new MetricTimeSeries(new MetricSchema(metricNames, metricTypes)); if (timeSeries.getTimeWindowSet().isEmpty()) { return movingAverage; } Long minTime = Collections.min(timeSeries.getTimeWindowSet()); Long maxTime = Collections.max(timeSeries.getTimeWindowSet()); long collectionWindow = config.getTime().getBucket().getUnit().convert(window.getSize(), window.getUnit()) / config.getTime().getBucket().getSize(); Long startTime = minTime + collectionWindow; Number[] currentSum = new Number[timeSeries.getSchema().getNumMetrics()]; Arrays.fill(currentSum, 0); for (long time = minTime; time <= maxTime; time++) { for (int j = 0; j < movingAverage.getSchema().getNumMetrics(); j++) { String metricName = movingAverage.getSchema().getMetricName(j); MetricType metricType = movingAverage.getSchema().getMetricType(j); Number newValue = timeSeries.get(time, metricName); currentSum[j] = NumberUtils.sum( currentSum[j], newValue.doubleValue() / collectionWindow, metricType); if (time >= startTime) { Number oldValue = timeSeries.get(time - collectionWindow, metricName); currentSum[j] = NumberUtils.difference( currentSum[j], oldValue.doubleValue() / collectionWindow, metricType); movingAverage.increment(time, metricName, currentSum[j]); } } } return movingAverage; } ThirdEyeMovingAverageFunction(TimeGranularity window); TimeGranularity getWindow(); @Override MetricTimeSeries apply(StarTreeConfig config, ThirdEyeQuery query, MetricTimeSeries timeSeries);  }
@Test public void testValidWindow_simple() { MetricTimeSeries timeSeries = new MetricTimeSeries(metricSchema); for (int i = 0; i < 21; i++) { timeSeries.increment(i, "L", 1); timeSeries.increment(i, "D", 1.0); } MetricTimeSeries derived = new ThirdEyeMovingAverageFunction( new TimeGranularity(7, TimeUnit.HOURS)).apply(config, query, timeSeries); Assert.assertEquals(derived.getTimeWindowSet().size(), 14); Assert.assertEquals(derived.getSchema().getNames().size(), 2); System.out.println(derived); for (Long time : derived.getTimeWindowSet()) { Assert.assertEquals(derived.get(time, "L").doubleValue(), 1.0, 0.001); } }