Пример #1
0
  private MetricQueryResult executeQuery(MetricQueryRequest queryRequest) throws Exception {
    if (queryRequest.getMetrics().size() == 0) {
      throw new IllegalArgumentException("Missing metrics parameter in the query");
    }

    Map<String, String> tagsSliceBy = humanToTagNames(transformTagMap(queryRequest.getTags()));

    MetricQueryRequest.TimeRange timeRange = queryRequest.getTimeRange();

    MetricDataQuery query =
        new MetricDataQuery(
            timeRange.getStart(),
            timeRange.getEnd(),
            timeRange.getResolutionInSeconds(),
            timeRange.getCount(),
            toMetrics(queryRequest.getMetrics()),
            tagsSliceBy,
            transformGroupByTags(queryRequest.getGroupBy()),
            timeRange.getInterpolate());
    Collection<MetricTimeSeries> queryResult = metricStore.query(query);

    long endTime = timeRange.getEnd();
    if (timeRange.getResolutionInSeconds() == Integer.MAX_VALUE && endTime == 0) {
      // for aggregate query, we set the end time to be query time (current time)
      endTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    }

    return decorate(queryResult, timeRange.getStart(), endTime, timeRange.getResolutionInSeconds());
  }
Пример #2
0
  private void setTimeRangeInQueryRequest(
      MetricQueryRequest request, Map<String, List<String>> queryTimeParams) {
    Long start =
        queryTimeParams.containsKey(PARAM_START_TIME)
            ? TimeMathParser.parseTimeInSeconds(queryTimeParams.get(PARAM_START_TIME).get(0))
            : null;
    Long end =
        queryTimeParams.containsKey(PARAM_END_TIME)
            ? TimeMathParser.parseTimeInSeconds(queryTimeParams.get(PARAM_END_TIME).get(0))
            : null;
    Integer count = null;

    boolean aggregate =
        queryTimeParams.containsKey(PARAM_AGGREGATE)
                && queryTimeParams.get(PARAM_AGGREGATE).get(0).equals("true")
            || ((start == null) && (end == null));

    Integer resolution =
        queryTimeParams.containsKey(PARAM_RESOLUTION)
            ? getResolution(queryTimeParams.get(PARAM_RESOLUTION).get(0), start, end)
            : 1;

    Interpolator interpolator = null;
    if (queryTimeParams.containsKey(PARAM_INTERPOLATE)) {
      long timeLimit =
          queryTimeParams.containsKey(PARAM_MAX_INTERPOLATE_GAP)
              ? Long.parseLong(queryTimeParams.get(PARAM_MAX_INTERPOLATE_GAP).get(0))
              : Long.MAX_VALUE;
      interpolator = getInterpolator(queryTimeParams.get(PARAM_INTERPOLATE).get(0), timeLimit);
    }

    if (queryTimeParams.containsKey(PARAM_COUNT)) {
      count = Integer.valueOf(queryTimeParams.get(PARAM_COUNT).get(0));
      if (start == null && end != null) {
        start = end - count * resolution;
      } else if (start != null && end == null) {
        end = start + count * resolution;
      }
    } else if (start != null && end != null) {
      count =
          (int)
              (((end / resolution * resolution) - (start / resolution * resolution)) / resolution
                  + 1);
    } else if (!aggregate) {
      throw new IllegalArgumentException(
          "At least two of count/start/end parameters " + "are required for time-range queries ");
    }

    if (aggregate) {
      request.setTimeRange(0L, 0L, 1, Integer.MAX_VALUE, null);
    } else {
      request.setTimeRange(start, end, count, resolution, interpolator);
    }
  }