<commit_before>package org.rhq.metrics.restServlet.influx;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.rhq.metrics.core.MetricsService.DEFAULT_TENANT_ID;
import static org.rhq.metrics.restServlet.influx.query.parse.InfluxQueryParser.QueryContext;
import static org.rhq.metrics.restServlet.influx.query.parse.InfluxQueryParser.SelectQueryContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Response;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.joda.time.Instant;
import org.joda.time.Interval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.rhq.metrics.core.MetricId;
import org.rhq.metrics.core.MetricsService;
import org.rhq.metrics.core.NumericData;
import org.rhq.metrics.core.NumericMetric2;
import org.rhq.metrics.restServlet.ServiceKeeper;
import org.rhq.metrics.restServlet.StringValue;
import org.rhq.metrics.restServlet.influx.query.InfluxQueryParseTreeWalker;
import org.rhq.metrics.restServlet.influx.query.parse.InfluxQueryParser;
import org.rhq.metrics.restServlet.influx.query.parse.InfluxQueryParserFactory;
import org.rhq.metrics.restServlet.influx.query.parse.QueryParseException;
import org.rhq.metrics.restServlet.influx.query.parse.definition.AggregatedColumnDefinition;
import org.rhq.metrics.restServlet.influx.query.parse.definition.BooleanExpression;
import org.rhq.metrics.restServlet.influx.query.parse.definition.FunctionArgument;
import org.rhq.metrics.restServlet.influx.query.parse.definition.GroupByClause;
import org.rhq.metrics.restServlet.influx.query.parse.definition.InfluxTimeUnit;
import org.rhq.metrics.restServlet.influx.query.parse.definition.NumberFunctionArgument;
import org.rhq.metrics.restServlet.influx.query.parse.definition.SelectQueryDefinitions;
import org.rhq.metrics.restServlet.influx.query.parse.definition.SelectQueryDefinitionsParser;
import org.rhq.metrics.restServlet.influx.query.parse.type.QueryType;
import org.rhq.metrics.restServlet.influx.query.parse.type.QueryTypeVisitor;
import org.rhq.metrics.restServlet.influx.query.translate.ToIntervalTranslator;
import org.rhq.metrics.restServlet.influx.query.validation.AggregationFunction;
import org.rhq.metrics.restServlet.influx.query.validation.IllegalQueryException;
import org.rhq.metrics.restServlet.influx.query.validation.QueryValidator;

/**
 * Some support for InfluxDB clients like Grafana.
 * This is very rough at the moment (to say it politely)
 * @author <NAME>
 */
@Path("/influx")
@Produces("application/json")
public class InfluxHandler {
    private static final Logger LOG = LoggerFactory.getLogger(InfluxHandler.class);

    @Inject
    private MetricsService metricsService;
    @Inject
    @InfluxQueryParseTreeWalker
    private ParseTreeWalker parseTreeWalker;
    @Inject
    private InfluxQueryParserFactory parserFactory;
    @Inject
    private QueryValidator queryValidator;
    @Inject
    private ToIntervalTranslator toIntervalTranslator;

    @GET
    @Path("/series")
    public void series(@Suspended final AsyncResponse asyncResponse, @QueryParam("q") String queryString) {

        if (queryString==null || queryString.isEmpty()) {
            asyncResponse.cancel();
            return;
        }

        InfluxQueryParser queryParser = parserFactory.newInstanceForQuery(queryString);
        QueryContext queryContext = queryParser.query();

        QueryType queryType;<commit_msg>Return BAD_REQUEST status when Influx query is missing<commit_after>package org.rhq.metrics.restServlet.influx;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.rhq.metrics.core.MetricsService.DEFAULT_TENANT_ID;
import static org.rhq.metrics.restServlet.influx.query.parse.InfluxQueryParser.QueryContext;
import static org.rhq.metrics.restServlet.influx.query.parse.InfluxQueryParser.SelectQueryContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Response;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.joda.time.Instant;
import org.joda.time.Interval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.rhq.metrics.core.MetricId;
import org.rhq.metrics.core.MetricsService;
import org.rhq.metrics.core.NumericData;
import org.rhq.metrics.core.NumericMetric2;
import org.rhq.metrics.restServlet.ServiceKeeper;
import org.rhq.metrics.restServlet.StringValue;
import org.rhq.metrics.restServlet.influx.query.InfluxQueryParseTreeWalker;
import org.rhq.metrics.restServlet.influx.query.parse.InfluxQueryParser;
import org.rhq.metrics.restServlet.influx.query.parse.InfluxQueryParserFactory;
import org.rhq.metrics.restServlet.influx.query.parse.QueryParseException;
import org.rhq.metrics.restServlet.influx.query.parse.definition.AggregatedColumnDefinition;
import org.rhq.metrics.restServlet.influx.query.parse.definition.BooleanExpression;
import org.rhq.metrics.restServlet.influx.query.parse.definition.FunctionArgument;
import org.rhq.metrics.restServlet.influx.query.parse.definition.GroupByClause;
import org.rhq.metrics.restServlet.influx.query.parse.definition.InfluxTimeUnit;
import org.rhq.metrics.restServlet.influx.query.parse.definition.NumberFunctionArgument;
import org.rhq.metrics.restServlet.influx.query.parse.definition.SelectQueryDefinitions;
import org.rhq.metrics.restServlet.influx.query.parse.definition.SelectQueryDefinitionsParser;
import org.rhq.metrics.restServlet.influx.query.parse.type.QueryType;
import org.rhq.metrics.restServlet.influx.query.parse.type.QueryTypeVisitor;
import org.rhq.metrics.restServlet.influx.query.translate.ToIntervalTranslator;
import org.rhq.metrics.restServlet.influx.query.validation.AggregationFunction;
import org.rhq.metrics.restServlet.influx.query.validation.IllegalQueryException;
import org.rhq.metrics.restServlet.influx.query.validation.QueryValidator;

/**
 * Some support for InfluxDB clients like Grafana.
 * This is very rough at the moment (to say it politely)
 * @author <NAME>
 */
@Path("/influx")
@Produces("application/json")
public class InfluxHandler {
    private static final Logger LOG = LoggerFactory.getLogger(InfluxHandler.class);

    @Inject
    private MetricsService metricsService;
    @Inject
    @InfluxQueryParseTreeWalker
    private ParseTreeWalker parseTreeWalker;
    @Inject
    private InfluxQueryParserFactory parserFactory;
    @Inject
    private QueryValidator queryValidator;
    @Inject
    private ToIntervalTranslator toIntervalTranslator;

    @GET
    @Path("/series")
    public void series(@Suspended final AsyncResponse asyncResponse, @QueryParam("q") String queryString) {

        if (queryString==null || queryString.isEmpty()) {
            asyncResponse.resume(Response.status(Response.Status.BAD_REQUEST).entity("Missing query").build());
            return;
        }

        InfluxQueryParser queryParser = parserFactory.newInstanceForQuery(queryString);
        QueryContext queryContext = queryParser.query();

        QueryType queryType;
{'fraction_non_alphanumeric': 0.10176390773405698, 'fraction_numerical': 0.0009868015295423707, 'mean_word_length': 5.549273021001616, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1697859', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2438, 'n_words': 379}