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"); } } }
public AggregateNumericMetric getSummaryAggregate(int scheduleId, long beginTime, long endTime) { Stopwatch stopwatch = new Stopwatch().start(); try { DateTime begin = new DateTime(beginTime); if (dateTimeService.isInRawDataRange(begin)) { Iterable<RawNumericMetric> metrics = dao.findRawMetrics(scheduleId, beginTime, endTime); return calculateAggregatedRaw(metrics, beginTime); } Bucket bucket = getBucket(begin); List<AggregateNumericMetric> metrics = dao.findAggregateMetrics(scheduleId, bucket, beginTime, endTime); return calculateAggregate(metrics, beginTime, bucket); } finally { stopwatch.stop(); if (log.isDebugEnabled()) { log.debug( "Finished calculating resource summary aggregate for [scheduleId: " + scheduleId + ", beginTime: " + beginTime + ", endTime: " + endTime + "] in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms"); } } }
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"); } } }
private List<AggregateNumericMetric> loadMetrics( List<Integer> scheduleIds, long begin, long end, Bucket bucket) { List<StorageResultSetFuture> futures = new ArrayList<StorageResultSetFuture>(scheduleIds.size()); for (Integer scheduleId : scheduleIds) { futures.add(dao.findAggregateMetricsAsync(scheduleId, bucket, begin, end)); } ListenableFuture<List<ResultSet>> resultSetsFuture = Futures.successfulAsList(futures); try { List<ResultSet> resultSets = resultSetsFuture.get(); AggregateNumericMetricMapper mapper = new AggregateNumericMetricMapper(); List<AggregateNumericMetric> metrics = new ArrayList<AggregateNumericMetric>(); for (ResultSet resultSet : resultSets) { metrics.addAll(mapper.mapAll(resultSet)); } return metrics; } catch (Exception e) { log.warn( "There was an error while fetching " + bucket + " data for {scheduleIds: " + scheduleIds + ", beginTime: " + begin + ", endTime: " + end + "}", e); return Collections.emptyList(); } }
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); }
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); } } }
public RawNumericMetric findLatestValueForResource(int scheduleId) { log.debug("Querying for most recent raw metrics for [scheduleId: " + scheduleId + "]"); return dao.findLatestRawMetric(scheduleId); }