Skip to content

Instantly share code, notes, and snippets.

@jsanda
Created June 16, 2015 19:11
Show Gist options
  • Save jsanda/726b2151704638841b53 to your computer and use it in GitHub Desktop.
Save jsanda/726b2151704638841b53 to your computer and use it in GitHub Desktop.
@Test
public void findAvg() throws Exception {
DateTime start = now().minusMinutes(30);
DateTime end = start.plusMinutes(20);
String tenantId = "t1";
metricsService.createTenant(new Tenant(tenantId)).toBlocking().lastOrDefault(null);
Metric<Double> m1 = new Metric<>(tenantId, GAUGE, new MetricId("m1"), asList(
new DataPoint<>(start.getMillis(), 1.1),
new DataPoint<>(start.plusMinutes(2).getMillis(), 2.2),
new DataPoint<>(start.plusMinutes(4).getMillis(), 3.3),
new DataPoint<>(end.getMillis(), 4.4)
));
Observable<Void> insertObservable = metricsService.addGaugeData(Observable.just(m1));
insertObservable.toBlocking().lastOrDefault(null);
Observable<DataPoint<Double>> observable = metricsService.findGaugeData("t1", new MetricId("m1"),
start.getMillis(), end.getMillis());
// Double avg = MathObservable.averageDouble(observable.map(DataPoint::getValue)).toBlocking().last();
List<Double> values = toList(observable.map(DataPoint::getValue));
double sum = 0.0;
for (double value : values) {
sum += value;
}
double avg = sum / values.size();
assertEquals(avg, (1.1 + 2.2 + 3.3 + 4.4) / 4, "The data does not match the expected values");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment