Skip to main content

ferroui_dynamic_data/diagnostics/
mod.rs

1//! `DynamicData.Diagnostics`: statistics of the change sets of a sequence (`ChangeStatistics`,
2//! `ChangeSummary`, `DiagnosticOperators.CollectUpdateStats`).
3
4use std::time::SystemTime;
5
6/// The counts of one change set, or of all of them so far (`ChangeStatistics`).
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct ChangeStatistics {
9    /// The number of the change set, from 0; `None` before any (`Index`, `-1` upstream).
10    pub index: Option<usize>,
11    pub adds: usize,
12    pub updates: usize,
13    pub removes: usize,
14    pub refreshes: usize,
15    pub moves: usize,
16    /// The number of changes (`Count`).
17    pub count: usize,
18    /// When the statistics were made (`LastUpdated`).
19    pub last_updated: SystemTime,
20}
21
22impl Default for ChangeStatistics {
23    fn default() -> Self {
24        Self::new(None, 0, 0, 0, 0, 0, 0)
25    }
26}
27
28impl ChangeStatistics {
29    pub fn new(index: Option<usize>, adds: usize, updates: usize, removes: usize, refreshes: usize, moves: usize, count: usize) -> Self {
30        Self { index, adds, updates, removes, refreshes, moves, count, last_updated: SystemTime::now() }
31    }
32}
33
34/// The statistics of the latest change set and of all of them (`ChangeSummary`).
35#[derive(Clone, Debug, Default, PartialEq, Eq)]
36pub struct ChangeSummary {
37    index: Option<usize>,
38    /// The latest change set (`Latest`).
39    pub latest: ChangeStatistics,
40    /// Every change set so far (`Overall`).
41    pub overall: ChangeStatistics,
42}
43
44impl ChangeSummary {
45    /// No change set yet (`ChangeSummary.Empty`).
46    pub fn empty() -> Self {
47        Self::default()
48    }
49
50    pub fn new(index: Option<usize>, latest: ChangeStatistics, overall: ChangeStatistics) -> Self {
51        Self { index, latest, overall }
52    }
53
54    /// The number of the latest change set.
55    pub fn index(&self) -> Option<usize> {
56        self.index
57    }
58
59    /// The summary after one more change set with these counts (the accumulator of
60    /// `CollectUpdateStats`).
61    pub(crate) fn next(&self, adds: usize, updates: usize, removes: usize, refreshes: usize, moves: usize, count: usize) -> Self {
62        let index = self.overall.index.map_or(0, |index| index + 1);
63        let overall = &self.overall;
64        let latest = ChangeStatistics::new(Some(index), adds, updates, removes, refreshes, moves, count);
65        let overall = ChangeStatistics::new(
66            Some(index),
67            overall.adds + adds,
68            overall.updates + updates,
69            overall.removes + removes,
70            overall.refreshes + refreshes,
71            overall.moves + moves,
72            overall.count + count,
73        );
74        Self::new(Some(index), latest, overall)
75    }
76}