Search in sources :

Example 1 with Plot

use of net.opentsdb.graph.Plot in project opentsdb by OpenTSDB.

the class CliQuery method doQuery.

private static Plot doQuery(final TSDB tsdb, final String[] args, final boolean want_plot) {
    final ArrayList<String> plotparams = new ArrayList<String>();
    final ArrayList<Query> queries = new ArrayList<Query>();
    final ArrayList<String> plotoptions = new ArrayList<String>();
    parseCommandLineQuery(args, tsdb, queries, plotparams, plotoptions);
    if (queries.isEmpty()) {
        usage(null, "Not enough arguments, need at least one query.", 2);
    }
    final Plot plot = (want_plot ? new Plot(queries.get(0).getStartTime(), queries.get(0).getEndTime()) : null);
    if (want_plot) {
        plot.setParams(parsePlotParams(plotparams));
    }
    final int nqueries = queries.size();
    for (int i = 0; i < nqueries; i++) {
        // TODO(tsuna): Optimization: run each query in parallel.
        final StringBuilder buf = want_plot ? null : new StringBuilder();
        for (final DataPoints datapoints : queries.get(i).run()) {
            if (want_plot) {
                plot.add(datapoints, plotoptions.get(i));
            } else {
                final String metric = datapoints.metricName();
                final String tagz = datapoints.getTags().toString();
                for (final DataPoint datapoint : datapoints) {
                    buf.append(metric).append(' ').append(datapoint.timestamp()).append(' ');
                    if (datapoint.isInteger()) {
                        buf.append(datapoint.longValue());
                    } else {
                        buf.append(String.format("%f", datapoint.doubleValue()));
                    }
                    buf.append(' ').append(tagz).append('\n');
                    System.out.print(buf);
                    buf.delete(0, buf.length());
                }
            }
        }
    }
    return plot;
}
Also used : Query(net.opentsdb.core.Query) DataPoint(net.opentsdb.core.DataPoint) Plot(net.opentsdb.graph.Plot) ArrayList(java.util.ArrayList) DataPoints(net.opentsdb.core.DataPoints) DataPoint(net.opentsdb.core.DataPoint)

Example 2 with Plot

use of net.opentsdb.graph.Plot in project opentsdb by OpenTSDB.

the class CliQuery method main.

public static void main(String[] args) throws Exception {
    ArgP argp = new ArgP();
    CliOptions.addCommon(argp);
    CliOptions.addVerbose(argp);
    argp.addOption("--graph", "BASEPATH", "Output data points to a set of files for gnuplot." + "  The path of the output files will start with" + " BASEPATH.");
    args = CliOptions.parse(argp, args);
    if (args == null) {
        usage(argp, "Invalid usage.", 1);
    } else if (args.length < 3) {
        usage(argp, "Not enough arguments.", 2);
    }
    // get a config object
    Config config = CliOptions.getConfig(argp);
    final TSDB tsdb = new TSDB(config);
    tsdb.checkNecessaryTablesExist().joinUninterruptibly();
    final String basepath = argp.get("--graph");
    argp = null;
    Plot plot = null;
    try {
        plot = doQuery(tsdb, args, basepath != null);
    } finally {
        try {
            tsdb.shutdown().joinUninterruptibly();
        } catch (Exception e) {
            LOG.error("Unexpected exception", e);
            System.exit(1);
        }
    }
    if (plot != null) {
        try {
            final int npoints = plot.dumpToFiles(basepath);
            LOG.info("Wrote " + npoints + " for Gnuplot");
        } catch (IOException e) {
            LOG.error("Failed to write the Gnuplot file under " + basepath, e);
            System.exit(1);
        }
    }
}
Also used : Config(net.opentsdb.utils.Config) Plot(net.opentsdb.graph.Plot) TSDB(net.opentsdb.core.TSDB) IOException(java.io.IOException) IOException(java.io.IOException) DataPoint(net.opentsdb.core.DataPoint)

Example 3 with Plot

