@Test public void testMultiFilterExtractor() throws InvalidFilterException { JsonObject element = new JsonObject(); element.addProperty("target", "ticker"); JsonArray array = new JsonArray(); JsonObject obj = new JsonObject(); obj.addProperty("key", "testg"); obj.addProperty("operator", "<"); obj.addProperty("value", "0"); array.add(obj); obj = new JsonObject(); obj.addProperty("type", "condition"); obj.addProperty("value", "AND"); array.add(obj); obj = new JsonObject(); obj.addProperty("key", "test"); obj.addProperty("operator", ">"); obj.addProperty("value", "2"); array.add(obj); obj = new JsonObject(); obj.addProperty("type", "condition"); obj.addProperty("value", "AND"); array.add(obj); obj = new JsonObject(); obj.addProperty("key", "test2"); obj.addProperty("operator", ">"); obj.addProperty("value", "3"); array.add(obj); element.add("filters", array); System.out.println(new Gson().toJson(element)); TagFilter filter = GrafanaUtils.extractGrafanaFilter(element); System.out.println(filter); assertNotNull(filter); }
public static TagFilter extractGrafanaFilter(JsonObject element) throws InvalidFilterException { Stack<TagFilter> predicateStack = new Stack<>(); JsonArray array = element.get("filters").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.get(i).getAsJsonObject(); if (!obj.has("value")) { continue; } String val = obj.get("value").getAsString(); if (obj.has("type")) { if (predicateStack.isEmpty()) { System.err.println("Error empty stack"); } else { TagFilter pop = predicateStack.pop(); if (val.equalsIgnoreCase("and")) { ComplexTagFilter andFilter = new ComplexTagFilter(ComplexFilterType.AND); andFilter.addFilter(pop); predicateStack.push(andFilter); } else if (val.equalsIgnoreCase("or")) { ComplexTagFilter orFilter = new ComplexTagFilter(ComplexFilterType.OR); orFilter.addFilter(pop); predicateStack.push(orFilter); } else { System.err.println("Error stack is not empty"); } } } else { SimpleTagFilter filter = MiscUtils.buildSimpleFilter(obj.get("key").getAsString() + obj.get("operator").getAsString() + obj.get("value").getAsString()); logger.log(Level.FINE, () -> "Simple filter:" + filter); if (predicateStack.isEmpty()) { predicateStack.push(filter); } else { if (predicateStack.peek() instanceof SimpleTagFilter) { System.err.println("Stack contains bad filter"); } else { ((ComplexTagFilter) predicateStack.peek()).addFilter(filter); } } } } if (predicateStack.isEmpty()) { return null; } else { return predicateStack.pop(); } }
GrafanaUtils { public static TagFilter extractGrafanaFilter(JsonObject element) throws InvalidFilterException { Stack<TagFilter> predicateStack = new Stack<>(); JsonArray array = element.get("filters").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.get(i).getAsJsonObject(); if (!obj.has("value")) { continue; } String val = obj.get("value").getAsString(); if (obj.has("type")) { if (predicateStack.isEmpty()) { System.err.println("Error empty stack"); } else { TagFilter pop = predicateStack.pop(); if (val.equalsIgnoreCase("and")) { ComplexTagFilter andFilter = new ComplexTagFilter(ComplexFilterType.AND); andFilter.addFilter(pop); predicateStack.push(andFilter); } else if (val.equalsIgnoreCase("or")) { ComplexTagFilter orFilter = new ComplexTagFilter(ComplexFilterType.OR); orFilter.addFilter(pop); predicateStack.push(orFilter); } else { System.err.println("Error stack is not empty"); } } } else { SimpleTagFilter filter = MiscUtils.buildSimpleFilter(obj.get("key").getAsString() + obj.get("operator").getAsString() + obj.get("value").getAsString()); logger.log(Level.FINE, () -> "Simple filter:" + filter); if (predicateStack.isEmpty()) { predicateStack.push(filter); } else { if (predicateStack.peek() instanceof SimpleTagFilter) { System.err.println("Stack contains bad filter"); } else { ((ComplexTagFilter) predicateStack.peek()).addFilter(filter); } } } } if (predicateStack.isEmpty()) { return null; } else { return predicateStack.pop(); } } }
GrafanaUtils { public static TagFilter extractGrafanaFilter(JsonObject element) throws InvalidFilterException { Stack<TagFilter> predicateStack = new Stack<>(); JsonArray array = element.get("filters").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.get(i).getAsJsonObject(); if (!obj.has("value")) { continue; } String val = obj.get("value").getAsString(); if (obj.has("type")) { if (predicateStack.isEmpty()) { System.err.println("Error empty stack"); } else { TagFilter pop = predicateStack.pop(); if (val.equalsIgnoreCase("and")) { ComplexTagFilter andFilter = new ComplexTagFilter(ComplexFilterType.AND); andFilter.addFilter(pop); predicateStack.push(andFilter); } else if (val.equalsIgnoreCase("or")) { ComplexTagFilter orFilter = new ComplexTagFilter(ComplexFilterType.OR); orFilter.addFilter(pop); predicateStack.push(orFilter); } else { System.err.println("Error stack is not empty"); } } } else { SimpleTagFilter filter = MiscUtils.buildSimpleFilter(obj.get("key").getAsString() + obj.get("operator").getAsString() + obj.get("value").getAsString()); logger.log(Level.FINE, () -> "Simple filter:" + filter); if (predicateStack.isEmpty()) { predicateStack.push(filter); } else { if (predicateStack.peek() instanceof SimpleTagFilter) { System.err.println("Stack contains bad filter"); } else { ((ComplexTagFilter) predicateStack.peek()).addFilter(filter); } } } } if (predicateStack.isEmpty()) { return null; } else { return predicateStack.pop(); } } private  GrafanaUtils(); }
GrafanaUtils { public static TagFilter extractGrafanaFilter(JsonObject element) throws InvalidFilterException { Stack<TagFilter> predicateStack = new Stack<>(); JsonArray array = element.get("filters").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.get(i).getAsJsonObject(); if (!obj.has("value")) { continue; } String val = obj.get("value").getAsString(); if (obj.has("type")) { if (predicateStack.isEmpty()) { System.err.println("Error empty stack"); } else { TagFilter pop = predicateStack.pop(); if (val.equalsIgnoreCase("and")) { ComplexTagFilter andFilter = new ComplexTagFilter(ComplexFilterType.AND); andFilter.addFilter(pop); predicateStack.push(andFilter); } else if (val.equalsIgnoreCase("or")) { ComplexTagFilter orFilter = new ComplexTagFilter(ComplexFilterType.OR); orFilter.addFilter(pop); predicateStack.push(orFilter); } else { System.err.println("Error stack is not empty"); } } } else { SimpleTagFilter filter = MiscUtils.buildSimpleFilter(obj.get("key").getAsString() + obj.get("operator").getAsString() + obj.get("value").getAsString()); logger.log(Level.FINE, () -> "Simple filter:" + filter); if (predicateStack.isEmpty()) { predicateStack.push(filter); } else { if (predicateStack.peek() instanceof SimpleTagFilter) { System.err.println("Stack contains bad filter"); } else { ((ComplexTagFilter) predicateStack.peek()).addFilter(filter); } } } } if (predicateStack.isEmpty()) { return null; } else { return predicateStack.pop(); } } 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 TagFilter extractGrafanaFilter(JsonObject element) throws InvalidFilterException { Stack<TagFilter> predicateStack = new Stack<>(); JsonArray array = element.get("filters").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.get(i).getAsJsonObject(); if (!obj.has("value")) { continue; } String val = obj.get("value").getAsString(); if (obj.has("type")) { if (predicateStack.isEmpty()) { System.err.println("Error empty stack"); } else { TagFilter pop = predicateStack.pop(); if (val.equalsIgnoreCase("and")) { ComplexTagFilter andFilter = new ComplexTagFilter(ComplexFilterType.AND); andFilter.addFilter(pop); predicateStack.push(andFilter); } else if (val.equalsIgnoreCase("or")) { ComplexTagFilter orFilter = new ComplexTagFilter(ComplexFilterType.OR); orFilter.addFilter(pop); predicateStack.push(orFilter); } else { System.err.println("Error stack is not empty"); } } } else { SimpleTagFilter filter = MiscUtils.buildSimpleFilter(obj.get("key").getAsString() + obj.get("operator").getAsString() + obj.get("value").getAsString()); logger.log(Level.FINE, () -> "Simple filter:" + filter); if (predicateStack.isEmpty()) { predicateStack.push(filter); } else { if (predicateStack.peek() instanceof SimpleTagFilter) { System.err.println("Stack contains bad filter"); } else { ((ComplexTagFilter) predicateStack.peek()).addFilter(filter); } } } } if (predicateStack.isEmpty()) { return null; } else { return predicateStack.pop(); } } 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);  }