ThirdEyeRatioFunction implements ThirdEyeFunction { @Override public MetricTimeSeries apply(StarTreeConfig config, ThirdEyeQuery query, MetricTimeSeries timeSeries) { List<String> metricNames = new ArrayList<>(timeSeries.getSchema().getNames()); List<MetricType> metricTypes = new ArrayList<>(timeSeries.getSchema().getTypes()); String ratioName = toString(); metricNames.add(ratioName); metricTypes.add(MetricType.DOUBLE); MetricSchema newSchema = new MetricSchema(metricNames, metricTypes); MetricTimeSeries newTimeSeries = new MetricTimeSeries(newSchema); for (Long time : timeSeries.getTimeWindowSet()) { MetricType denominatorType = null; for (int i = 0; i < timeSeries.getSchema().getNumMetrics(); i++) { String name = timeSeries.getSchema().getMetricName(i); newTimeSeries.increment(time, name, timeSeries.get(time, name)); if (name.equals(denominator)) { denominatorType = timeSeries.getSchema().getMetricType(i); } } Number n = timeSeries.get(time, numerator); Number d = timeSeries.get(time, denominator); if (!NumberUtils.isZero(d, denominatorType)) { double r = n.doubleValue() / d.doubleValue(); newTimeSeries.increment(time, ratioName, r); } } return newTimeSeries; } ThirdEyeRatioFunction(List<String> functionMetrics); @Override String toString(); @Override MetricTimeSeries apply(StarTreeConfig config, ThirdEyeQuery query, MetricTimeSeries timeSeries);  }
@Test public void testValid_simple() { MetricTimeSeries derived = new ThirdEyeRatioFunction(ImmutableList.of("L", "D")).apply(config, null, timeSeries); Assert.assertEquals(derived.getSchema().getNumMetrics(), 3); for (int i = 0; i < 16; i++) { Assert.assertEquals(derived.get(i, "RATIO(L,D)").doubleValue(), 0.25); } }