monitrs-core 1.0.1

Platform-neutral data model, rate engine, history ring, and diagnostics for monitrs
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! The history a rule is allowed to look at, and how it counts over it.
//!
//! # Why this is not just a [`HistoryView`]
//!
//! §11.1 sketches the rule interface as `evaluate(&SystemSnapshot, &HistoryView)`.
//! In this codebase a [`HistoryView`] is deliberately only a *cursor*: it holds no
//! reference to a ring so that it can be copied around application state (see
//! [`crate::history::view`]). A rule therefore needs the cursor **and** the ring
//! it indexes, and [`HistoryWindow`] is that pair. [`HistoryWindow::view`] exposes
//! the cursor unchanged.
//!
//! Evaluating against the cursor rather than always against live data is what lets
//! the Inspect screen explain a *selected* historical sample (§2.1, §7.5) with the
//! same rules that produced the live radar.
//!
//! # Counting rules
//!
//! Every count here reports three numbers: how many samples met the condition, how
//! many were readable at all, and how many were unavailable. A sample whose input
//! was withheld is **not** counted as failing the condition and **not** counted
//! towards the minimum sample requirement — §26's "unavailable is not zero" applies
//! to counting as much as to display, and §11.3 requires a counter reset not to be
//! read as an event.

use core::time::Duration;

use crate::history::{
    ContributorMetric, HistoricalSample, HistoryMetric, HistoryRing, HistoryView,
};
use crate::model::{MeasuredValue, ProcessIdentity};

use super::TimeWindow;

/// The outcome of counting a condition over a window of samples.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Counted {
    /// Samples whose reading met the condition.
    pub matched: usize,
    /// Samples that produced a fresh reading at all.
    pub considered: usize,
    /// Samples whose reading was unavailable, stale, or warming up.
    pub unavailable: usize,
    /// Monotonic span between the oldest and newest sample visited.
    pub span: Duration,
}

impl Counted {
    /// How many samples were visited, readable or not.
    #[must_use]
    pub const fn visited(&self) -> usize {
        self.considered.saturating_add(self.unavailable)
    }

    /// The evidence window these counts cover.
    #[must_use]
    pub const fn window(&self) -> TimeWindow {
        TimeWindow::new(self.span, self.considered)
    }

    /// Whether the condition held often enough, over enough readings, to support
    /// a sustained claim (§11.3).
    ///
    /// Both halves matter: `matched >= required` is the sustained condition, and
    /// `considered >= minimum` is the minimum-sample rule that stops a rule from
    /// firing on the second tick after launch.
    #[must_use]
    pub const fn sustained(&self, required: usize, minimum: usize) -> bool {
        self.considered >= minimum && self.matched >= required
    }
}

/// A ring plus the cursor into it that a rule evaluates against.
#[derive(Clone, Copy, Debug)]
pub struct HistoryWindow<'a> {
    ring: &'a HistoryRing,
    view: HistoryView,
}

impl<'a> HistoryWindow<'a> {
    /// Pairs a ring with an explicit cursor.
    #[must_use]
    pub const fn new(ring: &'a HistoryRing, view: HistoryView) -> Self {
        Self { ring, view }
    }

    /// Pairs a ring with a cursor following the newest sample.
    #[must_use]
    pub const fn live(ring: &'a HistoryRing) -> Self {
        Self::new(ring, HistoryView::live())
    }

    /// The cursor, which is the `HistoryView` §11.1's sketch passes.
    #[must_use]
    pub const fn view(&self) -> HistoryView {
        self.view
    }

