Search in sources :

Example 21 with DataPoint

use of net.opentsdb.core.DataPoint 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 22 with DataPoint

use of net.opentsdb.core.DataPoint 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 23 with DataPoint

use of net.opentsdb.core.DataPoint 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 24 with DataPoint

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

the class GraphHandler method respondAsciiQuery.

/**
   * Respond to a query that wants the output in ASCII.
   * <p>
   * When a query specifies the "ascii" query string parameter, we send the
   * data points back to the client in plain text instead of sending a PNG.
   * @param query The query we're currently serving.
   * @param max_age The maximum time (in seconds) we wanna allow clients to
   * cache the result in case of a cache hit.
   * @param basepath The base path used for the Gnuplot files.
   * @param plot The plot object to generate Gnuplot's input files.
   */
private static void respondAsciiQuery(final HttpQuery query, final int max_age, final String basepath, final Plot plot) {
    final String path = basepath + ".txt";
    PrintWriter asciifile;
    try {
        asciifile = new PrintWriter(path);
    } catch (IOException e) {
        query.internalError(e);
        return;
    }
    try {
        final StringBuilder tagbuf = new StringBuilder();
        for (final DataPoints dp : plot.getDataPoints()) {
            final String metric = dp.metricName();
            tagbuf.setLength(0);
            for (final Map.Entry<String, String> tag : dp.getTags().entrySet()) {
                tagbuf.append(' ').append(tag.getKey()).append('=').append(tag.getValue());
            }
            for (final DataPoint d : dp) {
                if (d.isInteger()) {
                    printMetricHeader(asciifile, metric, d.timestamp());
                    asciifile.print(d.longValue());
                } else {
                    // Doubles require extra processing.
                    final double value = d.doubleValue();
                    // Value might be NaN or infinity.
                    if (Double.isInfinite(value)) {
                        // Infinity is invalid.
                        throw new IllegalStateException("Infinity:" + value + " d=" + d + ", query=" + query);
                    } else if (Double.isNaN(value)) {
                        // NaNs should be skipped.
                        continue;
                    }
                    printMetricHeader(asciifile, metric, d.timestamp());
                    asciifile.print(value);
                }
                asciifile.print(tagbuf);
                asciifile.print('\n');
            }
        }
    } finally {
        asciifile.close();
    }
    try {
        query.sendFile(path, max_age);
    } catch (IOException e) {
        query.internalError(e);
    }
}
Also used : DataPoint(net.opentsdb.core.DataPoint) IOException(java.io.IOException) DataPoints(net.opentsdb.core.DataPoints) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 25 with DataPoint

use of net.opentsdb.core.DataPoint 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)

Aggregations

DataPoint (net.opentsdb.core.DataPoint)60 DataPoints (net.opentsdb.core.DataPoints)55 Test (org.junit.Test)49 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)49 SeekableViewsForTest (net.opentsdb.core.SeekableViewsForTest)47 SeekableView (net.opentsdb.core.SeekableView)46 ArrayList (java.util.ArrayList)9 MutableDataPoint (net.opentsdb.core.MutableDataPoint)7 IOException (java.io.IOException)3 Map (java.util.Map)3 AggregationIterator (net.opentsdb.core.AggregationIterator)3 Callback (com.stumbleupon.async.Callback)2 Deferred (com.stumbleupon.async.Deferred)2 PrintWriter (java.io.PrintWriter)2 HashMap (java.util.HashMap)2 Query (net.opentsdb.core.Query)2 TSSubQuery (net.opentsdb.core.TSSubQuery)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1