Search in sources :

Example 1 with Metric

use of net.opentsdb.query.pojo.Metric in project opentsdb by OpenTSDB.

the class TestQueryExecutor method nsunMetric.

@Test
public void nsunMetric() throws Exception {
    oneExtraSameE();
    final Metric metric1 = Metric.Builder().setMetric("A").setId("a").setFilter("f1").setAggregator("sum").build();
    final Metric metric2 = Metric.Builder().setMetric(NSUN_METRIC).setId("b").setFilter("f1").setAggregator("sum").build();
    metrics = Arrays.asList(metric1, metric2);
    final String json = JSON.serializeToString(getDefaultQueryBuilder().build());
    final QueryRpc rpc = new QueryRpc();
    final HttpQuery query = NettyMocks.postQuery(tsdb, "/api/query/exp", json);
    // to the correct serializer
    query.getQueryBaseRoute();
    NettyMocks.mockChannelFuture(query);
    rpc.execute(tsdb, query);
    final String response = query.response().getContent().toString(Charset.forName("UTF-8"));
    assertTrue(response.contains("\"code\":400"));
    assertTrue(response.contains("\"message\":\"No such name for '" + NSUN_METRIC + "'"));
}
Also used : Metric(net.opentsdb.query.pojo.Metric) BaseTimeSyncedIteratorTest(net.opentsdb.query.expression.BaseTimeSyncedIteratorTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with Metric

use of net.opentsdb.query.pojo.Metric in project opentsdb by OpenTSDB.

the class QueryExecutor method serialize.

/**
 * Writes the results to a ChannelBuffer to return to the caller. This will
 * iterate over all of the outputs and drop in meta data where appropriate.
 * @throws Exception if something went pear shaped
 */
private Deferred<ChannelBuffer> serialize() throws Exception {
    final long start = System.currentTimeMillis();
    // buffers and an array list to stored the deferreds
    final ChannelBuffer response = ChannelBuffers.dynamicBuffer();
    final OutputStream output_stream = new ChannelBufferOutputStream(response);
    final JsonGenerator json = JSON.getFactory().createGenerator(output_stream);
    json.writeStartObject();
    json.writeFieldName("outputs");
    json.writeStartArray();
    // We want the serializer to execute serially so we need to create a callback
    // chain so that when one DPsResolver is finished, it triggers the next to
    // start serializing.
    final Deferred<Object> cb_chain = new Deferred<Object>();
    // default to the expressions if there, or fall back to the metrics
    final List<Output> outputs;
    if (query.getOutputs() == null || query.getOutputs().isEmpty()) {
        if (query.getExpressions() != null && !query.getExpressions().isEmpty()) {
            outputs = new ArrayList<Output>(query.getExpressions().size());
            for (final Expression exp : query.getExpressions()) {
                outputs.add(Output.Builder().setId(exp.getId()).build());
            }
        } else if (query.getMetrics() != null && !query.getMetrics().isEmpty()) {
            outputs = new ArrayList<Output>(query.getMetrics().size());
            for (final Metric metric : query.getMetrics()) {
                outputs.add(Output.Builder().setId(metric.getId()).build());
            }
        } else {
            throw new IllegalArgumentException("How did we get here?? No metrics or expressions??");
        }
    } else {
        outputs = query.getOutputs();
    }
    for (final Output output : outputs) {
        if (expressions != null) {
            final ExpressionIterator it = expressions.get(output.getId());
            if (it != null) {
                cb_chain.addCallback(new SerializeExpressionIterator(tsdb, json, output, it, ts_query));
                continue;
            }
        }
        if (query.getMetrics() != null && !query.getMetrics().isEmpty()) {
            final TSSubQuery sub = sub_queries.get(output.getId());
            if (sub != null) {
                final TimeSyncedIterator it = new TimeSyncedIterator(output.getId(), sub.getFilterTagKs(), sub_query_results.get(output.getId()));
                cb_chain.addCallback(new SerializeSubIterator(tsdb, json, output, it));
                continue;
            }
        } else {
            LOG.warn("Couldn't find a variable matching: " + output.getId() + " in query " + query);
        }
    }
    /**
     * Final callback to close out the JSON array and return our results
     */
    class FinalCB implements Callback<ChannelBuffer, Object> {

        public ChannelBuffer call(final Object obj) throws Exception {
            json.writeEndArray();
            // ts_query.getQueryStats().setTimeSerialization(
            // DateTime.currentTimeMillis() - start);
            ts_query.getQueryStats().markSerializationSuccessful();
            // dump the original query
            if (true) {
                json.writeFieldName("query");
                json.writeObject(QueryExecutor.this.query);
            }
            // IMPORTANT Make sure the close the JSON array and the generator
            json.writeEndObject();
            json.close();
            return response;
        }
    }
    // trigger the callback chain here
    cb_chain.callback(null);
    return cb_chain.addCallback(new FinalCB());
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) ExpressionIterator(net.opentsdb.query.expression.ExpressionIterator) ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) OutputStream(java.io.OutputStream) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) TimeSyncedIterator(net.opentsdb.query.expression.TimeSyncedIterator) TSSubQuery(net.opentsdb.core.TSSubQuery) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Callback(com.stumbleupon.async.Callback) Expression(net.opentsdb.query.pojo.Expression) Output(net.opentsdb.query.pojo.Output) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Metric(net.opentsdb.query.pojo.Metric)

