private void parseGroupBy(String context, QueryMetric queryMetric, JsonArray groupBys) throws QueryException, BeanValidationException { for (int J = 0; J < groupBys.size(); J++) { String groupContext = "group_by[" + J + "]"; JsonObject jsGroupBy = groupBys.get(J).getAsJsonObject(); JsonElement nameElement = jsGroupBy.get("name"); if (nameElement == null || nameElement.getAsString().isEmpty()) throw new BeanValidationException( new SimpleConstraintViolation(groupContext, "must have a name"), context); String name = nameElement.getAsString(); GroupBy groupBy = m_groupByFactory.createGroupBy(name); if (groupBy == null) throw new BeanValidationException( new SimpleConstraintViolation(groupContext + "." + name, "invalid group_by name"), context); deserializeProperties(context + "." + groupContext, jsGroupBy, name, groupBy); validateObject(groupBy, context + "." + groupContext); groupBy.setStartDate(queryMetric.getStartTime()); queryMetric.addGroupBy(groupBy); } }
private void parsePlugins(String context, QueryMetric queryMetric, JsonArray plugins) throws BeanValidationException, QueryException { for (int I = 0; I < plugins.size(); I++) { JsonObject pluginJson = plugins.get(I).getAsJsonObject(); JsonElement name = pluginJson.get("name"); if (name == null || name.getAsString().isEmpty()) throw new BeanValidationException( new SimpleConstraintViolation("plugins[" + I + "]", "must have a name"), context); String pluginContext = context + ".plugins[" + I + "]"; String pluginName = name.getAsString(); QueryPlugin plugin = m_pluginFactory.createQueryPlugin(pluginName); if (plugin == null) throw new BeanValidationException( new SimpleConstraintViolation(pluginName, "invalid query plugin name"), pluginContext); deserializeProperties(pluginContext, pluginJson, pluginName, plugin); validateObject(plugin, pluginContext); queryMetric.addPlugin(plugin); } }
public DateTimeZone deserialize( JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) return null; String tz = json.getAsString(); if (tz.isEmpty()) // defaults to UTC return DateTimeZone.UTC; DateTimeZone timeZone; try { // check if time zone is valid timeZone = DateTimeZone.forID(tz); } catch (IllegalArgumentException e) { throw new ContextualJsonSyntaxException( tz, "is not a valid time zone, must be one of " + DateTimeZone.getAvailableIDs()); } return timeZone; }
private void parseAggregators( String context, QueryMetric queryMetric, JsonArray aggregators, DateTimeZone timeZone) throws QueryException, BeanValidationException { for (int J = 0; J < aggregators.size(); J++) { JsonObject jsAggregator = aggregators.get(J).getAsJsonObject(); JsonElement name = jsAggregator.get("name"); if (name == null || name.getAsString().isEmpty()) throw new BeanValidationException( new SimpleConstraintViolation("aggregators[" + J + "]", "must have a name"), context); String aggContext = context + ".aggregators[" + J + "]"; String aggName = name.getAsString(); Aggregator aggregator = m_aggregatorFactory.createAggregator(aggName); if (aggregator == null) throw new BeanValidationException( new SimpleConstraintViolation(aggName, "invalid aggregator name"), aggContext); // If it is a range aggregator we will default the start time to // the start of the query. if (aggregator instanceof RangeAggregator) { RangeAggregator ra = (RangeAggregator) aggregator; ra.setStartTime(queryMetric.getStartTime()); } if (aggregator instanceof TimezoneAware) { TimezoneAware ta = (TimezoneAware) aggregator; ta.setTimeZone(timeZone); } if (aggregator instanceof GroupByAware) { GroupByAware groupByAware = (GroupByAware) aggregator; groupByAware.setGroupBys(queryMetric.getGroupBys()); } deserializeProperties(aggContext, jsAggregator, aggName, aggregator); validateObject(aggregator, aggContext); queryMetric.addAggregator(aggregator); } }
@Override public Metric deserialize( JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject jsonObject = jsonElement.getAsJsonObject(); String name = null; if (jsonObject.get("name") != null) name = jsonObject.get("name").getAsString(); boolean exclude_tags = false; if (jsonObject.get("exclude_tags") != null) exclude_tags = jsonObject.get("exclude_tags").getAsBoolean(); TreeMultimap<String, String> tags = TreeMultimap.create(); JsonElement jeTags = jsonObject.get("tags"); if (jeTags != null) { JsonObject joTags = jeTags.getAsJsonObject(); int count = 0; for (Map.Entry<String, JsonElement> tagEntry : joTags.entrySet()) { String context = "tags[" + count + "]"; if (tagEntry.getKey().isEmpty()) throw new ContextualJsonSyntaxException(context, "name must not be empty"); if (tagEntry.getValue().isJsonArray()) { for (JsonElement element : tagEntry.getValue().getAsJsonArray()) { if (element.isJsonNull() || element.getAsString().isEmpty()) throw new ContextualJsonSyntaxException( context + "." + tagEntry.getKey(), "value must not be null or empty"); tags.put(tagEntry.getKey(), element.getAsString()); } } else { if (tagEntry.getValue().isJsonNull() || tagEntry.getValue().getAsString().isEmpty()) throw new ContextualJsonSyntaxException( context + "." + tagEntry.getKey(), "value must not be null or empty"); tags.put(tagEntry.getKey(), tagEntry.getValue().getAsString()); } count++; } } Metric ret = new Metric(name, exclude_tags, tags); JsonElement limit = jsonObject.get("limit"); if (limit != null) ret.setLimit(limit.getAsInt()); return (ret); }
public TimeUnit deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String unit = json.getAsString(); TimeUnit tu; try { tu = TimeUnit.from(unit); } catch (IllegalArgumentException e) { throw new ContextualJsonSyntaxException( unit, "is not a valid time unit, must be one of " + TimeUnit.toValueNames()); } return tu; }
private List<QueryMetric> parseQueryMetric(JsonObject obj, String contextPrefix) throws QueryException, BeanValidationException { List<QueryMetric> ret = new ArrayList<QueryMetric>(); Query query; try { query = m_gson.fromJson(obj, Query.class); validateObject(query); } catch (ContextualJsonSyntaxException e) { throw new BeanValidationException( new SimpleConstraintViolation(e.getContext(), e.getMessage()), "query"); } JsonArray metricsArray = obj.getAsJsonArray("metrics"); if (metricsArray == null) { throw new BeanValidationException( new SimpleConstraintViolation("metric[]", "must have a size of at least 1"), contextPrefix + "query"); } for (int I = 0; I < metricsArray.size(); I++) { String context = (!contextPrefix.isEmpty() ? contextPrefix + "." : contextPrefix) + "query.metric[" + I + "]"; try { Metric metric = m_gson.fromJson(metricsArray.get(I), Metric.class); validateObject(metric, context); long startTime = getStartTime(query, context); QueryMetric queryMetric = new QueryMetric(startTime, query.getCacheTime(), metric.getName()); queryMetric.setExcludeTags(metric.isExcludeTags()); queryMetric.setLimit(metric.getLimit()); long endTime = getEndTime(query); if (endTime > -1) queryMetric.setEndTime(endTime); if (queryMetric.getEndTime() < startTime) throw new BeanValidationException( new SimpleConstraintViolation("end_time", "must be greater than the start time"), context); queryMetric.setCacheString(query.getCacheString() + metric.getCacheString()); JsonObject jsMetric = metricsArray.get(I).getAsJsonObject(); JsonElement group_by = jsMetric.get("group_by"); if (group_by != null) { JsonArray groupBys = group_by.getAsJsonArray(); parseGroupBy(context, queryMetric, groupBys); } JsonElement aggregators = jsMetric.get("aggregators"); if (aggregators != null) { JsonArray asJsonArray = aggregators.getAsJsonArray(); if (asJsonArray.size() > 0) parseAggregators(context, queryMetric, asJsonArray, query.getTimeZone()); } JsonElement plugins = jsMetric.get("plugins"); if (plugins != null) { JsonArray pluginArray = plugins.getAsJsonArray(); if (pluginArray.size() > 0) parsePlugins(context, queryMetric, pluginArray); } JsonElement order = jsMetric.get("order"); if (order != null) queryMetric.setOrder(Order.fromString(order.getAsString(), context)); queryMetric.setTags(metric.getTags()); ret.add(queryMetric); } catch (ContextualJsonSyntaxException e) { throw new BeanValidationException( new SimpleConstraintViolation(e.getContext(), e.getMessage()), context); } } return (ret); }