]>
Commit | Line | Data |
---|---|---|
2260f065 DM |
1 | #[derive(Debug, Default)] |
2 | /// Tracker for progress of operations iterating over `Datastore` contents. | |
3 | pub struct StoreProgress { | |
4 | /// Completed groups | |
5 | pub done_groups: u64, | |
6 | /// Total groups | |
7 | pub total_groups: u64, | |
8 | /// Completed snapshots within current group | |
9 | pub done_snapshots: u64, | |
10 | /// Total snapshots in current group | |
11 | pub group_snapshots: u64, | |
12 | } | |
13 | ||
14 | impl StoreProgress { | |
15 | pub fn new(total_groups: u64) -> Self { | |
16 | StoreProgress { | |
17 | total_groups, | |
18 | .. Default::default() | |
19 | } | |
20 | } | |
21 | ||
22 | /// Calculates an interpolated relative progress based on current counters. | |
23 | pub fn percentage(&self) -> f64 { | |
24 | let per_groups = (self.done_groups as f64) / (self.total_groups as f64); | |
25 | if self.group_snapshots == 0 { | |
26 | per_groups | |
27 | } else { | |
28 | let per_snapshots = (self.done_snapshots as f64) / (self.group_snapshots as f64); | |
29 | per_groups + (1.0 / self.total_groups as f64) * per_snapshots | |
30 | } | |
31 | } | |
32 | } | |
33 | ||
34 | impl std::fmt::Display for StoreProgress { | |
35 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
36 | if self.group_snapshots == 0 { | |
37 | write!( | |
38 | f, | |
39 | "{:.2}% ({} of {} groups)", | |
40 | self.percentage() * 100.0, | |
41 | self.done_groups, | |
42 | self.total_groups, | |
43 | ) | |
44 | } else if self.total_groups == 1 { | |
45 | write!( | |
46 | f, | |
47 | "{:.2}% ({} of {} snapshots)", | |
48 | self.percentage() * 100.0, | |
49 | self.done_snapshots, | |
50 | self.group_snapshots, | |
51 | ) | |
52 | } else { | |
53 | write!( | |
54 | f, | |
55 | "{:.2}% ({} of {} groups, {} of {} group snapshots)", | |
56 | self.percentage() * 100.0, | |
57 | self.done_groups, | |
58 | self.total_groups, | |
59 | self.done_snapshots, | |
60 | self.group_snapshots, | |
61 | ) | |
62 | } | |
63 | } | |
64 | } |