update stats APIs to use TimePoint vs Duration correctly
[folly.git] / folly / stats / BucketedTimeSeries.h
index c64053a8ce238c6bb5c4d727fb7843ebbbbf070b..3680acb209c92def237c38f2191102db38173c41 100644 (file)
@@ -67,10 +67,6 @@ class BucketedTimeSeries {
   using Clock = CT;
   using Duration = typename Clock::duration;
   using TimePoint = typename Clock::time_point;
-  // The legacy TimeType.  The older code used this instead of Duration and
-  // TimePoint.  This will eventually be removed as the code is transitioned to
-  // Duration and TimePoint.
-  using TimeType = typename Clock::duration;
   using Bucket = detail::Bucket<ValueType>;
 
   /*
@@ -103,17 +99,18 @@ class BucketedTimeSeries {
    * Returns true on success, or false if now was older than the tracked time
    * window.
    */
-  bool addValue(TimeType now, const ValueType& val);
+  bool addValue(TimePoint now, const ValueType& val);
 
   /*
    * Adds the value 'val' the given number of 'times' at time 'now'
    */
-  bool addValue(TimeType now, const ValueType& val, int64_t times);
+  bool addValue(TimePoint now, const ValueType& val, int64_t times);
 
   /*
-   * Adds the value 'sum' as the sum of 'nsamples' samples
+   * Adds the value 'total' as the sum of 'nsamples' samples
    */
-  bool addValueAggregated(TimeType now, const ValueType& sum, int64_t nsamples);
+  bool
+  addValueAggregated(TimePoint now, const ValueType& total, int64_t nsamples);
 
   /*
    * Updates the container to the specified time, doing all the necessary
@@ -126,7 +123,7 @@ class BucketedTimeSeries {
    *
    * Returns the current bucket index after the update.
    */
-  size_t update(TimeType now);
+  size_t update(TimePoint now);
 
   /*
    * Reset the timeseries to an empty state,
@@ -139,7 +136,7 @@ class BucketedTimeSeries {
    *
    * If no data has ever been added to this timeseries, 0 will be returned.
    */
-  TimeType getLatestTime() const {
+  TimePoint getLatestTime() const {
     return latestTime_;
   }
 
@@ -157,7 +154,7 @@ class BucketedTimeSeries {
    * in the timeseries.  This will never be older than (getLatestTime() -
    * duration()).
    */
-  TimeType getEarliestTime() const;
+  TimePoint getEarliestTime() const;
 
   /*
    * Return the number of buckets.
@@ -170,7 +167,7 @@ class BucketedTimeSeries {
    * Return the maximum duration of data that can be tracked by this
    * BucketedTimeSeries.
    */
-  TimeType duration() const {
+  Duration duration() const {
     return duration_;
   }
 
@@ -179,7 +176,7 @@ class BucketedTimeSeries {
    * ever rolling over into new buckets.
    */
   bool isAllTime() const {
-    return (duration_ == TimeType(0));
+    return (duration_ == Duration(0));
   }
 
   /*
@@ -218,7 +215,7 @@ class BucketedTimeSeries {
    * Note that you generally should call update() before calling elapsed(), to
    * make sure you are not reading stale data.
    */
-  TimeType elapsed() const;
+  Duration elapsed() const;
 
   /*
    * Get the amount of time tracked by this timeseries, between the specified
@@ -228,7 +225,7 @@ class BucketedTimeSeries {
    * simply returns (end - start).  However, if start is earlier than
    * getEarliestTime(), this returns (end - getEarliestTime()).
    */
-  TimeType elapsed(TimeType start, TimeType end) const;
+  Duration elapsed(TimePoint start, TimePoint end) const;
 
   /*
    * Return the sum of all the data points currently tracked by this
@@ -272,7 +269,7 @@ class BucketedTimeSeries {
    * Note that you generally should call update() before calling rate(), to
    * make sure you are not reading stale data.
    */
-  template <typename ReturnType=double, typename Interval=TimeType>
+  template <typename ReturnType = double, typename Interval = Duration>
   ReturnType rate() const {
     return rateHelper<ReturnType, Interval>(total_.sum, elapsed());
   }
@@ -289,7 +286,7 @@ class BucketedTimeSeries {
    * Note that you generally should call update() before calling countRate(),
    * to make sure you are not reading stale data.
    */
-  template <typename ReturnType=double, typename Interval=TimeType>
+  template <typename ReturnType = double, typename Interval = Duration>
   ReturnType countRate() const {
     return rateHelper<ReturnType, Interval>(total_.count, elapsed());
   }
@@ -308,36 +305,36 @@ class BucketedTimeSeries {
    *
    * Note that the value returned is an estimate, and may not be precise.
    */
-  ValueType sum(TimeType start, TimeType end) const;
+  ValueType sum(TimePoint start, TimePoint end) const;
 
   /*
    * Estimate the number of data points that occurred in the specified time
    * period.
    *
-   * The same caveats documented in the sum(TimeType start, TimeType end)
+   * The same caveats documented in the sum(TimePoint start, TimePoint end)
    * comments apply here as well.
    */
-  uint64_t count(TimeType start, TimeType end) const;
+  uint64_t count(TimePoint start, TimePoint end) const;
 
   /*
    * Estimate the average value during the specified time period.
    *
-   * The same caveats documented in the sum(TimeType start, TimeType end)
+   * The same caveats documented in the sum(TimePoint start, TimePoint end)
    * comments apply here as well.
    */
-  template <typename ReturnType=double>
-  ReturnType avg(TimeType start, TimeType end) const;
+  template <typename ReturnType = double>
+  ReturnType avg(TimePoint start, TimePoint end) const;
 
   /*
    * Estimate the rate during the specified time period.
    *
-   * The same caveats documented in the sum(TimeType start, TimeType end)
+   * The same caveats documented in the sum(TimePoint start, TimePoint end)
    * comments apply here as well.
    */
-  template <typename ReturnType=double, typename Interval=TimeType>
-  ReturnType rate(TimeType start, TimeType end) const {
+  template <typename ReturnType = double, typename Interval = Duration>
+  ReturnType rate(TimePoint start, TimePoint end) const {
     ValueType intervalSum = sum(start, end);
-    TimeType interval = elapsed(start, end);
+    Duration interval = elapsed(start, end);
     return rateHelper<ReturnType, Interval>(intervalSum, interval);
   }
 
@@ -345,13 +342,13 @@ class BucketedTimeSeries {
    * Estimate the rate of data points being added during the specified time
    * period.
    *
-   * The same caveats documented in the sum(TimeType start, TimeType end)
+   * The same caveats documented in the sum(TimePoint start, TimePoint end)
    * comments apply here as well.
    */
-  template <typename ReturnType=double, typename Interval=TimeType>
-  ReturnType countRate(TimeType start, TimeType end) const {
+  template <typename ReturnType = double, typename Interval = Duration>
+  ReturnType countRate(TimePoint start, TimePoint end) const {
     uint64_t intervalCount = count(start, end);
-    TimeType interval = elapsed(start, end);
+    Duration interval = elapsed(start, end);
     return rateHelper<ReturnType, Interval>(intervalCount, interval);
   }
 
@@ -365,8 +362,8 @@ class BucketedTimeSeries {
    * to break out of the loop and stop, without calling the function on any
    * more buckets.
    *
-   * bool function(const Bucket& bucket, TimeType bucketStart,
-   *               TimeType nextBucketStart)
+   * bool function(const Bucket& bucket, TimePoint bucketStart,
+   *               TimePoint nextBucketStart)
    */
   template <typename Function>
   void forEachBucket(Function fn) const;
@@ -381,7 +378,7 @@ class BucketedTimeSeries {
    *
    * This method may not be called for all-time data.
    */
-  size_t getBucketIdx(TimeType time) const;
+  size_t getBucketIdx(TimePoint time) const;
 
   /*
    * Get the bucket at the specified index.
@@ -398,29 +395,56 @@ class BucketedTimeSeries {
    *
    * This method may not be called for all-time data.
    */
-  void getBucketInfo(TimeType time, size_t* bucketIdx,
-                     TimeType* bucketStart, TimeType* nextBucketStart) const;
+  void getBucketInfo(
+      TimePoint time,
+      size_t* bucketIdx,
+      TimePoint* bucketStart,
+      TimePoint* nextBucketStart) const;
+
+  /*
+   * Legacy APIs that accept a Duration parameters rather than TimePoint.
+   *
+   * These treat the Duration as relative to the clock epoch.
+   * Prefer using the correct TimePoint-based APIs instead.  These APIs will
+   * eventually be deprecated and removed.
+   */
+  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
+  addValueAggregated(Duration now, const ValueType& total, int64_t nsamples) {
+    return addValueAggregated(TimePoint(now), total, nsamples);
+  }
+  size_t update(Duration now) {
+    return update(TimePoint(now));
+  }
 
  private:
-  template <typename ReturnType=double, typename Interval=TimeType>
-  ReturnType rateHelper(ReturnType numerator, TimeType elapsedTime) const {
-    return detail::rateHelper<ReturnType, TimeType, Interval>(numerator,
-                                                              elapsedTime);
+  template <typename ReturnType = double, typename Interval = Duration>
+  ReturnType rateHelper(ReturnType numerator, Duration elapsedTime) const {
+    return detail::rateHelper<ReturnType, Duration, Interval>(
+        numerator, elapsedTime);
   }
 
-  TimeType getEarliestTimeNonEmpty() const;
-  size_t updateBuckets(TimeType now);
+  TimePoint getEarliestTimeNonEmpty() const;
+  size_t updateBuckets(TimePoint now);
 
-  ValueType rangeAdjust(TimeType bucketStart, TimeType nextBucketStart,
-                        TimeType start, TimeType end,
-                        ValueType input) const;
+  ValueType rangeAdjust(
+      TimePoint bucketStart,
+      TimePoint nextBucketStart,
+      TimePoint start,
+      TimePoint end,
+      ValueType input) const;
 
   template <typename Function>
-  void forEachBucket(TimeType start, TimeType end, Function fn) const;
+  void forEachBucket(TimePoint start, TimePoint end, Function fn) const;
 
-  TimeType firstTime_;   // time of first update() since clear()/constructor
-  TimeType latestTime_;  // time of last update()
-  TimeType duration_;    // total duration ("window length") of the time series
+  TimePoint firstTime_; // time of first update() since clear()/constructor
+  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