nexus-stats-core 3.0.1

Core types and utilities shared across nexus-stats subcrates
Documentation
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/// Policy for when to close a bucket and start a new one.
#[derive(Debug, Clone, Copy)]
pub enum BucketPolicy {
    /// Close after N observations.
    Count(u64),
    /// Close after accumulating this much volume.
    Volume(f64),
    /// Close after this many nanoseconds (wall time as u64 nanos).
    WallTimeNanos(u64),
}

/// Summary of a closed bucket.
#[derive(Debug, Clone, Copy)]
pub struct BucketSummary {
    sum: f64,
    count: u64,
    first: f64,
    last: f64,
}

impl BucketSummary {
    /// Sum of all observations in the bucket.
    #[inline]
    #[must_use]
    pub fn sum(&self) -> f64 {
        self.sum
    }

    /// Number of observations in the bucket.
    #[inline]
    #[must_use]
    pub fn count(&self) -> u64 {
        self.count
    }

    /// Mean of observations in the bucket.
    #[inline]
    #[must_use]
    pub fn mean(&self) -> f64 {
        self.sum / self.count as f64
    }

    /// First observation in the bucket.
    #[inline]
    #[must_use]
    pub fn first(&self) -> f64 {
        self.first
    }

    /// Last observation in the bucket.
    #[inline]
    #[must_use]
    pub fn last(&self) -> f64 {
        self.last
    }

    /// Change from first to last observation.
    #[inline]
    #[must_use]
    pub fn change(&self) -> f64 {
        self.last - self.first
    }
}

/// Accumulates observations into time/count/volume-based buckets.
///
/// Tracks sum, count, first, last for each bucket. On close, the user
/// reads whichever summary they need (mean, sum, count, change).
/// No prediction logic — that's composition with `LaggedPredictor` or
/// `EwLinearRegressionF64`.
///
/// # Examples
///
/// ```
/// use nexus_stats_core::statistics::{BucketAccumulator, BucketPolicy};
///
/// let mut bucket = BucketAccumulator::builder()
///     .policy(BucketPolicy::Count(10))
///     .build().unwrap();
///
/// let mut closures = 0;
/// for i in 0..25 {
///     if let Ok(Some(_summary)) = bucket.update(i as f64) {
///         closures += 1;
///     }
/// }
/// assert_eq!(closures, 2); // 10 + 10, then 5 still open
/// ```
#[derive(Debug, Clone)]
pub struct BucketAccumulator {
    policy: BucketPolicy,
    sum: f64,
    count: u64,
    first: f64,
    last: f64,
    accumulated_volume: f64,
    start_nanos: u64,
}

/// Builder for [`BucketAccumulator`].
#[derive(Debug, Clone)]
pub struct BucketAccumulatorBuilder {
    policy: Option<BucketPolicy>,
}

impl BucketAccumulator {
    /// Creates a builder.
    #[inline]
    #[must_use]
    pub fn builder() -> BucketAccumulatorBuilder {
        BucketAccumulatorBuilder { policy: None }
    }

    /// Feed an observation (Count policy).
    ///
    /// Returns `Some(summary)` when the bucket closes.
    ///
    /// # Errors
    ///
    /// Returns `DataError` if the value is NaN or infinite.
    #[inline]
    pub fn update(&mut self, value: f64) -> Result<Option<BucketSummary>, crate::DataError> {
        check_finite!(value);
        self.accumulate(value);

        let should_close = match self.policy {
            BucketPolicy::Count(n) => self.count >= n,
            _ => false,
        };

        if should_close {
            Ok(Some(self.close_inner()))
        } else {
            Ok(None)
        }
    }

    /// Feed an observation with volume (Volume policy).
    ///
    /// Returns `Some(summary)` when accumulated volume reaches the threshold.
    ///
    /// # Errors
    ///
    /// Returns `DataError` if the value is NaN or infinite, or if volume
    /// is negative, NaN, or infinite.
    #[inline]
    pub fn update_volume(
        &mut self,
        value: f64,
        volume: f64,
    ) -> Result<Option<BucketSummary>, crate::DataError> {
        check_finite!(value);
        check_finite!(volume);
        if volume < 0.0 {
            return Err(crate::DataError::Negative);
        }
        self.accumulate(value);

        let should_close = match self.policy {
            BucketPolicy::Volume(threshold) => {
                self.accumulated_volume += volume;
                self.accumulated_volume >= threshold
            }
            _ => false,
        };

        if should_close {
            Ok(Some(self.close_inner()))
        } else {
            Ok(None)
        }
    }

    /// Feed an observation with a raw `u64` timestamp in nanoseconds
    /// (WallTime policy, `no_std` compatible).
    ///
    /// Returns `Some(summary)` when elapsed time reaches the threshold.
    ///
    /// # Errors
    ///
    /// Returns `DataError` if the value is NaN or infinite.
    #[inline]
    pub fn update_at_raw(
        &mut self,
        value: f64,
        timestamp_nanos: u64,
    ) -> Result<Option<BucketSummary>, crate::DataError> {
        check_finite!(value);
        self.accumulate(value);

        let should_close = match self.policy {
            BucketPolicy::WallTimeNanos(duration_ns) => {
                if self.count == 1 {
                    // First observation — record start time.
                    self.start_nanos = timestamp_nanos;
                    false
                } else {
                    timestamp_nanos.saturating_sub(self.start_nanos) >= duration_ns
                }
            }
            _ => false,
        };

        if should_close {
            Ok(Some(self.close_inner()))
        } else {
            Ok(None)
        }
    }

    /// Force close the current bucket and return its summary.
    /// Returns `None` if the bucket is empty.
    #[inline]
    pub fn close(&mut self) -> Option<BucketSummary> {
        if self.count == 0 {
            return None;
        }
        Some(self.close_inner())
    }

