1
1
use num:: { Num , NumCast } ;
2
2
3
+ /// This type is responsible for calculating stats in a rolling fashion.
4
+ /// By rolling, it means that there is already some stats calculated
5
+ /// which needs to be further aggregated. This is commonly the case when
6
+ /// the sampling is done at a higher precision interval (say 1 minute) and
7
+ /// then further aggregated (say 1 hour).
8
+ ///
9
+ /// For example the number of lines written per hour is collected as new
10
+ /// write requests come in. However, the bucket [`crate::bucket::EventsBucket`]
11
+ /// holds `lines` as [`crate::stats::Stats<u64>`], to hold min/max/avg lines
12
+ /// written per minute. Then when taking samples per minute to calculate
13
+ /// hourly aggregates, [`RollingStats<T>`] is used. To see how it is calculated
14
+ /// see the [`RollingStats<T>::update`] method
3
15
#[ derive( Debug , Default ) ]
4
16
pub ( crate ) struct RollingStats < T > {
5
17
pub min : T ,
@@ -13,6 +25,12 @@ impl<T: Default + Num + Copy + NumCast + PartialOrd> RollingStats<T> {
13
25
RollingStats :: default ( )
14
26
}
15
27
28
+ /// Update the rolling stats [`Self::min`]/[`Self::max`]/[`Self::avg`] using
29
+ /// reference to an higher precision stats that is passed in. This is usually a
30
+ /// per minute interval stats. One thing to note here is the [`Self::num_samples`]
31
+ /// is updated locally here to calculate the rolling average for usually
32
+ /// an hour for a metric. Refer to [`crate::metrics::Writes`] or
33
+ /// [`crate::metrics::Queries`] to see how it is used
16
34
pub fn update ( & mut self , higher_precision_stats : & Stats < T > ) -> Option < ( ) > {
17
35
if self . num_samples == 0 {
18
36
self . min = higher_precision_stats. min ;
@@ -41,6 +59,8 @@ impl<T: Default + Num + Copy + NumCast + PartialOrd> RollingStats<T> {
41
59
}
42
60
}
43
61
62
+ /// This is basic stats to keep a tab on min/max/avg for a specific
63
+ /// metric
44
64
#[ derive( Debug , Default ) ]
45
65
pub ( crate ) struct Stats < T > {
46
66
pub min : T ,
@@ -54,6 +74,8 @@ impl<T: Default + Num + Copy + NumCast + PartialOrd> Stats<T> {
54
74
Stats :: default ( )
55
75
}
56
76
77
+ /// Update the [`Self::min`]/[`Self::max`]/[`Self::avg`] from a
78
+ /// new value that is sampled.
57
79
pub fn update ( & mut self , new_val : T ) -> Option < ( ) > {
58
80
if self . num_samples == 0 {
59
81
self . min = new_val;
@@ -75,6 +97,22 @@ impl<T: Default + Num + Copy + NumCast + PartialOrd> Stats<T> {
75
97
}
76
98
}
77
99
100
+ /// Generic function to calculate min/max/avg from another set of stats.
101
+ /// This function works for all types of numbers (unsigned/signed/floats).
102
+ /// It calculates min/max/avg by using already calculated min/max/avg for
103
+ /// possibly a higher resolution.
104
+ ///
105
+ /// For eg.
106
+ ///
107
+ /// Let's say we're looking at the stats for number of lines written.
108
+ /// And we have 1st sample's minimum was 20 and the 3rd sample's
109
+ /// minimum was 10. This means in the 1st sample for a whole minute
110
+ /// 20 was the minimum number of lines written in a single request and in
111
+ /// the 3rd sample (3rd minute) 10 is the minimum number of lines written
112
+ /// in a single request. These are already stats at per minute interval, when we
113
+ /// calculate the minimum number of lines for the whole hour we compare the samples
114
+ /// taken at per minute interval for whole hour. In this case 10 will be the new
115
+ /// minimum for the whole hour.
78
116
fn rollup_stats < T : Num + Copy + NumCast + PartialOrd > (
79
117
current_min : T ,
80
118
current_max : T ,
@@ -91,6 +129,10 @@ fn rollup_stats<T: Num + Copy + NumCast + PartialOrd>(
91
129
Some ( ( min, max, avg) )
92
130
}
93
131
132
+ /// Generic function to calculate min/max/avg from a new sampled value.
133
+ /// This function works for all types of numbers (unsigned/signed/floats).
134
+ /// One thing to note here is the average function, it is an incremental average
135
+ /// to avoid holding all the samples in memory.
94
136
fn stats < T : Num + Copy + NumCast + PartialOrd > (
95
137
current_min : T ,
96
138
current_max : T ,
0 commit comments