    /// The ring being read.
    #[must_use]
    pub const fn ring(&self) -> &'a HistoryRing {
        self.ring
    }

    /// The interval history is configured to retain samples at (§8.5).
    ///
    /// Rules that need a reference interval use this rather than assuming one
    /// second (§8.1).
    #[must_use]
    pub fn expected_interval(&self) -> Duration {
        self.ring.limits().interval()
    }

    /// The sample the cursor selects, or the newest one when live.
    #[must_use]
    pub fn selected(&self) -> Option<&'a HistoricalSample> {
        self.view.selected(self.ring)
    }

    /// How many samples are retained in total.
    #[must_use]
    pub fn len(&self) -> usize {
        self.ring.len()
    }

    /// Whether no sample has been recorded yet.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.ring.is_empty()
    }

    /// The `count` samples ending at the cursor, oldest first.
    ///
    /// Resolved by absolute index, so it costs one deque lookup per sample and
    /// never scans the ring (§21 M4).
    pub fn recent(&self, count: usize) -> impl DoubleEndedIterator<Item = &'a HistoricalSample> {
        let ring = self.ring;
        self.bounds(count)
            .into_iter()
            .flat_map(move |(first, last)| {
                (first..=last).filter_map(move |at| ring.get_absolute(at))
            })
    }

    /// The newest retained sample strictly older than `sequence`.
    ///
    /// Rules that compare "now" against "the previous sample" use this so they
    /// work whether or not the snapshot under evaluation has already been recorded
    /// into the ring.
    #[must_use]
    pub fn previous_sample(&self, sequence: u64) -> Option<&'a HistoricalSample> {
        self.recent(2).rfind(|sample| sample.sequence < sequence)
    }

    /// Counts how many of the `count` most recent samples satisfy `predicate`.
    ///
    /// `predicate` sees only *freshly measured* values: a sample whose metric was
    /// unavailable is tallied in [`Counted::unavailable`] and never passed to the
    /// predicate, so no rule can accidentally treat a missing reading as a zero
    /// (§26).
    #[must_use]
    pub fn count_where(
        &self,
        metric: HistoryMetric,
        count: usize,
        predicate: impl Fn(f64) -> bool,
    ) -> Counted {
        let mut counted = Counted::default();
        let mut oldest: Option<Duration> = None;
        let mut newest = Duration::ZERO;

        for sample in self.recent(count) {
            if oldest.is_none() {
                oldest = Some(sample.monotonic_offset);
            }
            newest = sample.monotonic_offset;
            match sample.system.scalar(metric) {
                Some(value) => {
                    counted.considered = counted.considered.saturating_add(1);
                    if predicate(value) {
                        counted.matched = counted.matched.saturating_add(1);
                    }
                }
                None => counted.unavailable = counted.unavailable.saturating_add(1),
            }
        }

        counted.span = newest.saturating_sub(oldest.unwrap_or(newest));
        counted
    }

    /// Counts how many of the `count` most recent samples are at or above
    /// `threshold`.
    #[must_use]
    pub fn count_at_least(&self, metric: HistoryMetric, count: usize, threshold: f64) -> Counted {
        self.count_where(metric, count, |value| value >= threshold)
    }

    /// The oldest and newest freshly measured values of `metric` in the window.
    ///
    /// Returns `None` unless *both* ends were measured, because a trend computed
    /// against a missing endpoint is a fabrication (§26). The returned duration is
    /// the real span between the two samples, never an assumed one (§8.1).
    #[must_use]
    pub fn trend(&self, metric: HistoryMetric, count: usize) -> Option<(f64, f64, Duration)> {
        let mut first: Option<(f64, Duration)> = None;
        let mut last: Option<(f64, Duration)> = None;
        for sample in self.recent(count) {
            if let Some(value) = sample.system.scalar(metric) {
                if first.is_none() {
                    first = Some((value, sample.monotonic_offset));
                }
                last = Some((value, sample.monotonic_offset));
            }
        }
        let (start, start_at) = first?;
        let (end, end_at) = last?;
        Some((start, end, end_at.saturating_sub(start_at)))
    }

    /// The oldest and newest absolute indices of the `count` samples ending at the
    /// cursor.
    fn bounds(&self, count: usize) -> Option<(u64, u64)> {
        if count == 0 {
            return None;
        }
        let last = self.view.selected_absolute(self.ring)?;
        let span = u64::try_from(count).unwrap_or(u64::MAX).saturating_sub(1);
        let first = last.saturating_sub(span).max(self.ring.first_absolute());
        Some((first, last))
    }
}

/// The retained value of a contributor metric for one process in one sample.
///
/// Keyed on the full [`ProcessIdentity`], so a reused PID never resolves to the
/// series of the process that used to hold it (§26).
///
/// Contributor lists are bounded to the top `K` per metric (§8.5), so a process that
/// dropped out of the top `K` returns `None` — a gap, not a zero.
#[must_use]
pub fn contributor_value(
    sample: &HistoricalSample,
    metric: ContributorMetric,
    identity: ProcessIdentity,
) -> Option<f64> {
    sample
        .contributors
        .metric(metric)
        .entries()
        .iter()
        .find(|entry| entry.identity == identity)
        .map(|entry| measured_scalar(entry.value))
}

