use of org.opennms.newts.aggregate.ResultProcessor in project newts by OpenNMS.
the class CassandraSampleRepository method select.
@Override
public Results<Measurement> select(Context context, Resource resource, Optional<Timestamp> start, Optional<Timestamp> end, ResultDescriptor descriptor, Optional<Duration> resolution, SampleSelectCallback callback) {
Timer.Context timer = m_measurementSelectTimer.time();
validateSelect(start, end);
Timestamp upper = end.isPresent() ? end.get() : Timestamp.now();
Timestamp lower = start.isPresent() ? start.get() : upper.minus(Duration.seconds(86400));
Duration step;
if (resolution.isPresent()) {
step = resolution.get();
} else {
// Determine the ideal step size, splitting the interval evenly into N slices
long stepMillis = upper.minus(lower).asMillis() / TARGET_NUMBER_OF_STEPS;
// But every step must be a multiple of the interval
long intervalMillis = descriptor.getInterval().asMillis();
// If the interval is greater than the target step, use the 2 * interval as the step
if (intervalMillis >= stepMillis) {
step = descriptor.getInterval().times(2);
} else {
// Otherwise, round stepMillkeyis up to the closest multiple of intervalMillis
long remainderMillis = stepMillis % intervalMillis;
if (remainderMillis != 0) {
stepMillis = stepMillis + intervalMillis - remainderMillis;
}
step = Duration.millis(stepMillis);
}
}
LOG.debug("Querying database for resource {}, from {} to {}", resource, lower.minus(step), upper);
DriverAdapter driverAdapter = new DriverAdapter(cassandraSelect(context, resource, lower.minus(step), upper), descriptor.getSourceNames());
Results<Measurement> results;
callback.beforeProcess();
try {
results = new ResultProcessor(resource, lower, upper, descriptor, step).process(driverAdapter);
} finally {
callback.afterProcess();
}
LOG.debug("{} results returned from database", driverAdapter.getResultCount());
m_samplesSelected.mark(driverAdapter.getResultCount());
try {
return results;
} finally {
timer.stop();
}
}
Aggregations