goodmetrics/aggregation/
statistic_set.rs

1use std::cmp::{max, min};
2
3/// A basic aggregation.
4#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
5pub struct StatisticSet {
6    /// Minimum observed value
7    pub min: i64,
8    /// Maximum observed value
9    pub max: i64,
10    /// Sum of all observed values
11    pub sum: i64,
12    /// Count of observations
13    pub count: u64,
14}
15
16impl std::fmt::Display for StatisticSet {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        f.debug_map()
19            .entry(&"min", &self.min)
20            .entry(&"max", &self.max)
21            .entry(&"sum", &self.sum)
22            .entry(&"count", &self.count)
23            .finish()
24    }
25}
26
27impl Default for StatisticSet {
28    fn default() -> Self {
29        Self {
30            min: i64::MAX,
31            max: i64::MIN,
32            sum: 0,
33            count: 0,
34        }
35    }
36}
37
38impl StatisticSet {
39    pub(crate) fn accumulate<T: Into<i64>>(&mut self, value: T) {
40        let v: i64 = value.into();
41        self.min = min(v, self.min);
42        self.max = max(v, self.max);
43        self.sum += v;
44        self.count += 1;
45    }
46}