/// A measured value as a comparable number.
///
/// Byte counts and event counts are integral in the model (§10.4); widening them
/// here is only ever done to compare or difference them, never to store them.
pub(crate) fn measured_scalar(value: MeasuredValue) -> f64 {
    match value {
        MeasuredValue::Bytes(bytes) | MeasuredValue::Count(bytes) => bytes as f64,
        MeasuredValue::ByteRate(rate) | MeasuredValue::EventRate(rate) => rate.per_second(),
        MeasuredValue::Percent(percent) => f64::from(percent.value()),
        MeasuredValue::Duration(duration) => duration.as_secs_f64(),
        MeasuredValue::Load(load) => f64::from(load),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::diagnostics::fixtures::{Timeline, set_cpu, set_memory};
    use crate::history::HistoryMetric;
    use crate::model::{MetricState, UnavailableReason};

    #[test]
    fn counting_an_empty_ring_reports_nothing_considered() {
        let timeline = Timeline::new(Duration::from_secs(1));
        let window = timeline.window();
        let counted = window.count_at_least(HistoryMetric::CpuBusy, 15, 80.0);

        assert!(window.is_empty());
        assert_eq!(counted, Counted::default());
        assert!(!counted.sustained(1, 1));
        assert_eq!(counted.window().samples, 0);
    }

    #[test]
    fn the_expected_interval_comes_from_history_rather_than_an_assumption() {
        // §8.1 forbids assuming one second; the rules that need a reference
        // interval read the configured one.
        for interval in [Duration::from_millis(500), Duration::from_secs(5)] {
            let timeline = Timeline::new(interval);
            assert_eq!(timeline.window().expected_interval(), timeline.interval());
            assert_eq!(timeline.window().expected_interval(), interval);
        }
    }

    #[test]
    fn counting_is_limited_to_the_requested_window() {
        let mut timeline = Timeline::new(Duration::from_secs(1));
        for _ in 0..10 {
            timeline.push(|snapshot| set_cpu(snapshot, 10.0));
        }
        for _ in 0..5 {
            timeline.push(|snapshot| set_cpu(snapshot, 90.0));
        }

        let window = timeline.window();
        let counted = window.count_at_least(HistoryMetric::CpuBusy, 5, 80.0);
        assert_eq!(counted.matched, 5);
        assert_eq!(counted.considered, 5);
        assert_eq!(counted.span, Duration::from_secs(4));

        let wider = window.count_at_least(HistoryMetric::CpuBusy, 15, 80.0);
        assert_eq!(wider.matched, 5);
        assert_eq!(wider.considered, 15);
    }

    #[test]
    fn a_window_larger_than_the_ring_counts_only_what_exists() {
        let mut timeline = Timeline::new(Duration::from_secs(1));
        for _ in 0..3 {
            timeline.push(|snapshot| set_cpu(snapshot, 99.0));
        }
        let counted = timeline
            .window()
            .count_at_least(HistoryMetric::CpuBusy, 100, 80.0);
        assert_eq!(counted.visited(), 3);
        assert_eq!(counted.matched, 3);
    }

    #[test]
    fn an_unavailable_sample_is_neither_a_match_nor_a_considered_reading() {
        let mut timeline = Timeline::new(Duration::from_secs(1));
        for _ in 0..5 {
            timeline.push(|snapshot| set_cpu(snapshot, 99.0));
        }
        for _ in 0..5 {
            timeline.push(|snapshot| {
                snapshot.cpu.total =
                    MetricState::TemporarilyUnavailable(UnavailableReason::CounterReset);
            });
        }

        let counted = timeline
            .window()
            .count_at_least(HistoryMetric::CpuBusy, 10, 80.0);
        assert_eq!(counted.matched, 5);
        assert_eq!(counted.considered, 5);
        assert_eq!(counted.unavailable, 5);
        assert_eq!(counted.visited(), 10);
        assert!(
            !counted.sustained(10, 10),
            "five readings cannot support a ten-sample claim"
        );
    }

    #[test]
    fn the_cursor_decides_which_window_is_counted() {
        let mut timeline = Timeline::new(Duration::from_secs(1));
        for _ in 0..10 {
            timeline.push(|snapshot| set_cpu(snapshot, 95.0));
        }
        for _ in 0..10 {
            timeline.push(|snapshot| set_cpu(snapshot, 1.0));
        }

        let live = timeline.window();
        assert_eq!(
            live.count_at_least(HistoryMetric::CpuBusy, 10, 80.0)
                .matched,
            0
        );

        let mut view = HistoryView::live();
        view.step_back(timeline.ring(), 10);
        let historical = HistoryWindow::new(timeline.ring(), view);
        assert_eq!(
            historical
                .count_at_least(HistoryMetric::CpuBusy, 10, 80.0)
                .matched,
            10,
            "a rule evaluated over a selected sample must see that sample's past"
        );
        assert_eq!(historical.view(), view);
    }

    #[test]
    fn a_trend_needs_both_endpoints_measured() {
        let mut timeline = Timeline::new(Duration::from_secs(1));
        timeline.push(|snapshot| set_memory(snapshot, 1_000, 800));
        timeline.push(|snapshot| {
            snapshot.memory.usage = MetricState::PermissionDenied;
        });
        assert!(
            timeline
                .window()
                .trend(HistoryMetric::MemoryUsedShare, 2)
                .is_some(),
            "the older endpoint is still measured, so the trend spans one sample"
        );

        let mut only_unavailable = Timeline::new(Duration::from_secs(1));
        only_unavailable.push(|snapshot| {
            snapshot.memory.usage = MetricState::PermissionDenied;
        });
        assert!(
            only_unavailable
                .window()
                .trend(HistoryMetric::MemoryUsedShare, 2)
                .is_none()
        );
    }

    #[test]
    fn a_trend_reports_the_real_span_between_the_endpoints() {
        let mut timeline = Timeline::new(Duration::from_millis(500));
        for used_share in [10u64, 20, 30] {
            // History retains the *used* share, so the fixture sets availability to
            // its complement.
            timeline.push(move |snapshot| set_memory(snapshot, 1_000, 1_000 - used_share * 10));
        }
        let (start, end, span) = timeline
            .window()
            .trend(HistoryMetric::MemoryUsedShare, 3)
            .expect("three measured samples");
        assert!((start - 10.0).abs() < 0.01, "{start}");
        assert!((end - 30.0).abs() < 0.01, "{end}");
        assert_eq!(span, Duration::from_secs(1), "two 500ms intervals");
    }

    #[test]
    fn the_previous_sample_is_the_newest_one_older_than_the_snapshot() {
        let mut timeline = Timeline::new(Duration::from_secs(1));
        timeline.push(|snapshot| set_cpu(snapshot, 1.0));
        timeline.push(|snapshot| set_cpu(snapshot, 2.0));
        let current = timeline.push(|snapshot| set_cpu(snapshot, 3.0));

        let window = timeline.window();
        let previous = window
            .previous_sample(current.sequence)
            .expect("a previous sample exists");
        assert_eq!(previous.sequence, current.sequence - 1);
        assert!(
            window.previous_sample(0).is_none(),
            "nothing precedes the first sample"
        );
    }

    #[test]
    fn a_zero_length_window_reads_nothing_instead_of_panicking() {
        let mut timeline = Timeline::new(Duration::from_secs(1));
        timeline.push(|snapshot| set_cpu(snapshot, 50.0));
        assert_eq!(timeline.window().recent(0).count(), 0);
        assert_eq!(
            timeline
                .window()
                .count_at_least(HistoryMetric::CpuBusy, 0, 1.0)
                .visited(),
            0
        );
    }

    #[test]
    fn every_measured_value_kind_has_a_comparable_scalar() {
        use crate::units::{Percent, Rate};
        let rate = Rate::new(1_024.0).expect("valid rate");
        let cases = [
            (MeasuredValue::Bytes(4_096), 4_096.0),
            (MeasuredValue::Count(7), 7.0),
            (MeasuredValue::ByteRate(rate), 1_024.0),
            (MeasuredValue::EventRate(rate), 1_024.0),
            (
                MeasuredValue::Percent(Percent::new(37.5).expect("valid")),
                37.5,
            ),
            (MeasuredValue::Duration(Duration::from_secs(2)), 2.0),
            (MeasuredValue::Load(4.25), 4.25),
        ];
        for (value, expected) in cases {
            let scalar = measured_scalar(value);
            assert!((scalar - expected).abs() < 0.001, "{value:?} -> {scalar}");
        }
    }
}