Skip to main content

nmbrs_tui/
reporter.rs

1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! TUI reporter: receives base-cadence metrics snapshots and forwards
5//! them to the TUI render thread via an mpsc channel. Used for
6//! sparkline feeds, history rings, and the live percentile display.
7//!
8//! For per-window (10s / 1m / …) sample-weighted views the TUI
9//! reads from a shared [`nmbrs_metrics::metrics_query::MetricsQuery`] —
10//! see SRD-42.
11
12use std::sync::mpsc;
13
14use nmbrs_metrics::scheduler::Reporter;
15use nmbrs_metrics::snapshot::MetricSet;
16
17/// A metrics reporter that forwards snapshots to the TUI via a channel.
18pub struct TuiReporter {
19    sender: mpsc::Sender<MetricSet>,
20}
21
22impl TuiReporter {
23    /// Create a reporter + its receiving end.
24    pub fn channel() -> (Self, mpsc::Receiver<MetricSet>) {
25        let (sender, receiver) = mpsc::channel();
26        (Self { sender }, receiver)
27    }
28}
29
30impl Reporter for TuiReporter {
31    fn report(&mut self, snapshot: &MetricSet) {
32        // Non-blocking send — if the TUI is slow, drop snapshots.
33        let _ = self.sender.send(snapshot.clone());
34    }
35}