Example 3 with Metric

use of net.opentsdb.query.pojo.Metric in project opentsdb by OpenTSDB.

the class TestQueryExecutor method setup.

@Before
public void setup() {
    intersection = Join.Builder().setOperator(SetOperator.INTERSECTION).build();
    time = Timespan.Builder().setStart("1431561600").setAggregator("sum").build();
    tags = Arrays.asList(new TagVFilter.Builder().setFilter("*").setGroupBy(true).setTagk("D").setType("wildcard").build());
    filters = Arrays.asList(Filter.Builder().setId("f1").setTags(tags).build());
    final Metric metric1 = Metric.Builder().setMetric("A").setId("a").setFilter("f1").build();
    final Metric metric2 = Metric.Builder().setMetric("B").setId("b").setFilter("f1").build();
    metrics = Arrays.asList(metric1, metric2);
    expressions = Arrays.asList(Expression.Builder().setId("e").setExpression("a + b").setJoin(intersection).build());
    outputs = Arrays.asList(Output.Builder().setId("e").setAlias("A plus B").build());
}
Also used : TagVFilter(net.opentsdb.query.filter.TagVFilter) Metric(net.opentsdb.query.pojo.Metric) Before(org.junit.Before)

Example 4 with Metric

use of net.opentsdb.query.pojo.Metric in project opentsdb by OpenTSDB.

the class TestQueryExecutor method twoExpressionsOneWithoutResultsDefaultOutput.

