Search in sources :

Example 31 with SeekableView

use of net.opentsdb.core.SeekableView in project opentsdb by OpenTSDB.

the class HighestCurrent method evaluate.

@Override
public DataPoints[] evaluate(final TSQuery data_query, final List<DataPoints[]> query_results, final List<String> params) {
    if (data_query == null) {
        throw new IllegalArgumentException("Missing time series query");
    }
    if (query_results == null || query_results.isEmpty()) {
        return new DataPoints[] {};
    }
    // TODO(cl) - allow for empty top-n maybe? Just sort the results by max?
    if (params == null || params.isEmpty()) {
        throw new IllegalArgumentException("Need aggregation window for moving average");
    }
    String param = params.get(0);
    if (param == null || param.length() == 0) {
        throw new IllegalArgumentException("Missing top n value " + "(number of series to return)");
    }
    int topn = 0;
    if (param.matches("^[0-9]+$")) {
        try {
            topn = Integer.parseInt(param);
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("Invalid parameter, must be an integer", nfe);
        }
    } else {
        throw new IllegalArgumentException("Unparseable top n value: " + param);
    }
    if (topn < 1) {
        throw new IllegalArgumentException("Top n value must be greater " + "than zero: " + topn);
    }
    int num_results = 0;
    for (DataPoints[] results : query_results) {
        num_results += results.length;
    }
    final PostAggregatedDataPoints[] post_agg_results = new PostAggregatedDataPoints[num_results];
    int ix = 0;
    // one or more sub queries (m=...&m=...&m=...)
    for (final DataPoints[] sub_query_result : query_results) {
        // group bys (m=sum:foo{host=*})
        for (final DataPoints dps : sub_query_result) {
            // TODO(cl) - Avoid iterating and copying if we can help it. We should
            // be able to pass the original DataPoints object to the seekable view
            // and then iterate through it.
            final List<DataPoint> mutable_points = new ArrayList<DataPoint>();
            for (final DataPoint point : dps) {
                mutable_points.add(point.isInteger() ? MutableDataPoint.ofLongValue(point.timestamp(), point.longValue()) : MutableDataPoint.ofDoubleValue(point.timestamp(), point.doubleValue()));
            }
            post_agg_results[ix++] = new PostAggregatedDataPoints(dps, mutable_points.toArray(new DataPoint[mutable_points.size()]));
        }
    }
    final SeekableView[] views = new SeekableView[num_results];
    for (int i = 0; i < num_results; i++) {
        views[i] = post_agg_results[i].iterator();
    }
    final MaxLatestAggregator aggregator = new MaxLatestAggregator(Aggregators.Interpolation.LERP, "maxLatest", num_results, data_query.startTime(), data_query.endTime());
    final SeekableView view = (new AggregationIterator(views, data_query.startTime(), data_query.endTime(), aggregator, Aggregators.Interpolation.LERP, false));
    // slurp all the points even though we aren't using them at this stage
    while (view.hasNext()) {
        final DataPoint mdp = view.next();
        @SuppressWarnings("unused") final Object o = mdp.isInteger() ? mdp.longValue() : mdp.doubleValue();
    }
    final long[] max_longs = aggregator.getLongMaxes();
    final double[] max_doubles = aggregator.getDoubleMaxes();
    final TopNSortingEntry[] max_by_ts = new TopNSortingEntry[num_results];
    if (aggregator.hasDoubles() && aggregator.hasLongs()) {
        for (int i = 0; i < num_results; i++) {
            max_by_ts[i] = new TopNSortingEntry(Math.max((double) max_longs[i], max_doubles[i]), i);
        }
    } else if (aggregator.hasLongs() && !aggregator.hasDoubles()) {
        for (int i = 0; i < num_results; i++) {
            max_by_ts[i] = new TopNSortingEntry((double) max_longs[i], i);
        }
    } else if (aggregator.hasDoubles() && !aggregator.hasLongs()) {
        for (int i = 0; i < num_results; i++) {
            max_by_ts[i] = new TopNSortingEntry(max_doubles[i], i);
        }
    }
    Arrays.sort(max_by_ts);
    final int result_count = Math.min(topn, num_results);
    final DataPoints[] results = new DataPoints[result_count];
    for (int i = 0; i < result_count; i++) {
        results[i] = post_agg_results[max_by_ts[i].pos];
    }
    return results;
}
Also used : AggregationIterator(net.opentsdb.core.AggregationIterator) ArrayList(java.util.ArrayList) SeekableView(net.opentsdb.core.SeekableView) DataPoints(net.opentsdb.core.DataPoints) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint) TopNSortingEntry(net.opentsdb.query.expression.HighestMax.TopNSortingEntry)

