@Test public void testExtractTargetsFromJson() throws InvalidFilterException { List<TargetSeries> targetSeries = new ArrayList<>(); String query = "{\"panelId\":3,\"range\":{\"from\":\"2017-02-09T21:20:53.328Z\",\"to\":\"2017-02-10T00:20:53.328Z\",\"raw\":{\"from\":\"now-3h\",\"to\":\"now\"}},\"rangeRaw\":{\"from\":\"now-3h\",\"to\":\"now\"},\"interval\":\"15s\",\"intervalMs\":15000,\"targets\":[{\"target\":\"interface\",\"filters\":[{}],\"correlate\":false,\"field\":\"if_octets_rx\",\"refId\":\"A\",\"type\":\"timeserie\"}],\"format\":\"json\",\"maxDataPoints\":640}"; Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonObject object = gson.fromJson(query, JsonObject.class); System.out.println(gson.toJson(object)); GrafanaUtils.extractTargetsFromJson(object, targetSeries); assertEquals(1, targetSeries.size()); assertEquals("interface", targetSeries.get(0).getMeasurementName()); assertEquals("if_octets_rx", targetSeries.get(0).getFieldName()); assertTrue(targetSeries.get(0).getTagFilter() == null); }
public static void extractTargetsFromJson(JsonObject json, List<TargetSeries> targetSeries) throws InvalidFilterException { JsonArray targets = json.get("targets").getAsJsonArray(); for (int i = 0; i < targets.size(); i++) { JsonObject jsonElement = targets.get(i).getAsJsonObject(); if (jsonElement == null) { continue; } if (jsonElement.has("target") && jsonElement.has("field")) { TagFilter filter = extractGrafanaFilter(jsonElement); Function aggregationFunction = extractGrafanaAggregation(jsonElement); FunctionIteratorFactory functionIterator = extractGrafanaFunction(jsonElement); boolean correlate = false; if (jsonElement.has("correlate")) { correlate = jsonElement.get("correlate").getAsBoolean(); } TargetSeries e = new TargetSeries(jsonElement.get("target").getAsString(), jsonElement.get("field").getAsString(), filter, aggregationFunction, correlate, functionIterator); targetSeries.add(e); logger.log(Level.FINE, () -> "Parsed and extracted target:" + e); } else if (jsonElement.has("raw") && jsonElement.get("rawQuery").getAsBoolean()) { TargetSeries e; try { e = MiscUtils.extractTargetFromQuery(jsonElement.get("raw").getAsString()); } catch (Exception e1) { throw new InvalidFilterException("Bad function expression:" + jsonElement.get("raw").getAsString() + " reason:" + e1.getMessage()); } logger.log(Level.FINE, () -> "Parsed and extracted raw query:" + e); if (e != null) { targetSeries.add(e); } } } }
GrafanaUtils { public static void extractTargetsFromJson(JsonObject json, List<TargetSeries> targetSeries) throws InvalidFilterException { JsonArray targets = json.get("targets").getAsJsonArray(); for (int i = 0; i < targets.size(); i++) { JsonObject jsonElement = targets.get(i).getAsJsonObject(); if (jsonElement == null) { continue; } if (jsonElement.has("target") && jsonElement.has("field")) { TagFilter filter = extractGrafanaFilter(jsonElement); Function aggregationFunction = extractGrafanaAggregation(jsonElement); FunctionIteratorFactory functionIterator = extractGrafanaFunction(jsonElement); boolean correlate = false; if (jsonElement.has("correlate")) { correlate = jsonElement.get("correlate").getAsBoolean(); } TargetSeries e = new TargetSeries(jsonElement.get("target").getAsString(), jsonElement.get("field").getAsString(), filter, aggregationFunction, correlate, functionIterator); targetSeries.add(e); logger.log(Level.FINE, () -> "Parsed and extracted target:" + e); } else if (jsonElement.has("raw") && jsonElement.get("rawQuery").getAsBoolean()) { TargetSeries e; try { e = MiscUtils.extractTargetFromQuery(jsonElement.get("raw").getAsString()); } catch (Exception e1) { throw new InvalidFilterException("Bad function expression:" + jsonElement.get("raw").getAsString() + " reason:" + e1.getMessage()); } logger.log(Level.FINE, () -> "Parsed and extracted raw query:" + e); if (e != null) { targetSeries.add(e); } } } } }
GrafanaUtils { public static void extractTargetsFromJson(JsonObject json, List<TargetSeries> targetSeries) throws InvalidFilterException { JsonArray targets = json.get("targets").getAsJsonArray(); for (int i = 0; i < targets.size(); i++) { JsonObject jsonElement = targets.get(i).getAsJsonObject(); if (jsonElement == null) { continue; } if (jsonElement.has("target") && jsonElement.has("field")) { TagFilter filter = extractGrafanaFilter(jsonElement); Function aggregationFunction = extractGrafanaAggregation(jsonElement); FunctionIteratorFactory functionIterator = extractGrafanaFunction(jsonElement); boolean correlate = false; if (jsonElement.has("correlate")) { correlate = jsonElement.get("correlate").getAsBoolean(); } TargetSeries e = new TargetSeries(jsonElement.get("target").getAsString(), jsonElement.get("field").getAsString(), filter, aggregationFunction, correlate, functionIterator); targetSeries.add(e); logger.log(Level.FINE, () -> "Parsed and extracted target:" + e); } else if (jsonElement.has("raw") && jsonElement.get("rawQuery").getAsBoolean()) { TargetSeries e; try { e = MiscUtils.extractTargetFromQuery(jsonElement.get("raw").getAsString()); } catch (Exception e1) { throw new InvalidFilterException("Bad function expression:" + jsonElement.get("raw").getAsString() + " reason:" + e1.getMessage()); } logger.log(Level.FINE, () -> "Parsed and extracted raw query:" + e); if (e != null) { targetSeries.add(e); } } } } private  GrafanaUtils(); }
GrafanaUtils { public static void extractTargetsFromJson(JsonObject json, List<TargetSeries> targetSeries) throws InvalidFilterException { JsonArray targets = json.get("targets").getAsJsonArray(); for (int i = 0; i < targets.size(); i++) { JsonObject jsonElement = targets.get(i).getAsJsonObject(); if (jsonElement == null) { continue; } if (jsonElement.has("target") && jsonElement.has("field")) { TagFilter filter = extractGrafanaFilter(jsonElement); Function aggregationFunction = extractGrafanaAggregation(jsonElement); FunctionIteratorFactory functionIterator = extractGrafanaFunction(jsonElement); boolean correlate = false; if (jsonElement.has("correlate")) { correlate = jsonElement.get("correlate").getAsBoolean(); } TargetSeries e = new TargetSeries(jsonElement.get("target").getAsString(), jsonElement.get("field").getAsString(), filter, aggregationFunction, correlate, functionIterator); targetSeries.add(e); logger.log(Level.FINE, () -> "Parsed and extracted target:" + e); } else if (jsonElement.has("raw") && jsonElement.get("rawQuery").getAsBoolean()) { TargetSeries e; try { e = MiscUtils.extractTargetFromQuery(jsonElement.get("raw").getAsString()); } catch (Exception e1) { throw new InvalidFilterException("Bad function expression:" + jsonElement.get("raw").getAsString() + " reason:" + e1.getMessage()); } logger.log(Level.FINE, () -> "Parsed and extracted raw query:" + e); if (e != null) { targetSeries.add(e); } } } } private  GrafanaUtils(); static void queryAndGetData(StorageEngine engine, String dbName, long startTs, long endTs,
			List<GrafanaOutput> output, TargetSeries targetSeriesEntry); static Iterator<GrafanaOutputv2> queryAndGetDatav2(StorageEngine engine, String dbName, long startTs,
			long endTs, TargetSeries targetSeriesEntry); static void extractTargetsFromJson(JsonObject json, List<TargetSeries> targetSeries); static Function extractGrafanaAggregation(JsonObject jsonElement); static FunctionIteratorFactory extractGrafanaFunction(JsonObject jsonElement); static TagFilter extractGrafanaFilter(JsonObject element); static int toSeconds(String unit); }
