/** * Returns the JSON string built by the builder. This is the JSON that can be used by the client * add metrics. * * @return JSON * @throws IOException if metrics cannot be converted to JSON */ public String build() throws IOException { for (Metric metric : metrics) { // verify that there is at least one tag for each metric checkState(metric.getTags().size() > 0, metric.getName() + " must contain at least one tag."); } return mapper.toJson(metrics); }
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); }
@Override @Secured public Metric addMetric(Test test, Metric metric) throws ServiceException { Test freshTest = testDAO.get(test.getId()); if (freshTest.getMetrics() == null) { freshTest.setMetrics(new ArrayList<>()); } if (metric.getId() != null) { // associating an existing metric with the test Metric freshMetric = metricDAO.get(metric.getId()); if (freshMetric == null) { throw new ServiceException("serviceException.metricNotFound", metric.getName().toString()); } if (freshMetric.getTests() == null) { freshMetric.setTests(new ArrayList<>()); } for (Test testForMetric : freshMetric.getTests()) { if (!testForMetric.getGroupId().equals(freshTest.getGroupId())) { throw new ServiceException("serviceException.metricSharingOnlyInGroup"); } if (testForMetric.getId().equals(freshTest.getId())) { throw new ServiceException( "serviceException.metricAlreadyExists", freshTest.getUid(), freshMetric.getName()); } } freshMetric.getTests().add(freshTest); freshTest.getMetrics().add(freshMetric); freshMetric = metricDAO.update(freshMetric); testDAO.update(freshTest); return freshMetric; } else { // creating a new metric object if (metric.getName() == null) { throw new IllegalArgumentException("Metric name is mandatory"); } // metric name needs to be unique in the metric space of a certain groupId // does it exist in a test with same group id (including the target test) ? List<Metric> existingMetricsForGroup = metricDAO.getMetricByNameAndGroup(metric.getName(), freshTest.getGroupId()); for (Metric existingMetric : existingMetricsForGroup) { if (existingMetric.getName().equals(metric.getName())) { Metric freshMetric = metricDAO.get(existingMetric.getId()); if (freshMetric.getTests().stream().anyMatch(t -> t.getId().equals(freshTest.getId()))) { throw new ServiceException( "serviceException.metricAlreadyExists", freshTest.getUid(), freshMetric.getName()); } } } metric.setTests(Arrays.asList(freshTest)); Metric freshMetric = metricDAO.create(metric); freshTest.getMetrics().add(freshMetric); testDAO.update(freshTest); return freshMetric; } }