Example 32 with SeekableView

use of net.opentsdb.core.SeekableView in project opentsdb by OpenTSDB.

the class HighestMax method evaluate.

@Override
public DataPoints[] evaluate(final TSQuery data_query, final List<DataPoints[]> query_results, final List<String> params) {
    if (data_query == null) {
        throw new IllegalArgumentException("Missing time series query");
    }
    if (query_results == null || query_results.isEmpty()) {
        return new DataPoints[] {};
    }
    // TODO(cl) - allow for empty top-n maybe? Just sort the results by max?
    if (params == null || params.isEmpty()) {
        throw new IllegalArgumentException("Need aggregation window for moving average");
    }
    String param = params.get(0);
    if (param == null || param.length() == 0) {
        throw new IllegalArgumentException("Missing top n value " + "(number of series to return)");
    }
    int topn = 0;
    if (param.matches("^[0-9]+$")) {
        try {
            topn = Integer.parseInt(param);
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("Invalid parameter, must be an integer", nfe);
        }
    } else {
        throw new IllegalArgumentException("Unparseable top n value: " + param);
    }
    if (topn < 1) {
        throw new IllegalArgumentException("Top n value must be greater " + "than zero: " + topn);
    }
    int num_results = 0;
    for (DataPoints[] results : query_results) {
        num_results += results.length;
    }
    final PostAggregatedDataPoints[] post_agg_results = new PostAggregatedDataPoints[num_results];
    int ix = 0;
    // one or more sub queries (m=...&m=...&m=...)
    for (final DataPoints[] sub_query_result : query_results) {
        // group bys (m=sum:foo{host=*})
        for (final DataPoints dps : sub_query_result) {
            // TODO(cl) - Avoid iterating and copying if we can help it. We should
            // be able to pass the original DataPoints object to the seekable view
            // and then iterate through it.
            final List<DataPoint> mutable_points = new ArrayList<DataPoint>();
            for (final DataPoint point : dps) {
                mutable_points.add(point.isInteger() ? MutableDataPoint.ofLongValue(point.timestamp(), point.longValue()) : MutableDataPoint.ofDoubleValue(point.timestamp(), point.doubleValue()));
            }
            post_agg_results[ix++] = new PostAggregatedDataPoints(dps, mutable_points.toArray(new DataPoint[mutable_points.size()]));
        }
    }
    final SeekableView[] views = new SeekableView[num_results];
    for (int i = 0; i < num_results; i++) {
        views[i] = post_agg_results[i].iterator();
    }
    final MaxCacheAggregator aggregator = new MaxCacheAggregator(Aggregators.Interpolation.LERP, "maxCache", num_results, data_query.startTime(), data_query.endTime());
    final SeekableView view = (new AggregationIterator(views, data_query.startTime(), data_query.endTime(), aggregator, Aggregators.Interpolation.LERP, false));
    // slurp all the points even though we aren't using them at this stage
    while (view.hasNext()) {
        final DataPoint mdp = view.next();
        @SuppressWarnings("unused") final Object o = mdp.isInteger() ? mdp.longValue() : mdp.doubleValue();
    }
    final long[] max_longs = aggregator.getLongMaxes();
    final double[] max_doubles = aggregator.getDoubleMaxes();
    final TopNSortingEntry[] max_by_ts = new TopNSortingEntry[num_results];
    if (aggregator.hasDoubles() && aggregator.hasLongs()) {
        for (int i = 0; i < num_results; i++) {
            max_by_ts[i] = new TopNSortingEntry(Math.max((double) max_longs[i], max_doubles[i]), i);
        }
    } else if (aggregator.hasLongs() && !aggregator.hasDoubles()) {
        for (int i = 0; i < num_results; i++) {
            max_by_ts[i] = new TopNSortingEntry((double) max_longs[i], i);
        }
    } else if (aggregator.hasDoubles() && !aggregator.hasLongs()) {
        for (int i = 0; i < num_results; i++) {
            max_by_ts[i] = new TopNSortingEntry(max_doubles[i], i);
        }
    }
    Arrays.sort(max_by_ts);
    final int result_count = Math.min(topn, num_results);
    final DataPoints[] results = new DataPoints[result_count];
    for (int i = 0; i < result_count; i++) {
        results[i] = post_agg_results[max_by_ts[i].pos];
    }
    return results;
}
Also used : AggregationIterator(net.opentsdb.core.AggregationIterator) ArrayList(java.util.ArrayList) SeekableView(net.opentsdb.core.SeekableView) DataPoints(net.opentsdb.core.DataPoints) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint)