GrafanaUtils { public static void extractTargetsFromJson(JsonObject json, List<TargetSeries> targetSeries) throws InvalidFilterException { JsonArray targets = json.get("targets").getAsJsonArray(); for (int i = 0; i < targets.size(); i++) { JsonObject jsonElement = targets.get(i).getAsJsonObject(); if (jsonElement == null) { continue; } if (jsonElement.has("target") && jsonElement.has("field")) { TagFilter filter = extractGrafanaFilter(jsonElement); Function aggregationFunction = extractGrafanaAggregation(jsonElement); FunctionIteratorFactory functionIterator = extractGrafanaFunction(jsonElement); boolean correlate = false; if (jsonElement.has("correlate")) { correlate = jsonElement.get("correlate").getAsBoolean(); } TargetSeries e = new TargetSeries(jsonElement.get("target").getAsString(), jsonElement.get("field").getAsString(), filter, aggregationFunction, correlate, functionIterator); targetSeries.add(e); logger.log(Level.FINE, () -> "Parsed and extracted target:" + e); } else if (jsonElement.has("raw") && jsonElement.get("rawQuery").getAsBoolean()) { TargetSeries e; try { e = MiscUtils.extractTargetFromQuery(jsonElement.get("raw").getAsString()); } catch (Exception e1) { throw new InvalidFilterException("Bad function expression:" + jsonElement.get("raw").getAsString() + " reason:" + e1.getMessage()); } logger.log(Level.FINE, () -> "Parsed and extracted raw query:" + e); if (e != null) { targetSeries.add(e); } } } } private  GrafanaUtils(); static void queryAndGetData(StorageEngine engine, String dbName, long startTs, long endTs,
			List<GrafanaOutput> output, TargetSeries targetSeriesEntry); static Iterator<GrafanaOutputv2> queryAndGetDatav2(StorageEngine engine, String dbName, long startTs,
			long endTs, TargetSeries targetSeriesEntry); static void extractTargetsFromJson(JsonObject json, List<TargetSeries> targetSeries); static Function extractGrafanaAggregation(JsonObject jsonElement); static FunctionIteratorFactory extractGrafanaFunction(JsonObject jsonElement); static TagFilter extractGrafanaFilter(JsonObject element); static int toSeconds(String unit);  }