Search in sources :

Example 1 with ResultProcessor

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();
    }
}
Also used : Measurement(org.opennms.newts.api.Measurement) Timer(com.codahale.metrics.Timer) Duration(org.opennms.newts.api.Duration) ResultProcessor(org.opennms.newts.aggregate.ResultProcessor) Timestamp(org.opennms.newts.api.Timestamp)

Aggregations

Timer (com.codahale.metrics.Timer)1 ResultProcessor (org.opennms.newts.aggregate.ResultProcessor)1 Duration (org.opennms.newts.api.Duration)1 Measurement (org.opennms.newts.api.Measurement)1 Timestamp (org.opennms.newts.api.Timestamp)1