memfault_ssf/stats/
aggregator.rs

1//
2// Copyright (c) Memfault, Inc.
3// See License.txt for details
4use std::{fmt::Display, time::Duration};
5
6use super::DeliveryStats;
7
8#[derive(Debug)]
9pub struct StatsAggregator {
10    count: usize,
11    max_queueing: Duration,
12    max_processing: Duration,
13    total_processing: Duration,
14}
15
16impl StatsAggregator {
17    pub fn new() -> Self {
18        StatsAggregator {
19            count: 0,
20            max_queueing: Duration::ZERO,
21            max_processing: Duration::ZERO,
22            total_processing: Duration::ZERO,
23        }
24    }
25    pub fn add(&mut self, stats: &DeliveryStats) {
26        self.count += 1;
27        self.total_processing += stats.processing;
28        self.max_queueing = self.max_queueing.max(stats.queued);
29        self.max_processing = self.max_processing.max(stats.processing);
30    }
31}
32
33impl Default for StatsAggregator {
34    fn default() -> Self {
35        StatsAggregator::new()
36    }
37}
38
39impl Display for StatsAggregator {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        if self.count > 0 {
42            f.write_fmt(format_args!(
43                "Calls: {} Max Queueing: {} Processing (avg/max): {}/{} ",
44                self.count,
45                self.max_queueing.as_millis(),
46                (self.total_processing / (self.count as u32)).as_millis(),
47                self.max_processing.as_millis(),
48            ))
49        } else {
50            f.write_fmt(format_args!("Calls: 0"))
51        }
52    }
53}