Created
June 17, 2015 16:39
-
-
Save jsanda/6008fdd5aea998c66d4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class MinMax { | |
public DataPoint<Double> min; | |
public DataPoint<Double> max; | |
} | |
@Test | |
public void findRange() 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); | |
Double range = metricsService.findGaugeData("t1", new MetricId("m1"), start.getMillis(), | |
end.plusSeconds(10).getMillis()) | |
.reduce(new MinMax(), (MinMax minAndMax, DataPoint<Double> dataPoint) -> { | |
if (minAndMax.min == null) { | |
minAndMax.min = dataPoint; | |
minAndMax.max = dataPoint; | |
} else { | |
if (dataPoint.getValue() < minAndMax.min.getValue()) { | |
minAndMax.min = dataPoint; | |
} | |
if (dataPoint.getValue() > minAndMax.max.getValue()) { | |
minAndMax.max = dataPoint; | |
} | |
} | |
return minAndMax; | |
}) | |
.map((MinMax minAndMax) -> minAndMax.max.getValue() - minAndMax.min.getValue()) | |
.toBlocking().last(); | |
assertEquals(range, 4.4 - 1.1, "The range is wrong"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment