1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Batch-level progress aggregation.
//!
//! [`BatchProgressAggregator`] tracks completion across a set of N total jobs
//! and exposes a percentage-complete readout and a done flag.
//!
//! # Example
//!
//! ```
//! use oximedia_batch::progress_agg::BatchProgressAggregator;
//!
//! let mut agg = BatchProgressAggregator::new(10);
//! agg.complete(3);
//! assert!((agg.percent() - 30.0).abs() < 1e-4);
//! assert!(!agg.is_done());
//!
//! agg.complete(7);
//! assert!(agg.is_done());
//! ```
// ---------------------------------------------------------------------------
// BatchProgressAggregator
// ---------------------------------------------------------------------------
/// Aggregates completion counts across a fixed-size batch of jobs.
#[derive(Debug, Clone)]
pub struct BatchProgressAggregator {
/// Total number of jobs in the batch.
total: usize,
/// Number of completed jobs (clamped to `total`).
completed: usize,
/// Number of failed jobs (informational only; counted separately).
failed: usize,
}
impl BatchProgressAggregator {
/// Create a new aggregator for a batch of `total` jobs.
///
/// # Panics
///
/// (None — zero `total` is allowed; `percent()` will return `0.0` and
/// `is_done()` will return `true`.)
#[must_use]
pub fn new(total: usize) -> Self {
Self {
total,
completed: 0,
failed: 0,
}
}
/// Mark `n` additional jobs as successfully completed.
///
/// The running total is clamped to `total` so over-reporting never causes
/// a percentage above 100 %.
pub fn complete(&mut self, n: usize) {
self.completed = (self.completed.saturating_add(n)).min(self.total);
}
/// Mark `n` additional jobs as failed.
///
/// Failed jobs are counted separately; they do **not** automatically
/// count as completed. Call `complete(n)` as well if you want to
/// advance the progress.
pub fn record_failure(&mut self, n: usize) {
self.failed = self.failed.saturating_add(n);
}
/// Completion percentage in `[0.0, 100.0]`.
///
/// Returns `100.0` when `total == 0` (vacuously done).
#[must_use]
pub fn percent(&self) -> f32 {
if self.total == 0 {
return 100.0;
}
(self.completed as f32 / self.total as f32) * 100.0
}
/// Returns `true` when all jobs have been completed (`completed >= total`).
#[must_use]
pub fn is_done(&self) -> bool {
self.completed >= self.total
}
/// Number of successfully completed jobs.
#[must_use]
pub fn completed(&self) -> usize {
self.completed
}
/// Number of failed jobs.
#[must_use]
pub fn failed(&self) -> usize {
self.failed
}
/// Total number of jobs in this batch.
#[must_use]
pub fn total(&self) -> usize {
self.total
}
/// Number of jobs that have neither completed nor failed.
#[must_use]
pub fn remaining(&self) -> usize {
self.total.saturating_sub(self.completed)
}
/// Reset counters (useful when reusing the aggregator for a new batch).
pub fn reset(&mut self) {
self.completed = 0;
self.failed = 0;
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
// ── new ──────────────────────────────────────────────────────────────────
#[test]
fn test_new_starts_at_zero() {
let agg = BatchProgressAggregator::new(10);
assert_eq!(agg.completed(), 0);
assert_eq!(agg.total(), 10);
assert!(!agg.is_done());
}
#[test]
fn test_new_zero_total_is_done() {
let agg = BatchProgressAggregator::new(0);
assert!(agg.is_done());
assert!((agg.percent() - 100.0).abs() < 1e-4);
}
// ── complete ──────────────────────────────────────────────────────────────
#[test]
fn test_complete_increments_counter() {
let mut agg = BatchProgressAggregator::new(10);
agg.complete(3);
assert_eq!(agg.completed(), 3);
}
#[test]
fn test_complete_clamped_to_total() {
let mut agg = BatchProgressAggregator::new(5);
agg.complete(10); // more than total
assert_eq!(agg.completed(), 5);
assert!(agg.is_done());
}
#[test]
fn test_complete_accumulates() {
let mut agg = BatchProgressAggregator::new(10);
agg.complete(3);
agg.complete(4);
assert_eq!(agg.completed(), 7);
}
// ── percent ───────────────────────────────────────────────────────────────
#[test]
fn test_percent_zero_completed() {
let agg = BatchProgressAggregator::new(10);
assert!((agg.percent() - 0.0).abs() < 1e-4);
}
#[test]
fn test_percent_partial() {
let mut agg = BatchProgressAggregator::new(10);
agg.complete(3);
assert!((agg.percent() - 30.0).abs() < 1e-3);
}
#[test]
fn test_percent_full() {
let mut agg = BatchProgressAggregator::new(4);
agg.complete(4);
assert!((agg.percent() - 100.0).abs() < 1e-4);
}
// ── is_done ───────────────────────────────────────────────────────────────
#[test]
fn test_is_done_false_initially() {
let agg = BatchProgressAggregator::new(5);
assert!(!agg.is_done());
}
#[test]
fn test_is_done_true_when_all_complete() {
let mut agg = BatchProgressAggregator::new(3);
agg.complete(3);
assert!(agg.is_done());
}
// ── record_failure ────────────────────────────────────────────────────────
#[test]
fn test_record_failure_increments_failed() {
let mut agg = BatchProgressAggregator::new(10);
agg.record_failure(2);
assert_eq!(agg.failed(), 2);
}
#[test]
fn test_record_failure_does_not_advance_completed() {
let mut agg = BatchProgressAggregator::new(10);
agg.record_failure(3);
assert_eq!(agg.completed(), 0);
assert!(!agg.is_done());
}
// ── remaining ────────────────────────────────────────────────────────────
#[test]
fn test_remaining_decreases_with_completion() {
let mut agg = BatchProgressAggregator::new(10);
agg.complete(4);
assert_eq!(agg.remaining(), 6);
}
#[test]
fn test_remaining_zero_when_done() {
let mut agg = BatchProgressAggregator::new(3);
agg.complete(3);
assert_eq!(agg.remaining(), 0);
}
// ── reset ────────────────────────────────────────────────────────────────
#[test]
fn test_reset_clears_counters() {
let mut agg = BatchProgressAggregator::new(10);
agg.complete(5);
agg.record_failure(2);
agg.reset();
assert_eq!(agg.completed(), 0);
assert_eq!(agg.failed(), 0);
}
}