Search in sources :

Example 41 with DataPoints

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

the class Absolute 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[] {};
    }
    int num_results = 0;
    for (DataPoints[] results : query_results) {
        num_results += results.length;
    }
    final DataPoints[] results = new DataPoints[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) {
            results[ix++] = abs(dps);
        }
    }
    return results;
}
Also used : DataPoints(net.opentsdb.core.DataPoints) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint)

Example 42 with DataPoints

use of net.opentsdb.core.DataPoints 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 43 with DataPoints

use of net.opentsdb.core.DataPoints 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 44 with DataPoints

use of net.opentsdb.core.DataPoints 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 45 with DataPoints

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

the class Scale 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 scaling factor");
    }
    // zero is fine, if useless *shrug*
    double scale_factor = 0;
    final String factor = params.get(0);
    if (factor != null && factor.matches("^[-0-9\\.]+$")) {
        try {
            scale_factor = Double.parseDouble(factor);
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("Invalid parameter, must be an integer or floating point", nfe);
        }
    } else {
        throw new IllegalArgumentException("Unparseable scale factor value: " + scale_factor);
    }
    int num_results = 0;
    for (DataPoints[] results : query_results) {
        num_results += results.length;
    }
    final DataPoints[] results = new DataPoints[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) {
            results[ix++] = scale(dps, scale_factor);
        }
    }
    return results;
}
Also used : DataPoints(net.opentsdb.core.DataPoints) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint)

Aggregations

DataPoints (net.opentsdb.core.DataPoints)82 Test (org.junit.Test)67 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)67 DataPoint (net.opentsdb.core.DataPoint)64 SeekableViewsForTest (net.opentsdb.core.SeekableViewsForTest)54 SeekableView (net.opentsdb.core.SeekableView)50 ArrayList (java.util.ArrayList)24 TSQuery (net.opentsdb.core.TSQuery)18 Annotation (net.opentsdb.meta.Annotation)17 MockDataPoints (net.opentsdb.storage.MockDataPoints)13 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)11 Matchers.anyString (org.mockito.Matchers.anyString)9 IOException (java.io.IOException)6 MutableDataPoint (net.opentsdb.core.MutableDataPoint)6 Callback (com.stumbleupon.async.Callback)5 Query (net.opentsdb.core.Query)5 TSSubQuery (net.opentsdb.core.TSSubQuery)5 Deferred (com.stumbleupon.async.Deferred)4 Map (java.util.Map)4 List (java.util.List)3