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;
}
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);
}
}
}
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);
}
}
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) {
}
}
Aggregations