Search in sources :

Example 1 with GrafanaStreamingOutput

use of com.srotya.sidewinder.core.utils.GrafanaStreamingOutput in project sidewinder by srotya.

the class GrafanaQueryApiv2 method queryData.

@Path("/query")
@POST
@Consumes({ MediaType.APPLICATION_JSON })
public Response queryData(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String queryString) throws ParseException {
    grafanaQueryCounter.mark();
    Context time = grafanaQueryLatency.time();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    logger.log(Level.FINE, () -> "Grafana query:" + dbName + "\t" + gson.toJson(gson.fromJson(queryString, JsonObject.class)));
    JsonObject json = gson.fromJson(queryString, JsonObject.class);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    JsonObject range = json.get("range").getAsJsonObject();
    long startTs = sdf.parse(range.get("from").getAsString()).getTime();
    long endTs = sdf.parse(range.get("to").getAsString()).getTime();
    startTs = tz.getOffset(startTs) + startTs;
    endTs = tz.getOffset(endTs) + endTs;
    List<TargetSeries> targetSeries = new ArrayList<>();
    try {
        GrafanaUtils.extractTargetsFromJson(json, targetSeries);
    } catch (InvalidFilterException e) {
        throw new BadRequestException(e.getMessage());
    }
    List<Iterator<GrafanaOutputv2>> output = new ArrayList<>();
    logger.log(Level.FINE, "Extracted targets from query json, target count:" + targetSeries.size() + " " + new Date(startTs));
    for (TargetSeries targetSeriesEntry : targetSeries) {
        logger.log(Level.FINE, () -> "Running grafana query fetch for:" + targetSeriesEntry);
        try {
            Iterator<GrafanaOutputv2> outputIterator = GrafanaUtils.queryAndGetDatav2(engine, dbName, startTs, endTs, targetSeriesEntry);
            if (outputIterator != null) {
                output.add(outputIterator);
            }
        } catch (IOException e) {
            throw new InternalServerErrorException(e);
        }
    }
    time.stop();
    // Adding sorted output so series colors do not change in grafana
    logger.log(Level.FINER, () -> "Grafana query result size:" + output.size());
    return Response.ok(new GrafanaStreamingOutput(output)).type("application/json").build();
}
Also used : Context(com.codahale.metrics.Timer.Context) InvalidFilterException(com.srotya.sidewinder.core.utils.InvalidFilterException) GrafanaStreamingOutput(com.srotya.sidewinder.core.utils.GrafanaStreamingOutput) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) Date(java.util.Date) Iterator(java.util.Iterator) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

Context (com.codahale.metrics.Timer.Context)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 GrafanaStreamingOutput (com.srotya.sidewinder.core.utils.GrafanaStreamingOutput)1 InvalidFilterException (com.srotya.sidewinder.core.utils.InvalidFilterException)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 BadRequestException (javax.ws.rs.BadRequestException)1 Consumes (javax.ws.rs.Consumes)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1