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();
}
Aggregations