/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2012-present 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/stats/detail/Bucket.h>
namespace folly {
*/
BucketedTimeSeries(size_t numBuckets, Duration duration);
+ /*
+ * Create a new BucketedTimeSeries.
+ *
+ * This constructor is used to reconstruct a timeseries using
+ * previously saved data
+ */
+ BucketedTimeSeries(
+ TimePoint theFirstTime,
+ TimePoint theLatestTime,
+ Duration maxDuration,
+ const std::vector<Bucket>& bucketsList);
+
/*
* Adds the value 'val' at time 'now'
*
/*
* Adds the value 'val' the given number of 'times' at time 'now'
*/
- bool addValue(TimePoint now, const ValueType& val, int64_t times);
+ bool addValue(TimePoint now, const ValueType& val, uint64_t times);
/*
* Adds the value 'total' as the sum of 'nsamples' samples
*/
bool
- addValueAggregated(TimePoint now, const ValueType& total, int64_t nsamples);
+ addValueAggregated(TimePoint now, const ValueType& total, uint64_t nsamples);
/*
* Updates the container to the specified time, doing all the necessary
return firstTime_ > latestTime_;
}
+ /*
+ * Returns time of first update() since clear()/constructor.
+ * Note that the returned value is only meaningful when empty() is false.
+ */
+ TimePoint firstTime() const {
+ return firstTime_;
+ }
+
+ /*
+ * Returns time of last update().
+ * Note that the returned value is only meaningful when empty() is false.
+ */
+ TimePoint latestTime() const {
+ return latestTime_;
+ }
+
+ /*
+ * Returns actual buckets of values
+ */
+ const std::vector<Bucket>& buckets() const {
+ return buckets_;
+ }
+
/*
* Get the amount of time tracked by this timeseries.
*
* Note that you generally should call update() before calling avg(), to
* make sure you are not reading stale data.
*/
- template <typename ReturnType=double>
+ template <typename ReturnType = double>
ReturnType avg() const {
return total_.template avg<ReturnType>();
}
*/
template <typename ReturnType = double, typename Interval = Duration>
ReturnType rate() const {
- return rateHelper<ReturnType, Interval>(total_.sum, elapsed());
+ return rateHelper<ReturnType, Interval>(ReturnType(total_.sum), elapsed());
}
/*
*/
template <typename ReturnType = double, typename Interval = Duration>
ReturnType countRate() const {
- return rateHelper<ReturnType, Interval>(total_.count, elapsed());
+ return rateHelper<ReturnType, Interval>(
+ ReturnType(total_.count), elapsed());
}
/*
ReturnType countRate(TimePoint start, TimePoint end) const {
uint64_t intervalCount = count(start, end);
Duration interval = elapsed(start, end);
- return rateHelper<ReturnType, Interval>(intervalCount, interval);
+ return rateHelper<ReturnType, Interval>(
+ ReturnType(intervalCount), interval);
}
/*
bool addValue(Duration now, const ValueType& val) {
return addValueAggregated(TimePoint(now), val, 1);
}
- bool addValue(Duration now, const ValueType& val, int64_t times) {
- return addValueAggregated(TimePoint(now), val * times, times);
+ bool addValue(Duration now, const ValueType& val, uint64_t times) {
+ return addValueAggregated(TimePoint(now), val * ValueType(times), times);
}
bool
- addValueAggregated(Duration now, const ValueType& total, int64_t nsamples) {
+ addValueAggregated(Duration now, const ValueType& total, uint64_t nsamples) {
return addValueAggregated(TimePoint(now), total, nsamples);
}
size_t update(Duration now) {
TimePoint latestTime_; // time of last update()
Duration duration_; // total duration ("window length") of the time series
- Bucket total_; // sum and count of everything in time series
- std::vector<Bucket> buckets_; // actual buckets of values
+ Bucket total_; // sum and count of everything in time series
+ std::vector<Bucket> buckets_; // actual buckets of values
};
-} // folly
+} // namespace folly