use of net.opentsdb.graph.Plot in project opentsdb by OpenTSDB.

the class GraphHandler method doGraph.

// TODO(HugoMFernandes): Most of this (query-related) logic is implemented in
// net.opentsdb.tsd.QueryRpc.java (which actually does this asynchronously),
// so we should refactor both classes to split the actual logic used to
// generate the data from the actual visualization (removing all duped code).
private void doGraph(final TSDB tsdb, final HttpQuery query) throws IOException {
    final String basepath = getGnuplotBasePath(tsdb, query);
    long start_time = DateTime.parseDateTimeString(query.getRequiredQueryStringParam("start"), query.getQueryStringParam("tz"));
    final boolean nocache = query.hasQueryStringParam("nocache");
    if (start_time == -1) {
        throw BadRequestException.missingParameter("start");
    } else {
        // temp fixup to seconds from ms until the rest of TSDB supports ms
        // Note you can't append this to the DateTime.parseDateTimeString() call as
        // it clobbers -1 results
        start_time /= 1000;
    }
    long end_time = DateTime.parseDateTimeString(query.getQueryStringParam("end"), query.getQueryStringParam("tz"));
    final long now = System.currentTimeMillis() / 1000;
    if (end_time == -1) {
        end_time = now;
    } else {
        // temp fixup to seconds from ms until the rest of TSDB supports ms
        // Note you can't append this to the DateTime.parseDateTimeString() call as
        // it clobbers -1 results
        end_time /= 1000;
    }
    final int max_age = computeMaxAge(query, start_time, end_time, now);
    if (!nocache && isDiskCacheHit(query, end_time, max_age, basepath)) {
        return;
    }
    // Parse TSQuery from HTTP query
    final TSQuery tsquery = QueryRpc.parseQuery(tsdb, query);
    tsquery.validateAndSetQuery();
    // Build the queries for the parsed TSQuery
    Query[] tsdbqueries = tsquery.buildQueries(tsdb);
    List<String> options = null;
    final String options_allow_list = tsdb.getConfig().getString("tsd.gnuplot.options.allowlist");
    if (!Strings.isNullOrEmpty(options_allow_list)) {
        String[] allow_list_strings = options_allow_list.split(";");
        Set<String> allow_list = Sets.newHashSet();
        for (int i = 0; i < allow_list_strings.length; i++) {
            String allow = allow_list_strings[i];
            if (allow != null) {
                allow = URLDecoder.decode(allow.trim());
                allow_list.add(allow);
            }
        }
        options = query.getQueryStringParams("o");
        for (int i = 0; i < options.size(); i++) {
            if (!allow_list.contains(options.get(i))) {
                throw new BadRequestException("Query option at index " + i + " was not in the allow list.");
            }
        }
    }
    if (options == null) {
        options = new ArrayList<String>(tsdbqueries.length);
        for (int i = 0; i < tsdbqueries.length; i++) {
            options.add("");
        }
    } else if (options.size() != tsdbqueries.length) {
        throw new BadRequestException(options.size() + " `o' parameters, but " + tsdbqueries.length + " `m' parameters.");
    }
    for (final Query tsdbquery : tsdbqueries) {
        try {
            tsdbquery.setStartTime(start_time);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("start time: " + e.getMessage());
        }
        try {
            tsdbquery.setEndTime(end_time);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("end time: " + e.getMessage());
        }
    }
    final Plot plot = new Plot(start_time, end_time, DateTime.timezones.get(query.getQueryStringParam("tz")));
    setPlotDimensions(query, plot);
    setPlotParams(query, plot);
    final int nqueries = tsdbqueries.length;
    @SuppressWarnings("unchecked") final HashSet<String>[] aggregated_tags = new HashSet[nqueries];
    int npoints = 0;
    for (int i = 0; i < nqueries; i++) {
        try {
            // execute the TSDB query!
            // XXX This is slow and will block Netty.  TODO(tsuna): Don't block.
            // TODO(tsuna): Optimization: run each query in parallel.
            final DataPoints[] series = tsdbqueries[i].run();
            for (final DataPoints datapoints : series) {
                plot.add(datapoints, options.get(i));
                aggregated_tags[i] = new HashSet<String>();
                aggregated_tags[i].addAll(datapoints.getAggregatedTags());
                npoints += datapoints.aggregatedSize();
            }
        } catch (RuntimeException e) {
            logInfo(query, "Query failed (stack trace coming): " + tsdbqueries[i]);
            throw e;
        }
        // free()
        tsdbqueries[i] = null;
    }
    // free()
    tsdbqueries = null;
    if (query.hasQueryStringParam("ascii")) {
        respondAsciiQuery(query, max_age, basepath, plot);
        return;
    }
    final RunGnuplot rungnuplot = new RunGnuplot(query, max_age, plot, basepath, aggregated_tags, npoints);
    class ErrorCB implements Callback<Object, Exception> {

        public Object call(final Exception e) throws Exception {
            LOG.warn("Failed to retrieve global annotations: ", e);
            throw e;
        }
    }
    class GlobalCB implements Callback<Object, List<Annotation>> {

        public Object call(final List<Annotation> global_annotations) throws Exception {
            rungnuplot.plot.setGlobals(global_annotations);
            execGnuplot(rungnuplot, query);
            return null;
        }
    }
    // Fetch global annotations, if needed
    if (!tsquery.getNoAnnotations() && tsquery.getGlobalAnnotations()) {
        Annotation.getGlobalAnnotations(tsdb, start_time, end_time).addCallback(new GlobalCB()).addErrback(new ErrorCB());
    } else {
        execGnuplot(rungnuplot, query);
    }
}
Also used : Query(net.opentsdb.core.Query) TSQuery(net.opentsdb.core.TSQuery) DataPoints(net.opentsdb.core.DataPoints) TSQuery(net.opentsdb.core.TSQuery) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Plot(net.opentsdb.graph.Plot) DataPoint(net.opentsdb.core.DataPoint) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Annotation(net.opentsdb.meta.Annotation) Callback(com.stumbleupon.async.Callback)

