use of net.opentsdb.core.Aggregator in project opentsdb by OpenTSDB.
the class MovingAverage 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 moving average window size");
}
String param = params.get(0);
if (param == null || param.isEmpty()) {
throw new IllegalArgumentException("Missing moving average window size");
}
param = param.trim();
long condition = -1;
boolean is_time_unit = false;
if (param.matches("^[0-9]+$")) {
try {
condition = Integer.parseInt(param);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Invalid parameter, must be an integer", nfe);
}
} else if (param.startsWith("'") && param.endsWith("'")) {
condition = parseParam(param);
is_time_unit = true;
} else {
throw new IllegalArgumentException("Unparseable window size: " + param);
}
if (condition <= 0) {
throw new IllegalArgumentException("Moving average window must be an " + "integer greater than zero");
}
int num_results = 0;
for (final DataPoints[] results : query_results) {
num_results += results.length;
}
final PostAggregatedDataPoints[] post_agg_results = new PostAggregatedDataPoints[num_results];
int ix = 0;
// one or more 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) - Avoid iterating and copying if we can help it. We should
// be able to pass the original DataPoints object to the seekable view
// and then iterate through it.
final List<DataPoint> mutable_points = new ArrayList<DataPoint>();
for (final DataPoint point : dps) {
// avoid flip-flopping between integers and floats, always use double
// for average.
mutable_points.add(MutableDataPoint.ofDoubleValue(point.timestamp(), point.toDouble()));
}
post_agg_results[ix++] = new PostAggregatedDataPoints(dps, mutable_points.toArray(new DataPoint[mutable_points.size()]));
}
}
final DataPoints[] results = new DataPoints[num_results];
for (int i = 0; i < num_results; i++) {
final Aggregator moving_average = new MovingAverageAggregator(Aggregators.Interpolation.LERP, "movingAverage", condition, is_time_unit);
final SeekableView[] metrics_groups = new SeekableView[] { post_agg_results[i].iterator() };
final SeekableView view = new AggregationIterator(metrics_groups, data_query.startTime(), data_query.endTime(), moving_average, Aggregators.Interpolation.LERP, false);
final List<DataPoint> points = new ArrayList<DataPoint>();
while (view.hasNext()) {
final DataPoint mdp = view.next();
points.add(MutableDataPoint.ofDoubleValue(mdp.timestamp(), mdp.toDouble()));
}
results[i] = new PostAggregatedDataPoints(post_agg_results[i], points.toArray(new DataPoint[points.size()]));
}
return results;
}
use of net.opentsdb.core.Aggregator in project opentsdb by OpenTSDB.
the class CliQuery method parseCommandLineQuery.
/**
* Parses the query from the command lines.
* @param args The command line arguments.
* @param tsdb The TSDB to use.
* @param queries The list in which {@link Query}s will be appended.
* @param plotparams The list in which global plot parameters will be
* appended. Ignored if {@code null}.
* @param plotoptions The list in which per-line plot options will be
* appended. Ignored if {@code null}.
*/
static void parseCommandLineQuery(final String[] args, final TSDB tsdb, final ArrayList<Query> queries, final ArrayList<String> plotparams, final ArrayList<String> plotoptions) {
long start_ts = DateTime.parseDateTimeString(args[0], null);
if (start_ts >= 0)
start_ts /= 1000;
long end_ts = -1;
if (args.length > 3) {
// see if we can detect an end time
try {
if (args[1].charAt(0) != '+' && (args[1].indexOf(':') >= 0 || args[1].indexOf('/') >= 0 || args[1].indexOf('-') >= 0 || Long.parseLong(args[1]) > 0)) {
end_ts = DateTime.parseDateTimeString(args[1], null);
}
} catch (NumberFormatException nfe) {
// ignore it as it means the third parameter is likely the aggregator
}
}
// it clobbers -1 results
if (end_ts >= 0)
end_ts /= 1000;
int i = end_ts < 0 ? 1 : 2;
while (i < args.length && args[i].charAt(0) == '+') {
if (plotparams != null) {
plotparams.add(args[i]);
}
i++;
}
while (i < args.length) {
final Aggregator agg = Aggregators.get(args[i++]);
final boolean rate = args[i].equals("rate");
RateOptions rate_options = new RateOptions(false, Long.MAX_VALUE, RateOptions.DEFAULT_RESET_VALUE);
if (rate) {
i++;
long counterMax = Long.MAX_VALUE;
long resetValue = RateOptions.DEFAULT_RESET_VALUE;
if (args[i].startsWith("counter")) {
String[] parts = Tags.splitString(args[i], ',');
if (parts.length >= 2 && parts[1].length() > 0) {
counterMax = Long.parseLong(parts[1]);
}
if (parts.length >= 3 && parts[2].length() > 0) {
resetValue = Long.parseLong(parts[2]);
}
rate_options = new RateOptions(true, counterMax, resetValue);
i++;
}
}
final boolean downsample = args[i].equals("downsample");
if (downsample) {
i++;
}
final long interval = downsample ? Long.parseLong(args[i++]) : 0;
final Aggregator sampler = downsample ? Aggregators.get(args[i++]) : null;
final String metric = args[i++];
final HashMap<String, String> tags = new HashMap<String, String>();
while (i < args.length && args[i].indexOf(' ', 1) < 0 && args[i].indexOf('=', 1) > 0) {
Tags.parse(tags, args[i++]);
}
if (i < args.length && args[i].indexOf(' ', 1) > 0) {
plotoptions.add(args[i++]);
}
final Query query = tsdb.newQuery();
query.setStartTime(start_ts);
if (end_ts > 0) {
query.setEndTime(end_ts);
}
query.setTimeSeries(metric, tags, agg, rate, rate_options);
if (downsample) {
query.downsample(interval, sampler);
}
queries.add(query);
}
}
Aggregations