Ejemplo n.º 1
0
  public Iterable<MeasurementDataNumericHighLowComposite> findDataForResource(
      int scheduleId, long beginTime, long endTime, int numberOfBuckets) {
    Stopwatch stopwatch = new Stopwatch().start();
    try {
      DateTime begin = new DateTime(beginTime);

      if (dateTimeService.isInRawDataRange(begin)) {
        Iterable<RawNumericMetric> metrics = dao.findRawMetrics(scheduleId, beginTime, endTime);
        return createRawComposites(metrics, beginTime, endTime, numberOfBuckets);
      }

      List<AggregateNumericMetric> metrics = null;
      if (dateTimeService.isIn1HourDataRange(begin)) {
        metrics = dao.findAggregateMetrics(scheduleId, Bucket.ONE_HOUR, beginTime, endTime);
        return createComposites(metrics, beginTime, endTime, numberOfBuckets);
      } else if (dateTimeService.isIn6HourDataRange(begin)) {
        metrics = dao.findAggregateMetrics(scheduleId, Bucket.SIX_HOUR, beginTime, endTime);
        return createComposites(metrics, beginTime, endTime, numberOfBuckets);
      } else if (dateTimeService.isIn24HourDataRange(begin)) {
        metrics = dao.findAggregateMetrics(scheduleId, Bucket.TWENTY_FOUR_HOUR, beginTime, endTime);
        return createComposites(metrics, beginTime, endTime, numberOfBuckets);
      } else {
        throw new IllegalArgumentException(
            "beginTime[" + beginTime + "] is outside the accepted range.");
      }
    } finally {
      stopwatch.stop();
      if (log.isDebugEnabled()) {
        log.debug(
            "Finished calculating resource summary aggregate in "
                + stopwatch.elapsed(TimeUnit.MILLISECONDS)
                + " ms");
      }
    }
  }
Ejemplo n.º 2
0
 protected Bucket getBucket(DateTime begin) {
   Bucket bucket;
   if (dateTimeService.isIn1HourDataRange(begin)) {
     bucket = Bucket.ONE_HOUR;
   } else if (dateTimeService.isIn6HourDataRange(begin)) {
     bucket = Bucket.SIX_HOUR;
   } else if (dateTimeService.isIn24HourDataRange(begin)) {
     bucket = Bucket.TWENTY_FOUR_HOUR;
   } else {
     throw new IllegalArgumentException(
         "beginTime[" + begin.getMillis() + "] is outside the accepted range.");
   }
   return bucket;
 }
Ejemplo n.º 3
0
  public AggregateNumericMetric getSummaryAggregate(
      List<Integer> scheduleIds, long beginTime, long endTime) {
    Stopwatch stopwatch = new Stopwatch().start();
    try {
      DateTime begin = new DateTime(beginTime);

      if (dateTimeService.isInRawDataRange(new DateTime(beginTime))) {
        Iterable<RawNumericMetric> metrics = dao.findRawMetrics(scheduleIds, beginTime, endTime);
        return calculateAggregatedRaw(metrics, beginTime);
      }
      Bucket bucket = getBucket(begin);
      List<AggregateNumericMetric> metrics = loadMetrics(scheduleIds, beginTime, endTime, bucket);

      return calculateAggregate(metrics, beginTime, bucket);
    } finally {
      stopwatch.stop();
      if (log.isDebugEnabled()) {
        log.debug(
            "Finished calculating group summary aggregate for [scheduleIds: "
                + scheduleIds
                + ", beginTime: "
                + beginTime
                + ", endTime: "
                + endTime
                + "] in "
                + stopwatch.elapsed(TimeUnit.MILLISECONDS)
                + " ms");
      }
    }
  }