Example 4 with Plot

use of net.opentsdb.graph.Plot in project opentsdb by OpenTSDB.

the class TestGraphHandler method setYRangeParams.

@Test
public void setYRangeParams() throws Exception {
    Plot plot = mock(Plot.class);
    HttpQuery query = mock(HttpQuery.class);
    Map<String, List<String>> params = Maps.newHashMap();
    when(query.getQueryString()).thenReturn(params);
    params.put("yrange", Lists.newArrayList("[0:1]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:0]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:42]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:-42]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:0.8]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:-0.8]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:42.4]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:-42.4]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:4e4]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:-4e4]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:4e-4]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:-4e-4]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:4.2e4]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[:-4.2e4]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[0:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[-5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[0.5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[-0.5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[10.5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[-10.5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[10e5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[-10e5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[10e-5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[-10e-5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[10.1e-5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[-10.1e-5:]"));
    GraphHandler.setPlotParams(query, plot);
    params.put("yrange", Lists.newArrayList("[33:system('touch /tmp/poc.txt')]"));
    try {
        GraphHandler.setPlotParams(query, plot);
        fail("Expected BadRequestException");
    } catch (BadRequestException e) {
    }
}
Also used : Plot(net.opentsdb.graph.Plot) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Plot (net.opentsdb.graph.Plot)4 DataPoint (net.opentsdb.core.DataPoint)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DataPoints (net.opentsdb.core.DataPoints)2 Query (net.opentsdb.core.Query)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 Callback (com.stumbleupon.async.Callback)1 FileNotFoundException (java.io.FileNotFoundException)1 HashSet (java.util.HashSet)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TSDB (net.opentsdb.core.TSDB)1 TSQuery (net.opentsdb.core.TSQuery)1 Annotation (net.opentsdb.meta.Annotation)1 Config (net.opentsdb.utils.Config)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1