Search in sources :

Example 1 with Datasource

use of org.opennms.newts.api.query.Datasource in project newts by OpenNMS.

the class Aggregation method next.

@Override
public Row<Measurement> next() {
    if (!hasNext())
        throw new NoSuchElementException();
    Multimap<String, Double> values = ArrayListMultimap.create();
    Map<String, Map<String, String>> aggregatedAttrs = Maps.newHashMap();
    while (inRange()) {
        // accumulate
        for (Datasource ds : getDatasources()) {
            Measurement metric = m_working.getElement(ds.getSource());
            values.put(ds.getLabel(), metric != null ? metric.getValue() : Double.NaN);
            Map<String, String> metricAttrs = aggregatedAttrs.get(ds.getLabel());
            if (metricAttrs == null) {
                metricAttrs = Maps.newHashMap();
                aggregatedAttrs.put(ds.getLabel(), metricAttrs);
            }
            if (metric != null && metric.getAttributes() != null) {
                metricAttrs.putAll(metric.getAttributes());
            }
        }
        m_working = nextWorking();
    }
    for (Datasource ds : getDatasources()) {
        Double v = aggregate(ds, values.get(ds.getLabel()));
        Map<String, String> attrs = aggregatedAttrs.get(ds.getLabel());
        m_nextOut.addElement(new Measurement(m_nextOut.getTimestamp(), m_resource, ds.getLabel(), v, attrs));
    }
    try {
        return m_nextOut;
    } finally {
        m_nextOut = m_timestamps.hasNext() ? new Row<Measurement>(m_timestamps.next(), m_resource) : null;
    }
}
Also used : Datasource(org.opennms.newts.api.query.Datasource) Measurement(org.opennms.newts.api.Measurement) Row(org.opennms.newts.api.Results.Row) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with Datasource

use of org.opennms.newts.api.query.Datasource in project newts by OpenNMS.

the class PrimaryData method next.

@Override
public Row<Measurement> next() {
    if (!hasNext())
        throw new NoSuchElementException();
    Timestamp intervalCeiling = m_timestamps.next();
    Row<Measurement> output = new Row<>(intervalCeiling, m_resource);
    for (Datasource ds : m_resultDescriptor.getDatasources().values()) {
        Accumulation accumulation = getOrCreateAccumulation(ds.getSource());
        accumulation.reset();
        int lastSampleIdx = 0;
        if (m_lastSampleIndex.containsKey(ds.getSource())) {
            lastSampleIdx = m_lastSampleIndex.get(ds.getSource());
        }
        Sample last = null;
        for (int sampleIdx = lastSampleIdx; sampleIdx < m_samples.size(); sampleIdx++) {
            Row<Sample> row = m_samples.get(sampleIdx);
            Sample current;
            current = row.getElement(ds.getSource());
            // Skip the row if it does not contain a sample for the current datasource
            if (current == null) {
                continue;
            }
            if (last == null) {
                last = current;
                lastSampleIdx = sampleIdx;
                continue;
            }
            // Accumulate nothing when samples are beyond this interval
            if (intervalCeiling.lt(last.getTimestamp())) {
                break;
            }
            Timestamp lowerBound = last.getTimestamp();
            if (lastIntervalCeiling != null && lastIntervalCeiling.gt(lowerBound)) {
                lowerBound = lastIntervalCeiling;
            }
            Timestamp upperBound = current.getTimestamp();
            if (intervalCeiling.lt(upperBound)) {
                upperBound = intervalCeiling;
            }
            if (lowerBound.gt(upperBound)) {
                lowerBound = upperBound;
            }
            Duration elapsedWithinInterval = upperBound.minus(lowerBound);
            Duration elapsedBetweenSamples = current.getTimestamp().minus(last.getTimestamp());
            m_lastSampleIndex.put(ds.getSource(), lastSampleIdx);
            accumulation.accumulateValue(elapsedWithinInterval, elapsedBetweenSamples, ds.getHeartbeat(), current.getValue()).accumlateAttrs(current.getAttributes());
            last = current;
            lastSampleIdx = sampleIdx;
        }
        // Add sample with accumulated value to output row
        output.addElement(new Measurement(output.getTimestamp(), output.getResource(), ds.getSource(), accumulation.getAverage(), accumulation.getAttributes()));
    }
    lastIntervalCeiling = intervalCeiling;
    return output;
}
Also used : Measurement(org.opennms.newts.api.Measurement) Datasource(org.opennms.newts.api.query.Datasource) Sample(org.opennms.newts.api.Sample) Duration(org.opennms.newts.api.Duration) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)2 Measurement (org.opennms.newts.api.Measurement)2 Row (org.opennms.newts.api.Results.Row)2 Datasource (org.opennms.newts.api.query.Datasource)2 Map (java.util.Map)1 Duration (org.opennms.newts.api.Duration)1 Sample (org.opennms.newts.api.Sample)1 Timestamp (org.opennms.newts.api.Timestamp)1