@Test
public void twoExpressionsOneWithoutResultsDefaultOutput() throws Exception {
    oneExtraSameE();
    final Metric metric1 = Metric.Builder().setMetric("A").setId("a").setFilter("f1").setAggregator("sum").build();
    final Metric metric2 = Metric.Builder().setMetric("B").setId("b").setFilter("f1").setAggregator("sum").build();
    final Metric metric3 = Metric.Builder().setMetric("D").setId("d").setFilter("f1").setAggregator("sum").build();
    final Metric metric4 = Metric.Builder().setMetric("F").setId("f").setFilter("f1").setAggregator("sum").build();
    metrics = Arrays.asList(metric1, metric2, metric3, metric4);
    expressions = Arrays.asList(Expression.Builder().setId("e").setExpression("a + b").setJoin(intersection).build(), Expression.Builder().setId("x").setExpression("d + f").setJoin(intersection).build());
    final Query q = Query.Builder().setExpressions(expressions).setFilters(filters).setMetrics(metrics).setName("q1").setTime(time).build();
    String json = JSON.serializeToString(q);
    final QueryRpc rpc = new QueryRpc();
    final HttpQuery query = NettyMocks.postQuery(tsdb, "/api/query/exp", json);
    // to the correct serializer
    query.getQueryBaseRoute();
    NettyMocks.mockChannelFuture(query);
    rpc.execute(tsdb, query);
    final String response = query.response().getContent().toString(Charset.forName("UTF-8"));
    assertTrue(response.contains("\"id\":\"e\""));
    assertTrue(response.contains("\"dps\":[[1431561600000,12.0,18.0]"));
    assertTrue(response.contains("[1431561660000,14.0,20.0]"));
    assertTrue(response.contains("[1431561720000,16.0,22.0]"));
    assertTrue(response.contains("\"firstTimestamp\":1431561600000"));
    assertTrue(response.contains("\"index\":1"));
    assertTrue(response.contains("\"metrics\":[\"A\",\"B\"]"));
    assertTrue(response.contains("\"index\":2"));
    assertTrue(response.contains("\"id\":\"x\""));
    assertTrue(response.contains("\"dps\":[]"));
    assertTrue(response.contains("\"firstTimestamp\":0"));
    assertTrue(response.contains("\"series\":0"));
}
Also used : Query(net.opentsdb.query.pojo.Query) TSQuery(net.opentsdb.core.TSQuery) Metric(net.opentsdb.query.pojo.Metric) BaseTimeSyncedIteratorTest(net.opentsdb.query.expression.BaseTimeSyncedIteratorTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with Metric

use of net.opentsdb.query.pojo.Metric in project opentsdb by OpenTSDB.

the class TestQueryExecutor method noIntersectionsFoundNestedExpression.

@Test
public void noIntersectionsFoundNestedExpression() throws Exception {
    oneExtraSameE();
    final Metric metric1 = Metric.Builder().setMetric("A").setId("a").setFilter("f1").setAggregator("sum").build();
    final Metric metric2 = Metric.Builder().setMetric("B").setId("b").setFilter("f1").setAggregator("sum").build();
    final Metric metric3 = Metric.Builder().setMetric("D").setId("d").setFilter("f1").setAggregator("sum").build();
    metrics = Arrays.asList(metric1, metric2, metric3);
    expressions = Arrays.asList(Expression.Builder().setId("e").setExpression("a + b").setJoin(intersection).build(), Expression.Builder().setId("x").setExpression("d + e").setJoin(intersection).build());
    final Query q = Query.Builder().setExpressions(expressions).setFilters(filters).setMetrics(metrics).setName("q1").setTime(time).build();
    String json = JSON.serializeToString(q);
    final QueryRpc rpc = new QueryRpc();
    final HttpQuery query = NettyMocks.postQuery(tsdb, "/api/query/exp", json);
    // to the correct serializer
    query.getQueryBaseRoute();
    NettyMocks.mockChannelFuture(query);
    rpc.execute(tsdb, query);
    final String response = query.response().getContent().toString(Charset.forName("UTF-8"));
    assertTrue(response.contains("\"code\":400"));
    assertTrue(response.contains("\"message\":\"No intersections found"));
}
Also used : Query(net.opentsdb.query.pojo.Query) TSQuery(net.opentsdb.core.TSQuery) Metric(net.opentsdb.query.pojo.Metric) BaseTimeSyncedIteratorTest(net.opentsdb.query.expression.BaseTimeSyncedIteratorTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Metric (net.opentsdb.query.pojo.Metric)7 BaseTimeSyncedIteratorTest (net.opentsdb.query.expression.BaseTimeSyncedIteratorTest)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 TSQuery (net.opentsdb.core.TSQuery)2 Query (net.opentsdb.query.pojo.Query)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 Callback (com.stumbleupon.async.Callback)1 Deferred (com.stumbleupon.async.Deferred)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 TSSubQuery (net.opentsdb.core.TSSubQuery)1 ExpressionIterator (net.opentsdb.query.expression.ExpressionIterator)1 TimeSyncedIterator (net.opentsdb.query.expression.TimeSyncedIterator)1 TagVFilter (net.opentsdb.query.filter.TagVFilter)1 Expression (net.opentsdb.query.pojo.Expression)1 Output (net.opentsdb.query.pojo.Output)1 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1 ChannelBufferOutputStream (org.jboss.netty.buffer.ChannelBufferOutputStream)1 Before (org.junit.Before)1