Example #1
0
  public RollupTask parseRollupTask(JsonObject rollupTask, String context)
      throws BeanValidationException, QueryException {
    RollupTask task = m_gson.fromJson(rollupTask.getAsJsonObject(), RollupTask.class);

    validateObject(task);

    JsonArray rollups = rollupTask.getAsJsonObject().getAsJsonArray("rollups");
    if (rollups != null) {
      for (int j = 0; j < rollups.size(); j++) {
        JsonObject rollupObject = rollups.get(j).getAsJsonObject();
        Rollup rollup = m_gson.fromJson(rollupObject, Rollup.class);

        context = context + "rollup[" + j + "]";
        validateObject(rollup, context);

        JsonObject queryObject = rollupObject.getAsJsonObject("query");
        List<QueryMetric> queries = parseQueryMetric(queryObject, context);

        for (int k = 0; k < queries.size(); k++) {
          QueryMetric query = queries.get(k);
          context += ".query[" + k + "]";
          validateHasRangeAggregator(query, context);

          // Add aggregators needed for rollups
          SaveAsAggregator saveAsAggregator =
              (SaveAsAggregator) m_aggregatorFactory.createAggregator("save_as");
          saveAsAggregator.setMetricName(rollup.getSaveAs());

          TrimAggregator trimAggregator =
              (TrimAggregator) m_aggregatorFactory.createAggregator("trim");
          trimAggregator.setTrim(TrimAggregator.Trim.LAST);

          query.addAggregator(saveAsAggregator);
          query.addAggregator(trimAggregator);
        }

        rollup.addQueries(queries);
        task.addRollup(rollup);
      }
    }

    return task;
  }
Example #2
0
  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);
    }
  }