Ejemplo n.º 1
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);
    }
  }
Ejemplo n.º 2
0
 private Integer getResolution(String resolution, Long start, Long end) {
   if (resolution.equals(PARAM_AUTO_RESOLUTION)) {
     if (start != null && end != null) {
       long difference = end - start;
       return MetricQueryParser.getResolution(difference).getResolution();
     } else {
       throw new IllegalArgumentException(
           "if resolution=auto, start and end timestamp "
               + "should be provided to determine resolution");
     }
   } else {
     // if not auto, check if the given resolution matches available resolutions that we support.
     int resolutionInterval = TimeMathParser.resolutionInSeconds(resolution);
     if (!((resolutionInterval == Integer.MAX_VALUE)
         || (resolutionInterval == 3600)
         || (resolutionInterval == 60)
         || (resolutionInterval == 1))) {
       throw new IllegalArgumentException(
           "Resolution interval not supported, only 1 second, 1 minute and "
               + "1 hour resolutions are supported currently");
     }
     return resolutionInterval;
   }
 }