Example 33 with SeekableView

use of net.opentsdb.core.SeekableView in project opentsdb by OpenTSDB.

the class MovingAverage method evaluate.

@Override
public DataPoints[] evaluate(final TSQuery data_query, final List<DataPoints[]> query_results, final List<String> params) {
    if (data_query == null) {
        throw new IllegalArgumentException("Missing time series query");
    }
    if (query_results == null || query_results.isEmpty()) {
        return new DataPoints[] {};
    }
    if (params == null || params.isEmpty()) {
        throw new IllegalArgumentException("Missing moving average window size");
    }
    String param = params.get(0);
    if (param == null || param.isEmpty()) {
        throw new IllegalArgumentException("Missing moving average window size");
    }
    param = param.trim();
    long condition = -1;
    boolean is_time_unit = false;
    if (param.matches("^[0-9]+$")) {
        try {
            condition = Integer.parseInt(param);
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("Invalid parameter, must be an integer", nfe);
        }
    } else if (param.startsWith("'") && param.endsWith("'")) {
        condition = parseParam(param);
        is_time_unit = true;
    } else {
        throw new IllegalArgumentException("Unparseable window size: " + param);
    }
    if (condition <= 0) {
        throw new IllegalArgumentException("Moving average window must be an " + "integer greater than zero");
    }
    int num_results = 0;
    for (final DataPoints[] results : query_results) {
        num_results += results.length;
    }
    final PostAggregatedDataPoints[] post_agg_results = new PostAggregatedDataPoints[num_results];
    int ix = 0;
    // one or more queries (m=...&m=...&m=...)
    for (final DataPoints[] sub_query_result : query_results) {
        // group bys (m=sum:foo{host=*})
        for (final DataPoints dps : sub_query_result) {
            // TODO(cl) - Avoid iterating and copying if we can help it. We should
            // be able to pass the original DataPoints object to the seekable view
            // and then iterate through it.
            final List<DataPoint> mutable_points = new ArrayList<DataPoint>();
            for (final DataPoint point : dps) {
                // avoid flip-flopping between integers and floats, always use double
                // for average.
                mutable_points.add(MutableDataPoint.ofDoubleValue(point.timestamp(), point.toDouble()));
            }
            post_agg_results[ix++] = new PostAggregatedDataPoints(dps, mutable_points.toArray(new DataPoint[mutable_points.size()]));
        }
    }
    final DataPoints[] results = new DataPoints[num_results];
    for (int i = 0; i < num_results; i++) {
        final Aggregator moving_average = new MovingAverageAggregator(Aggregators.Interpolation.LERP, "movingAverage", condition, is_time_unit);
        final SeekableView[] metrics_groups = new SeekableView[] { post_agg_results[i].iterator() };
        final SeekableView view = new AggregationIterator(metrics_groups, data_query.startTime(), data_query.endTime(), moving_average, Aggregators.Interpolation.LERP, false);
        final List<DataPoint> points = new ArrayList<DataPoint>();
        while (view.hasNext()) {
            final DataPoint mdp = view.next();
            points.add(MutableDataPoint.ofDoubleValue(mdp.timestamp(), mdp.toDouble()));
        }
        results[i] = new PostAggregatedDataPoints(post_agg_results[i], points.toArray(new DataPoint[points.size()]));
    }
    return results;
}
Also used : AggregationIterator(net.opentsdb.core.AggregationIterator) ArrayList(java.util.ArrayList) SeekableView(net.opentsdb.core.SeekableView) Aggregator(net.opentsdb.core.Aggregator) DataPoints(net.opentsdb.core.DataPoints) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint)

