@@ -43,28 +43,36 @@ impl<T: Number<T>> Buckets<T> {
43
43
}
44
44
}
45
45
46
- /// Summarizes a set of measurements with explicitly defined buckets.
47
- struct HistValues < T > {
48
- record_sum : bool ,
49
- bounds : Vec < f64 > ,
46
+ /// Summarizes a set of measurements as a histogram with explicitly defined
47
+ /// buckets.
48
+ pub ( crate ) struct Histogram < T > {
50
49
values : Mutex < HashMap < AttributeSet , Buckets < T > > > ,
50
+ bounds : Vec < f64 > ,
51
+ record_min_max : bool ,
52
+ record_sum : bool ,
53
+ start : Mutex < SystemTime > ,
51
54
}
52
55
53
- impl < T : Number < T > > HistValues < T > {
54
- fn new ( mut bounds : Vec < f64 > , record_sum : bool ) -> Self {
55
- bounds. retain ( |v| !v. is_nan ( ) ) ;
56
- bounds. sort_by ( |a, b| a. partial_cmp ( b) . expect ( "NaNs filtered out" ) ) ;
57
-
58
- HistValues {
59
- record_sum,
60
- bounds,
56
+ impl < T : Number < T > > Histogram < T > {
57
+ pub ( crate ) fn new ( boundaries : Vec < f64 > , record_min_max : bool , record_sum : bool ) -> Self {
58
+ let mut histogram = Histogram {
61
59
values : Mutex :: new ( Default :: default ( ) ) ,
62
- }
60
+ bounds : boundaries,
61
+ record_min_max,
62
+ record_sum,
63
+ start : Mutex :: new ( SystemTime :: now ( ) ) ,
64
+ } ;
65
+
66
+ histogram. bounds . retain ( |v| !v. is_nan ( ) ) ;
67
+ histogram
68
+ . bounds
69
+ . sort_by ( |a, b| a. partial_cmp ( b) . expect ( "NaNs filtered out" ) ) ;
70
+
71
+ histogram
63
72
}
64
- }
65
73
66
- impl < T : Number < T > > HistValues < T > {
67
- fn measure ( & self , measurement : T , attrs : AttributeSet ) {
74
+ pub ( crate ) fn measure ( & self , measurement : T , attrs : & [ KeyValue ] ) {
75
+ let attrs: AttributeSet = attrs . into ( ) ;
68
76
let f = measurement. into_float ( ) ;
69
77
70
78
// This search will return an index in the range `[0, bounds.len()]`, where
@@ -109,35 +117,12 @@ impl<T: Number<T>> HistValues<T> {
109
117
b. sum ( measurement)
110
118
}
111
119
}
112
- }
113
-
114
- /// Summarizes a set of measurements as a histogram with explicitly defined
115
- /// buckets.
116
- pub ( crate ) struct Histogram < T > {
117
- hist_values : HistValues < T > ,
118
- record_min_max : bool ,
119
- start : Mutex < SystemTime > ,
120
- }
121
-
122
- impl < T : Number < T > > Histogram < T > {
123
- pub ( crate ) fn new ( boundaries : Vec < f64 > , record_min_max : bool , record_sum : bool ) -> Self {
124
- Histogram {
125
- hist_values : HistValues :: new ( boundaries, record_sum) ,
126
- record_min_max,
127
- start : Mutex :: new ( SystemTime :: now ( ) ) ,
128
- }
129
- }
130
-
131
- pub ( crate ) fn measure ( & self , measurement : T , attrs : & [ KeyValue ] ) {
132
- let attrs: AttributeSet = attrs. into ( ) ;
133
- self . hist_values . measure ( measurement, attrs)
134
- }
135
120
136
121
pub ( crate ) fn delta (
137
122
& self ,
138
123
dest : Option < & mut dyn Aggregation > ,
139
124
) -> ( usize , Option < Box < dyn Aggregation > > ) {
140
- let mut values = match self . hist_values . values . lock ( ) {
125
+ let mut values = match self . values . lock ( ) {
141
126
Ok ( guard) if !guard. is_empty ( ) => guard,
142
127
_ => return ( 0 , None ) ,
143
128
} ;
@@ -174,9 +159,9 @@ impl<T: Number<T>> Histogram<T> {
174
159
start_time : start,
175
160
time : t,
176
161
count : b. count ,
177
- bounds : self . hist_values . bounds . clone ( ) ,
162
+ bounds : self . bounds . clone ( ) ,
178
163
bucket_counts : b. counts . clone ( ) ,
179
- sum : if self . hist_values . record_sum {
164
+ sum : if self . record_sum {
180
165
b. total
181
166
} else {
182
167
T :: default ( )
@@ -207,7 +192,7 @@ impl<T: Number<T>> Histogram<T> {
207
192
& self ,
208
193
dest : Option < & mut dyn Aggregation > ,
209
194
) -> ( usize , Option < Box < dyn Aggregation > > ) {
210
- let values = match self . hist_values . values . lock ( ) {
195
+ let values = match self . values . lock ( ) {
211
196
Ok ( guard) if !guard. is_empty ( ) => guard,
212
197
_ => return ( 0 , None ) ,
213
198
} ;
@@ -248,9 +233,9 @@ impl<T: Number<T>> Histogram<T> {
248
233
start_time : start,
249
234
time : t,
250
235
count : b. count ,
251
- bounds : self . hist_values . bounds . clone ( ) ,
236
+ bounds : self . bounds . clone ( ) ,
252
237
bucket_counts : b. counts . clone ( ) ,
253
- sum : if self . hist_values . record_sum {
238
+ sum : if self . record_sum {
254
239
b. total
255
240
} else {
256
241
T :: default ( )
0 commit comments