Ejemplo n.º 4
0
  public ListenableFuture<AggregateNumericMetric> getSummaryAggregateAsync(
      int scheduleId, long beginTime, long endTime) {
    long start = System.currentTimeMillis();
    try {
      if (log.isDebugEnabled()) {
        log.debug(
            "Calculating resource summary aggregate (async) for [scheduleId: "
                + scheduleId
                + ", beginTime: "
                + beginTime
                + ", endTime: "
                + endTime
                + "]");
      }
      DateTime begin = new DateTime(beginTime);
      StorageResultSetFuture queryFuture;

      if (dateTimeService.isInRawDataRange(begin)) {
        queryFuture = dao.findRawMetricsAsync(scheduleId, beginTime, endTime);
        return Futures.transform(queryFuture, new ComputeRawAggregate(beginTime));
      }
      Bucket bucket = getBucket(begin);
      queryFuture = dao.findAggregateMetricsAsync(scheduleId, bucket, beginTime, endTime);

      return Futures.transform(queryFuture, new ComputeAggregate(beginTime, bucket));
    } finally {
      long end = System.currentTimeMillis();
      if (log.isDebugEnabled()) {
        log.debug(
            "Finished calculating resource summary aggregate (async) in " + (end - start) + " ms");
      }
    }
  }
Ejemplo n.º 5
0
  public List<MeasurementDataNumericHighLowComposite> findDataForGroup(
      List<Integer> scheduleIds, long beginTime, long endTime, int numberOfBuckets) {
    if (log.isDebugEnabled()) {
      log.debug(
          "Querying for metric data using parameters [scheduleIds: "
              + scheduleIds
              + ", beingTime: "
              + beginTime
              + ", endTime: "
              + endTime
              + ", numberOfBuckets: "
              + numberOfBuckets
              + "]");
    }

    DateTime begin = new DateTime(beginTime);
    if (dateTimeService.isInRawDataRange(begin)) {
      Iterable<RawNumericMetric> metrics = dao.findRawMetrics(scheduleIds, beginTime, endTime);
      return createRawComposites(metrics, beginTime, endTime, numberOfBuckets);
    }
    Bucket bucket = getBucket(begin);
    List<AggregateNumericMetric> metrics = loadMetrics(scheduleIds, beginTime, endTime, bucket);

    return createComposites(metrics, beginTime, endTime, numberOfBuckets);
  }
Ejemplo n.º 6
0
  public void addNumericData(
      final Set<MeasurementDataNumeric> dataSet, final RawDataInsertedCallback callback) {
    if (log.isDebugEnabled()) {
      log.debug("Inserting " + dataSet.size() + " raw metrics");
    }
    final Stopwatch stopwatch = new Stopwatch().start();
    final AtomicInteger remainingInserts = new AtomicInteger(dataSet.size());

    for (final MeasurementDataNumeric data : dataSet) {
      DateTime collectionTimeSlice =
          dateTimeService.getTimeSlice(
              new DateTime(data.getTimestamp()), configuration.getRawTimeSliceDuration());
      Days days = Days.daysBetween(collectionTimeSlice, dateTimeService.now());

      if (days.isGreaterThan(rawDataAgeLimit)) {
        log.info(
            data
                + " is older than the raw data age limit of "
                + rawDataAgeLimit.getDays()
                + " days. It will not be stored.");
      } else {
        StorageResultSetFuture rawFuture = dao.insertRawData(data);
        StorageResultSetFuture indexFuture =
            dao.updateIndex(IndexBucket.RAW, collectionTimeSlice.getMillis(), data.getScheduleId());
        ListenableFuture<List<ResultSet>> insertsFuture =
            Futures.successfulAsList(rawFuture, indexFuture);
        Futures.addCallback(
            insertsFuture,
            new FutureCallback<List<ResultSet>>() {
              @Override
              public void onSuccess(List<ResultSet> result) {
                callback.onSuccess(data);
                if (remainingInserts.decrementAndGet() == 0) {
                  stopwatch.stop();
                  if (log.isDebugEnabled()) {
                    log.debug(
                        "Finished inserting "
                            + dataSet.size()
                            + " raw metrics in "
                            + stopwatch.elapsed(TimeUnit.MILLISECONDS)
                            + " ms");
                  }
                  callback.onFinish();
                }
              }

              @Override
              public void onFailure(Throwable t) {
                if (log.isDebugEnabled()) {
                  log.debug(
                      "An error occurred while inserting raw data", ThrowableUtil.getRootCause(t));
                } else {
                  log.warn(
                      "An error occurred while inserting raw data: "
                          + ThrowableUtil.getRootMessage(t));
                }
                callback.onFailure(t);
              }
            },
            tasks);
      }
    }
  }