Example 34 with SeekableView

use of net.opentsdb.core.SeekableView in project opentsdb by OpenTSDB.

the class TestMovingAverage method evaluateGroupBy.

@Test
public void evaluateGroupBy() throws Exception {
    params.add("1");
    SeekableView view2 = SeekableViewsForTest.generator(START_TIME, INTERVAL, NUM_POINTS, true, 10, 1);
    DataPoints dps2 = PowerMockito.mock(DataPoints.class);
    when(dps2.iterator()).thenReturn(view2);
    when(dps2.metricNameAsync()).thenReturn(Deferred.fromResult("sys.mem"));
    group_bys = new DataPoints[] { dps, dps2 };
    query_results.clear();
    query_results.add(group_bys);
    final DataPoints[] results = func.evaluate(data_query, query_results, params);
    assertEquals(2, results.length);
    assertEquals(METRIC, results[0].metricName());
    assertEquals("sys.mem", results[1].metricName());
    long ts = START_TIME;
    double v = 1;
    for (DataPoint dp : results[0]) {
        assertEquals(ts, dp.timestamp());
        assertFalse(dp.isInteger());
        assertEquals(v, dp.doubleValue(), 0.001);
        ts += INTERVAL;
        v += 1;
    }
    ts = START_TIME;
    v = 10;
    for (DataPoint dp : results[1]) {
        assertEquals(ts, dp.timestamp());
        assertFalse(dp.isInteger());
        assertEquals(v, dp.doubleValue(), 0.001);
        ts += INTERVAL;
        v += 1;
    }
}
Also used : DataPoint(net.opentsdb.core.DataPoint) SeekableView(net.opentsdb.core.SeekableView) DataPoints(net.opentsdb.core.DataPoints) Test(org.junit.Test) SeekableViewsForTest(net.opentsdb.core.SeekableViewsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 35 with SeekableView

use of net.opentsdb.core.SeekableView in project opentsdb by OpenTSDB.

the class TestMultiplySeries method multiplyTooManyResultSets.

@Test(expected = IllegalArgumentException.class)
public void multiplyTooManyResultSets() throws Exception {
    SeekableView view2 = SeekableViewsForTest.generator(START_TIME, INTERVAL, NUM_POINTS, true, 10, 1);
    DataPoints dps2 = PowerMockito.mock(DataPoints.class);
    when(dps2.iterator()).thenReturn(view2);
    when(dps2.metricName()).thenReturn("sys.mem");
    when(dps2.metricUID()).thenReturn(new byte[] { 0, 0, 2 });
    group_bys = new DataPoints[] { dps2 };
    // doesn't matter what they are
    for (int i = 0; i < 100; i++) {
        query_results.add(group_bys);
    }
    func.evaluate(data_query, query_results, params);
}
Also used : SeekableView(net.opentsdb.core.SeekableView) DataPoints(net.opentsdb.core.DataPoints) DataPoint(net.opentsdb.core.DataPoint) Test(org.junit.Test) SeekableViewsForTest(net.opentsdb.core.SeekableViewsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

SeekableView (net.opentsdb.core.SeekableView)54 DataPoint (net.opentsdb.core.DataPoint)50 DataPoints (net.opentsdb.core.DataPoints)50 Test (org.junit.Test)47 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)47 SeekableViewsForTest (net.opentsdb.core.SeekableViewsForTest)45 ArrayList (java.util.ArrayList)7 MutableDataPoint (net.opentsdb.core.MutableDataPoint)7 AggregationIterator (net.opentsdb.core.AggregationIterator)3 NoSuchElementException (java.util.NoSuchElementException)2 Callback (com.stumbleupon.async.Callback)1 Deferred (com.stumbleupon.async.Deferred)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Aggregator (net.opentsdb.core.Aggregator)1 Query (net.opentsdb.core.Query)1 TSDB (net.opentsdb.core.TSDB)1 TSQuery (net.opentsdb.core.TSQuery)1 TSSubQuery (net.opentsdb.core.TSSubQuery)1 TopNSortingEntry (net.opentsdb.query.expression.HighestMax.TopNSortingEntry)1