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
//! Frontend-facing traits. Frontends (`tui`, `mcp`, `serve`) never touch OS APIs: they receive a
//! [`SnapshotProvider`] (and optionally an [`crate::actions::Actuator`]) built by `oomtop-cli`.
use crate::headroom::HeadroomConfig;
use crate::history::History;
use crate::model::Snapshot;
/// Produces enriched snapshots (sampled, attributed, adapters probed, idle/orphan/forecast applied).
pub trait SnapshotProvider: Send {
/// Takes a fresh sample (the TUI/serve call this on their refresh cadence; MCP per call).
fn snapshot(&mut self) -> Snapshot;
/// Like [`Self::snapshot`] but samples every source now, ignoring the cadence (the TUI's second
/// sample 500 ms after start, so CPU % appears within a second; a refresh right after an action).
fn snapshot_now(&mut self) -> Snapshot {
self.snapshot()
}
/// History ring buffer of samples taken so far (may be empty for one-shot providers).
fn history(&self) -> &History;
/// Headroom tunables from config.
fn headroom_config(&self) -> HeadroomConfig {
HeadroomConfig::default()
}
}
/// A provider that always returns the same snapshot (tests, fixtures, `--replay`).
#[derive(Debug, Clone, Default)]
pub struct StaticProvider {
pub snapshot: Snapshot,
pub history: History,
pub headroom: HeadroomConfig,
}
impl StaticProvider {
pub fn new(snapshot: Snapshot) -> Self {
let mut history = History::default();
history.push_snapshot(&snapshot);
StaticProvider {
snapshot,
history,
headroom: HeadroomConfig::default(),
}
}
}
impl SnapshotProvider for StaticProvider {
fn snapshot(&mut self) -> Snapshot {
self.snapshot.clone()
}
fn history(&self) -> &History {
&self.history
}
fn headroom_config(&self) -> HeadroomConfig {
self.headroom.clone()
}
}
/// A provider replaying a sequence of snapshots, then repeating the last one.
#[derive(Debug, Clone, Default)]
pub struct SequenceProvider {
snapshots: Vec<Snapshot>,
next: usize,
history: History,
}
impl SequenceProvider {
pub fn new(snapshots: Vec<Snapshot>) -> Self {
SequenceProvider {
snapshots,
next: 0,
history: History::default(),
}
}
}
impl SnapshotProvider for SequenceProvider {
fn snapshot(&mut self) -> Snapshot {
let s = match self.snapshots.get(self.next) {
Some(s) => {
self.next += 1;
s.clone()
}
None => self.snapshots.last().cloned().unwrap_or_default(),
};
self.history.push_snapshot(&s);
s
}
fn history(&self) -> &History {
&self.history
}
}