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