コード例 #1
0
ファイル: QueryParser.java プロジェクト: rdettai/kairosdb
  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);
    }
  }
コード例 #2
0
ファイル: QueryParser.java プロジェクト: rdettai/kairosdb
  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);
    }
  }
コード例 #3
0
ファイル: QueryParser.java プロジェクト: rdettai/kairosdb
  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);
    }
  }
コード例 #4
0
ファイル: QueryParser.java プロジェクト: rdettai/kairosdb
  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;
  }
コード例 #5
0
ファイル: QueryParser.java プロジェクト: rdettai/kairosdb
  private void validateHasRangeAggregator(QueryMetric query, String context)
      throws BeanValidationException {
    boolean hasRangeAggregator = false;
    for (Aggregator aggregator : query.getAggregators()) {
      if (aggregator instanceof RangeAggregator) {
        hasRangeAggregator = true;
        break;
      }
    }

    if (!hasRangeAggregator) {
      throw new BeanValidationException(
          new SimpleConstraintViolation(
              "aggregator", "At least one aggregator must be a range aggregator"),
          context);
    }
  }
コード例 #6
0
ファイル: QueryParser.java プロジェクト: rdettai/kairosdb
  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);
  }