Search in sources :

Example 36 with DataPoints

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

the class TestAlias method evaluateWithATag.

@Test
public void evaluateWithATag() 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.getTagsAsync()).thenReturn(Deferred.fromResult(tags));
    when(dps2.getTagUids()).thenReturn(tag_uids);
    group_bys = new DataPoints[] { dps, dps2 };
    query_results.clear();
    query_results.add(group_bys);
    params.add("My Alias.@dc");
    final DataPoints[] results = func.evaluate(data_query, query_results, params);
    assertEquals(2, results.length);
    assertEquals("My Alias.lga", results[0].metricName());
    assertEquals("My Alias.lga", results[1].metricName());
}
Also used : 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 37 with DataPoints

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

the class TestDiffSeries method diffTooManyResultSets.

@Test(expected = IllegalArgumentException.class)
public void diffTooManyResultSets() 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)

Example 38 with DataPoints

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

the class TestDivideSeries method divideTooManyResultSets.

@Test(expected = IllegalArgumentException.class)
public void divideTooManyResultSets() 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 };
    query_results.add(group_bys);
    // 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)

Example 39 with DataPoints

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

the class QueryExample method main.

public static void main(final String[] args) throws IOException {
    // Set these as arguments so you don't have to keep path information in
    // source files 
    String pathToConfigFile = (args != null && args.length > 0 ? args[0] : null);
    // Create a config object with a path to the file for parsing. Or manually
    // override settings.
    // e.g. config.overrideConfig("tsd.storage.hbase.zk_quorum", "localhost");
    final Config config;
    if (pathToConfigFile != null && !pathToConfigFile.isEmpty()) {
        config = new Config(pathToConfigFile);
    } else {
        // Search for a default config from /etc/opentsdb/opentsdb.conf, etc.
        config = new Config(true);
    }
    final TSDB tsdb = new TSDB(config);
    // main query
    final TSQuery query = new TSQuery();
    // use any string format from
    // http://opentsdb.net/docs/build/html/user_guide/query/dates.html
    query.setStart("1h-ago");
    // Optional: set other global query params
    // at least one sub query required. This is where you specify the metric and
    // tags
    final TSSubQuery subQuery = new TSSubQuery();
    subQuery.setMetric("my.tsdb.test.metric");
    // filters are optional but useful.
    final List<TagVFilter> filters = new ArrayList<TagVFilter>(1);
    filters.add(new TagVFilter.Builder().setType("literal_or").setFilter("example1").setTagk("script").setGroupBy(true).build());
    subQuery.setFilters(filters);
    // you do have to set an aggregator. Just provide the name as a string
    subQuery.setAggregator("sum");
    // IMPORTANT: don't forget to add the subQuery
    final ArrayList<TSSubQuery> subQueries = new ArrayList<TSSubQuery>(1);
    subQueries.add(subQuery);
    query.setQueries(subQueries);
    // otherwise we aggregate on the second. 
    query.setMsResolution(true);
    // make sure the query is valid. This will throw exceptions if something
    // is missing
    query.validateAndSetQuery();
    // compile the queries into TsdbQuery objects behind the scenes
    Query[] tsdbqueries = query.buildQueries(tsdb);
    // create some arrays for storing the results and the async calls
    final int nqueries = tsdbqueries.length;
    final ArrayList<DataPoints[]> results = new ArrayList<DataPoints[]>(nqueries);
    final ArrayList<Deferred<DataPoints[]>> deferreds = new ArrayList<Deferred<DataPoints[]>>(nqueries);
    // deferred in an array so we can wait for them to complete.
    for (int i = 0; i < nqueries; i++) {
        deferreds.add(tsdbqueries[i].runAsync());
    }
    // Start timer
    long startTime = DateTime.nanoTime();
    // query has finished
    class QueriesCB implements Callback<Object, ArrayList<DataPoints[]>> {

        public Object call(final ArrayList<DataPoints[]> queryResults) throws Exception {
            results.addAll(queryResults);
            return null;
        }
    }
    // Make sure to handle any errors that might crop up
    class QueriesEB implements Callback<Object, Exception> {

        @Override
        public Object call(final Exception e) throws Exception {
            System.err.println("Queries failed");
            e.printStackTrace();
            return null;
        }
    }
    // have completed.
    try {
        Deferred.groupInOrder(deferreds).addCallback(new QueriesCB()).addErrback(new QueriesEB()).join();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // End timer.
    double elapsedTime = DateTime.msFromNanoDiff(DateTime.nanoTime(), startTime);
    System.out.println("Query returned in: " + elapsedTime + " milliseconds.");
    // results and do any processing necessary.
    for (final DataPoints[] dataSets : results) {
        for (final DataPoints data : dataSets) {
            System.out.print(data.metricName());
            Map<String, String> resolvedTags = data.getTags();
            for (final Map.Entry<String, String> pair : resolvedTags.entrySet()) {
                System.out.print(" " + pair.getKey() + "=" + pair.getValue());
            }
            System.out.print("\n");
            final SeekableView it = data.iterator();
            /*
         * An important point about SeekableView:
         * Because no data is copied during iteration and no new object gets
         * created, the DataPoint returned must not be stored and gets
         * invalidated as soon as next is called on the iterator (actually it
         * doesn't get invalidated but rather its contents changes). If you want
         * to store individual data points, you need to copy the timestamp and
         * value out of each DataPoint into your own data structures.
         * 
         * In the vast majority of cases, the iterator will be used to go once
         * through all the data points, which is why it's not a problem if the
         * iterator acts just as a transient "view". Iterating will be very
         * cheap since no memory allocation is required (except to instantiate
         * the actual iterator at the beginning).
         */
            while (it.hasNext()) {
                final DataPoint dp = it.next();
                System.out.println("  " + dp.timestamp() + " " + (dp.isInteger() ? dp.longValue() : dp.doubleValue()));
            }
            System.out.println("");
        }
    }
    // Gracefully shutdown connection to TSDB
    try {
        tsdb.shutdown().join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Query(net.opentsdb.core.Query) TSQuery(net.opentsdb.core.TSQuery) TSSubQuery(net.opentsdb.core.TSSubQuery) Config(net.opentsdb.utils.Config) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) DataPoints(net.opentsdb.core.DataPoints) TSQuery(net.opentsdb.core.TSQuery) TagVFilter(net.opentsdb.query.filter.TagVFilter) DataPoint(net.opentsdb.core.DataPoint) TSDB(net.opentsdb.core.TSDB) SeekableView(net.opentsdb.core.SeekableView) TSSubQuery(net.opentsdb.core.TSSubQuery) DataPoint(net.opentsdb.core.DataPoint) IOException(java.io.IOException) Callback(com.stumbleupon.async.Callback) Map(java.util.Map)

Example 40 with DataPoints

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

the class Plot method writeGnuplotScript.

/**
   * Generates the Gnuplot script.
   * @param basepath The base path to use.
   * @param datafiles The names of the data files that need to be plotted,
   * in the order in which they ought to be plotted.  It is assumed that
   * the ith file will correspond to the ith entry in {@code datapoints}.
   * Can be {@code null} if there's no data to plot.
   */
private void writeGnuplotScript(final String basepath, final String[] datafiles) throws IOException {
    final String script_path = basepath + ".gnuplot";
    final PrintWriter gp = new PrintWriter(script_path);
    try {
        // XXX don't hardcode all those settings.  At least not like that.
        gp.append("set term png small size ").append(Short.toString(width)).append(",").append(Short.toString(height));
        final String smooth = params.remove("smooth");
        final String fgcolor = params.remove("fgcolor");
        final String style = params.remove("style");
        String bgcolor = params.remove("bgcolor");
        if (fgcolor != null && bgcolor == null) {
            // We can't specify a fgcolor without specifying a bgcolor.
            // So use a default.
            bgcolor = "xFFFFFF";
        }
        if (bgcolor != null) {
            if (fgcolor != null && "transparent".equals(bgcolor)) {
                // In case we need to specify a fgcolor but we wanted a transparent
                // background, we also need to pass a bgcolor otherwise the first
                // hex color will be mistakenly taken as a bgcolor by Gnuplot.
                bgcolor = "transparent xFFFFFF";
            }
            gp.append(' ').append(bgcolor);
        }
        if (fgcolor != null) {
            gp.append(' ').append(fgcolor);
        }
        gp.append("\n" + "set xdata time\n" + "set timefmt \"%s\"\n" + "if (GPVAL_VERSION < 4.6) set xtics rotate; else set xtics rotate right\n" + "set output \"").append(basepath + ".png").append("\"\n" + "set xrange [\"").append(String.valueOf((start_time & UNSIGNED) + utc_offset)).append("\":\"").append(String.valueOf((end_time & UNSIGNED) + utc_offset)).append("\"]\n");
        if (!params.containsKey("format x")) {
            gp.append("set format x \"").append(xFormat()).append("\"\n");
        }
        final int nseries = datapoints.size();
        if (nseries > 0) {
            gp.write("set grid\n" + "set style data ");
            gp.append(style != null ? style : "linespoint").append("\n");
            if (!params.containsKey("key")) {
                gp.write("set key right box\n");
            }
        } else {
            gp.write("unset key\n");
            if (params == null || !params.containsKey("label")) {
                gp.write("set label \"No data\" at graph 0.5,0.9 center\n");
            }
        }
        if (params != null) {
            for (final Map.Entry<String, String> entry : params.entrySet()) {
                final String key = entry.getKey();
                final String value = entry.getValue();
                if (value != null) {
                    gp.append("set ").append(key).append(' ').append(value).write('\n');
                } else {
                    gp.append("unset ").append(key).write('\n');
                }
            }
        }
        for (final String opts : options) {
            if (opts.contains("x1y2")) {
                // Create a second scale for the y-axis on the right-hand side.
                gp.write("set y2tics border\n");
                break;
            }
        }
        // compile annotations to determine if we have any to graph
        final List<Annotation> notes = new ArrayList<Annotation>();
        for (int i = 0; i < nseries; i++) {
            final DataPoints dp = datapoints.get(i);
            final List<Annotation> series_notes = dp.getAnnotations();
            if (series_notes != null && !series_notes.isEmpty()) {
                notes.addAll(series_notes);
            }
        }
        if (globals != null) {
            notes.addAll(globals);
        }
        if (notes.size() > 0) {
            Collections.sort(notes);
            for (Annotation note : notes) {
                String ts = Long.toString(note.getStartTime());
                String value = new String(note.getDescription());
                gp.append("set arrow from \"").append(ts).append("\", graph 0 to \"");
                gp.append(ts).append("\", graph 1 nohead ls 3\n");
                gp.append("set object rectangle at \"").append(ts);
                gp.append("\", graph 0 size char (strlen(\"").append(value);
                gp.append("\")), char 1 front fc rgbcolor \"white\"\n");
                gp.append("set label \"").append(value).append("\" at \"");
                gp.append(ts).append("\", graph 0 front center\n");
            }
        }
        gp.write("plot ");
        for (int i = 0; i < nseries; i++) {
            final DataPoints dp = datapoints.get(i);
            final String title = dp.metricName() + dp.getTags();
            gp.append(" \"").append(datafiles[i]).append("\" using 1:2");
            if (smooth != null) {
                gp.append(" smooth ").append(smooth);
            }
            // TODO(tsuna): Escape double quotes in title.
            gp.append(" title \"").append(title).write('"');
            final String opts = options.get(i);
            if (!opts.isEmpty()) {
                gp.append(' ').write(opts);
            }
            if (i != nseries - 1) {
                gp.print(", \\");
            }
            gp.write('\n');
        }
        if (nseries == 0) {
            gp.write('0');
        }
    } finally {
        gp.close();
        LOG.info("Wrote Gnuplot script to " + script_path);
    }
}
Also used : ArrayList(java.util.ArrayList) DataPoints(net.opentsdb.core.DataPoints) Map(java.util.Map) DataPoint(net.opentsdb.core.DataPoint) Annotation(net.opentsdb.meta.Annotation) PrintWriter(java.io.PrintWriter)

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