Search in sources :

Example 1 with SeekableView

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

the class Scale method scale.

/**
   * Multiplies each data point in the series by the scale factor, maintaining
   * integers if both the data point and scale are integers.
   * @param points The data points to factor
   * @param scale_factor The factor to multiply by
   * @return The resulting data points
   */
private DataPoints scale(final DataPoints points, final double scale_factor) {
    // TODO(cl) - Using an array as the size function may not return the exact
    // results and we should figure a way to avoid copying data anyway.
    final List<DataPoint> dps = new ArrayList<DataPoint>();
    final boolean scale_is_int = (scale_factor == Math.floor(scale_factor)) && !Double.isInfinite(scale_factor);
    final SeekableView view = points.iterator();
    while (view.hasNext()) {
        DataPoint pt = view.next();
        if (pt.isInteger() && scale_is_int) {
            dps.add(MutableDataPoint.ofLongValue(pt.timestamp(), (long) scale_factor * pt.longValue()));
        } else {
            // NaNs are fine here, they'll just be re-computed as NaN
            dps.add(MutableDataPoint.ofDoubleValue(pt.timestamp(), scale_factor * pt.toDouble()));
        }
    }
    final DataPoint[] results = new DataPoint[dps.size()];
    dps.toArray(results);
    return new PostAggregatedDataPoints(points, results);
}
Also used : MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint) ArrayList(java.util.ArrayList) SeekableView(net.opentsdb.core.SeekableView)

Example 2 with SeekableView

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

the class Absolute method abs.

/**
   * Iterate over each data point and store the absolute value
   * @param points The data points to modify
   * @return The resulting data points
   */
private DataPoints abs(final DataPoints points) {
    // TODO(cl) - Using an array as the size function may not return the exact
    // results and we should figure a way to avoid copying data anyway.
    final List<DataPoint> dps = new ArrayList<DataPoint>();
    final SeekableView view = points.iterator();
    while (view.hasNext()) {
        DataPoint pt = view.next();
        if (pt.isInteger()) {
            dps.add(MutableDataPoint.ofLongValue(pt.timestamp(), Math.abs(pt.longValue())));
        } else {
            dps.add(MutableDataPoint.ofDoubleValue(pt.timestamp(), Math.abs(pt.doubleValue())));
        }
    }
    final DataPoint[] results = new DataPoint[dps.size()];
    dps.toArray(results);
    return new PostAggregatedDataPoints(points, results);
}
Also used : MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint) ArrayList(java.util.ArrayList) SeekableView(net.opentsdb.core.SeekableView)

Example 3 with SeekableView

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

the class Alias 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 the alias");
    }
    final String alias_template = COMMA_JOINER.join(params);
    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) {
            // TODO(cl) - Using an array as the size function may not return the exact
            // results and we should figure a way to avoid copying data anyway.
            final List<DataPoint> new_dps_list = new ArrayList<DataPoint>();
            final SeekableView view = dps.iterator();
            while (view.hasNext()) {
                DataPoint pt = view.next();
                if (pt.isInteger()) {
                    new_dps_list.add(MutableDataPoint.ofLongValue(pt.timestamp(), Math.abs(pt.longValue())));
                } else {
                    new_dps_list.add(MutableDataPoint.ofDoubleValue(pt.timestamp(), Math.abs(pt.doubleValue())));
                }
            }
            final DataPoint[] new_dps = new DataPoint[dps.size()];
            new_dps_list.toArray(new_dps);
            final PostAggregatedDataPoints padps = new PostAggregatedDataPoints(dps, new_dps);
            padps.setAlias(alias_template);
            results[ix++] = padps;
        }
    }
    return results;
}
Also used : MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint) ArrayList(java.util.ArrayList) SeekableView(net.opentsdb.core.SeekableView) DataPoints(net.opentsdb.core.DataPoints) MutableDataPoint(net.opentsdb.core.MutableDataPoint) DataPoint(net.opentsdb.core.DataPoint)

Example 4 with SeekableView

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

the class TestAbsolute method evaluateFactorNegativeGroupByLong.

@Test
public void evaluateFactorNegativeGroupByLong() 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.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;
    long v = 1;
    for (DataPoint dp : results[0]) {
        assertEquals(ts, dp.timestamp());
        assertTrue(dp.isInteger());
        assertEquals(v, dp.longValue());
        ts += INTERVAL;
        v += 1;
    }
    ts = START_TIME;
    v = 10;
    for (DataPoint dp : results[1]) {
        assertEquals(ts, dp.timestamp());
        assertTrue(dp.isInteger());
        assertEquals(v, dp.longValue());
        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 5 with SeekableView

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

the class TestAbsolute method evaluateNegativeGroupByDouble.

@Test
public void evaluateNegativeGroupByDouble() throws Exception {
    SeekableView view2 = SeekableViewsForTest.generator(START_TIME, INTERVAL, NUM_POINTS, false, -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());
        assertTrue(dp.isInteger());
        assertEquals((long) v, dp.longValue());
        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)

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