/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <chrono>
#include <vector>
-#include "folly/detail/Stats.h"
+#include <folly/detail/Stats.h>
namespace folly {
* be discarded and new data will go into the newly opened bucket. Internally,
* it uses a circular array of buckets that it reuses as time advances.
*
- * The class assumes that time advances forward -- you can't retroactively add
- * values for events in the past -- the 'now' argument is provided for better
- * efficiency and ease of unittesting.
- *
+ * This class assumes that time advances forwards. The window of time tracked
+ * by the timeseries will advance forwards whenever a more recent timestamp is
+ * passed to addValue(). While it is possible to pass old time values to
+ * addValue(), this will never move the time window backwards. If the old time
+ * value falls outside the tracked window of time, the data point will be
+ * ignored.
*
* This class is not thread-safe -- use your own synchronization!
*/
/*
* Adds the value 'val' at time 'now'
*
- * This function expects time to always move forwards: it cannot be used to
- * add historical data points that have occurred in the past. If now is
- * older than the another timestamp that has already been passed to
- * addValue() or update(), now will be ignored and the latest timestamp will
- * be used.
+ * This function expects time to generally move forwards. The window of time
+ * tracked by this time series will move forwards with time. If 'now' is
+ * more recent than any time previously seen, addValue() will automatically
+ * call update(now) to advance the time window tracked by this data
+ * structure.
+ *
+ * Values in the recent past may be added to the data structure by passing in
+ * a slightly older value of 'now', as long as this time point still falls
+ * within the tracked duration. If 'now' is older than the tracked duration
+ * of time, the data point value will be ignored, and addValue() will return
+ * false without doing anything else.
+ *
+ * Returns true on success, or false if now was older than the tracked time
+ * window.
*/
- void addValue(TimeType now, const ValueType& val);
+ bool addValue(TimeType now, const ValueType& val);
/*
* Adds the value 'val' the given number of 'times' at time 'now'
*/
- void addValue(TimeType now, const ValueType& val, int64_t times);
+ bool addValue(TimeType now, const ValueType& val, int64_t times);
/*
* Adds the value 'sum' as the sum of 'nsamples' samples
*/
- void addValueAggregated(TimeType now, const ValueType& sum, int64_t nsamples);
+ bool addValueAggregated(TimeType now, const ValueType& sum, int64_t nsamples);
/*
* Updates the container to the specified time, doing all the necessary
/*
* Get the latest time that has ever been passed to update() or addValue().
+ *
+ * If no data has ever been added to this timeseries, 0 will be returned.
*/
TimeType getLatestTime() const {
return latestTime_;
}
+ /*
+ * Get the time of the earliest data point stored in this timeseries.
+ *
+ * If no data has ever been added to this timeseries, 0 will be returned.
+ *
+ * If isAllTime() is true, this is simply the time when the first data point
+ * was recorded.
+ *
+ * For non-all-time data, the timestamp reflects the first data point still
+ * remembered. As new data points are added, old data will be expired.
+ * getEarliestTime() returns the timestamp of the oldest bucket still present
+ * in the timeseries. This will never be older than (getLatestTime() -
+ * duration()).
+ */
+ TimeType getEarliestTime() const;
+
/*
* Return the number of buckets.
*/
*/
TimeType elapsed() const;
+ /*
+ * Get the amount of time tracked by this timeseries, between the specified
+ * start and end times.
+ *
+ * If the timeseries contains data for the entire time range specified, this
+ * simply returns (end - start). However, if start is earlier than
+ * getEarliestTime(), this returns (end - getEarliestTime()).
+ */
+ TimeType elapsed(TimeType start, TimeType end) const;
+
/*
* Return the sum of all the data points currently tracked by this
* BucketedTimeSeries.
*
* Note that data outside of the timeseries duration will no longer be
* available for use in the estimation. Specifying a start time earlier than
- * (getLatestTime() - elapsed()) will not have much effect, since only data
- * points after that point in time will be counted.
+ * getEarliestTime() will not have much effect, since only data points after
+ * that point in time will be counted.
*
* Note that the value returned is an estimate, and may not be precise.
*/
template <typename ReturnType=double, typename Interval=TimeType>
ReturnType rate(TimeType start, TimeType end) const {
ValueType intervalSum = sum(start, end);
- return rateHelper<ReturnType, Interval>(intervalSum, end - start);
+ TimeType interval = elapsed(start, end);
+ return rateHelper<ReturnType, Interval>(intervalSum, interval);
}
/*
template <typename ReturnType=double, typename Interval=TimeType>
ReturnType countRate(TimeType start, TimeType end) const {
uint64_t intervalCount = count(start, end);
- return rateHelper<ReturnType, Interval>(intervalCount, end - start);
+ TimeType interval = elapsed(start, end);
+ return rateHelper<ReturnType, Interval>(intervalCount, interval);
}
/*
private:
template <typename ReturnType=double, typename Interval=TimeType>
- ReturnType rateHelper(ReturnType numerator, TimeType elapsed) const {
- if (elapsed == TimeType(0)) {
- return 0;
- }
-
- // Use std::chrono::duration_cast to convert between the native
- // duration and the desired interval. However, convert the rates,
- // rather than just converting the elapsed duration. Converting the
- // elapsed time first may collapse it down to 0 if the elapsed interval
- // is less than the desired interval, which will incorrectly result in
- // an infinite rate.
- typedef std::chrono::duration<
- ReturnType, std::ratio<TimeType::period::den,
- TimeType::period::num>> NativeRate;
- typedef std::chrono::duration<
- ReturnType, std::ratio<Interval::period::den,
- Interval::period::num>> DesiredRate;
-
- NativeRate native(numerator / elapsed.count());
- DesiredRate desired = std::chrono::duration_cast<DesiredRate>(native);
- return desired.count();
+ ReturnType rateHelper(ReturnType numerator, TimeType elapsedTime) const {
+ return detail::rateHelper<ReturnType, TimeType, Interval>(numerator,
+ elapsedTime);
}
+ TimeType getEarliestTimeNonEmpty() const;
+ size_t updateBuckets(TimeType now);
+
ValueType rangeAdjust(TimeType bucketStart, TimeType nextBucketStart,
TimeType start, TimeType end,
ValueType input) const;