    #[inline]
    fn accumulate(&mut self, value: f64) {
        if self.count == 0 {
            self.first = value;
        }
        self.last = value;
        self.sum += value;
        self.count += 1;
    }

    fn close_inner(&mut self) -> BucketSummary {
        let summary = BucketSummary {
            sum: self.sum,
            count: self.count,
            first: self.first,
            last: self.last,
        };
        self.sum = 0.0;
        self.count = 0;
        self.first = 0.0;
        self.last = 0.0;
        self.accumulated_volume = 0.0;
        self.start_nanos = 0;
        summary
    }

    /// Number of observations in the current (open) bucket.
    #[inline]
    #[must_use]
    pub fn current_count(&self) -> u64 {
        self.count
    }

    /// Sum of observations in the current (open) bucket.
    #[inline]
    #[must_use]
    pub fn current_sum(&self) -> f64 {
        self.sum
    }

    /// Reset all state.
    pub fn reset(&mut self) {
        self.sum = 0.0;
        self.count = 0;
        self.first = 0.0;
        self.last = 0.0;
        self.accumulated_volume = 0.0;
        self.start_nanos = 0;
    }
}

impl BucketAccumulatorBuilder {
    /// Set the bucket closure policy. Required.
    #[inline]
    #[must_use]
    pub fn policy(mut self, policy: BucketPolicy) -> Self {
        self.policy = Some(policy);
        self
    }

    /// Build the accumulator.
    ///
    /// # Errors
    ///
    /// Returns `ConfigError::Missing` if no policy was set.
    pub fn build(self) -> Result<BucketAccumulator, crate::ConfigError> {
        let policy = self.policy.ok_or(crate::ConfigError::Missing("policy"))?;
        Ok(BucketAccumulator {
            policy,
            sum: 0.0,
            count: 0,
            first: 0.0,
            last: 0.0,
            accumulated_volume: 0.0,
            start_nanos: 0,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn count_policy_closes_every_n() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Count(10))
            .build()
            .unwrap();

        let mut closure_count = 0u32;
        for i in 0..30 {
            if let Ok(Some(s)) = bucket.update(i as f64) {
                closure_count += 1;
                assert_eq!(s.count(), 10);
            }
        }
        assert_eq!(closure_count, 3);
    }

    #[test]
    fn volume_policy() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Volume(100.0))
            .build()
            .unwrap();

        let mut closures = 0;
        // 10 observations with volume 15 each = 150, closes at 100
        for _ in 0..10 {
            if let Ok(Some(_)) = bucket.update_volume(1.0, 15.0) {
                closures += 1;
            }
        }
        assert_eq!(closures, 1); // closed once at 105 cumulative
    }

    #[test]
    fn volume_negative_rejected() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Volume(100.0))
            .build()
            .unwrap();
        assert!(bucket.update_volume(1.0, -5.0).is_err());
    }

    #[test]
    fn wall_time_raw_policy() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::WallTimeNanos(1_000_000_000)) // 1 second
            .build()
            .unwrap();

        // First observation at t=0
        assert!(bucket.update_at_raw(1.0, 0).unwrap().is_none());
        // At t=500ms — not yet
        assert!(bucket.update_at_raw(2.0, 500_000_000).unwrap().is_none());
        // At t=1.1s — closes
        let s = bucket.update_at_raw(3.0, 1_100_000_000).unwrap().unwrap();
        assert_eq!(s.count(), 3);
        assert!((s.first() - 1.0).abs() < f64::EPSILON);
        assert!((s.last() - 3.0).abs() < f64::EPSILON);
    }

    #[test]
    fn empty_close_returns_none() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Count(10))
            .build()
            .unwrap();
        assert!(bucket.close().is_none());
    }

    #[test]
    fn force_close() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Count(100))
            .build()
            .unwrap();
        bucket.update(1.0).unwrap();
        bucket.update(2.0).unwrap();
        bucket.update(3.0).unwrap();
        let s = bucket.close().unwrap();
        assert_eq!(s.count(), 3);
        assert!((s.mean() - 2.0).abs() < f64::EPSILON);
        assert!((s.change() - 2.0).abs() < f64::EPSILON);
    }

    #[test]
    fn summary_accessors() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Count(5))
            .build()
            .unwrap();
        let mut s = None;
        for i in 1..=5 {
            if let Ok(Some(summary)) = bucket.update(i as f64) {
                s = Some(summary);
            }
        }
        let s = s.expect("bucket should have closed at count=5");
        assert_eq!(s.count(), 5);
        assert!((s.sum() - 15.0).abs() < f64::EPSILON);
        assert!((s.mean() - 3.0).abs() < f64::EPSILON);
        assert!((s.first() - 1.0).abs() < f64::EPSILON);
        assert!((s.last() - 5.0).abs() < f64::EPSILON);
        assert!((s.change() - 4.0).abs() < f64::EPSILON);
    }

    #[test]
    fn reset_clears_state() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Count(10))
            .build()
            .unwrap();
        bucket.update(42.0).unwrap();
        assert_eq!(bucket.current_count(), 1);
        bucket.reset();
        assert_eq!(bucket.current_count(), 0);
        assert!((bucket.current_sum()).abs() < f64::EPSILON);
    }

    #[test]
    fn nan_rejected() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Count(10))
            .build()
            .unwrap();
        assert!(bucket.update(f64::NAN).is_err());
    }

    #[test]
    fn inf_rejected() {
        let mut bucket = BucketAccumulator::builder()
            .policy(BucketPolicy::Count(10))
            .build()
            .unwrap();
        assert!(bucket.update(f64::INFINITY).is_err());
    }
}