Fix copyright lines
[folly.git] / folly / stats / BucketedTimeSeries.h
index 57307e5d108005c1544b5d9ec072b9802a10ec48..dd4e0f7160edd29863b9ea4ab7534c3f18795e7d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -19,7 +19,7 @@
 #include <chrono>
 #include <vector>
 
-#include <folly/detail/Stats.h>
+#include <folly/stats/detail/Bucket.h>
 
 namespace folly {
 
@@ -81,6 +81,18 @@ class BucketedTimeSeries {
    */
   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'
    *
@@ -104,13 +116,13 @@ class BucketedTimeSeries {
   /*
    * 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
@@ -192,6 +204,29 @@ class BucketedTimeSeries {
     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.
    *
@@ -258,7 +293,7 @@ class BucketedTimeSeries {
    * 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>();
   }
@@ -271,7 +306,7 @@ class BucketedTimeSeries {
    */
   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());
   }
 
   /*
@@ -288,7 +323,8 @@ class BucketedTimeSeries {
    */
   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());
   }
 
   /*
@@ -349,7 +385,8 @@ class BucketedTimeSeries {
   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);
   }
 
   /*
@@ -411,11 +448,11 @@ class BucketedTimeSeries {
   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) {
@@ -446,8 +483,8 @@ class BucketedTimeSeries {
   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