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
//! Host metrics and the 10-minute history over the console socket (#10942, #10943).
//!
//! Why: `tm tui` must draw the console's machine cards and their sparklines
//! over UDS alone (#10899), and it has no Cargo edge on trusty-console. The
//! payloads `GET /api/console/machine-status` and
//! `GET /api/console/machine-status/history` serve were defined inside
//! trusty-console; they move here so both ends decode one definition.
//! What: the params and answers of `console.machine_status`
//! ([`MachineStatusRequest`] → [`ConsoleMachineStatus`]) and `console.history`
//! ([`HistoryRequest`] → [`HistorySnapshot`]), plus the history's row types
//! ([`ServiceSample`], [`ServiceTransition`], [`ServiceState`]) that
//! trusty-console re-exports from `machine_history`. The per-service CPU/RSS
//! rings (#10943) ride on [`HistorySnapshot::service_samples`].
//! Gated on `console-metrics` + `host-metrics`, the two modules the payloads
//! embed ([`MachineStatus`], [`HostMetrics`]).
//! Test: `metrics_tests.rs` beside this file, and trusty-console's
//! `console_uds::metrics_tests` over a real socket.
// #10942: moved from trusty_console::machine_history so the TUI can decode it.
use BTreeMap;
use fmt;
use ;
use ;
use ServiceStatus;
use crateServiceHealth;
use crateMachineStatus;
use crateHostMetrics;
/// The schema version [`HistorySnapshot`] advertises.
///
/// Why: lets a client detect a shape change instead of mis-rendering one,
/// as `MachineStatus::schema_version` does.
/// What: bumped on any change a client must notice. `2` (#6642) added
/// `service_samples`; `3` (#6773) added `ServiceSample::rss_bytes`; `4`
/// (#6908) added `sse_client_count`.
/// Test: `history_snapshot_golden_json`.
pub const MACHINE_HISTORY_SCHEMA_VERSION: u32 = 4;
/// Params of `console.machine_status`: none today.
///
/// Why: a typed request lets a later console add a param without a new method.
/// What: encodes as `{}`; decodes from an absent or `null` params, an object
/// (keys ignored) or an empty array. Any other value is refused, so the router
/// answers `invalid_params` rather than guessing.
/// Test: `request_types_accept_absent_null_or_an_object`,
/// `request_types_refuse_a_scalar_or_a_non_empty_array`.
/// Params of `console.history`: none today. Same decode rules as
/// [`MachineStatusRequest`].
///
/// Test: `request_types_accept_absent_null_or_an_object`,
/// `request_types_refuse_a_scalar_or_a_non_empty_array`.
/// Accepts the spellings of "no params" and refuses everything else.
;
/// The `console.machine_status` answer.
///
/// Why: the HTTP route answers `503` until the first host sample lands; a UDS
/// call has no status code, so readiness travels in the body, as
/// `ConsoleFleetStatus::ready` does.
/// What: `ready` is `false` and `machine` is `null` before the first host
/// sample. Otherwise `machine` is exactly the body
/// `GET /api/console/machine-status` serves for the same caches.
/// Test: `machine_status_not_ready_golden_json`,
/// `machine_status_ready_round_trips`.
/// One service's state, CPU and memory at one instant (#6642, #6773).
///
/// Why: a service row draws a CPU and a memory graph side by side; one sample
/// carrying both keeps the two graphs on the same second.
/// What: the service id (joins `ServiceInfo::id`), the status the poller last
/// detected, the CPU percentage (`100.0` is one saturated core) and the
/// resident memory in bytes. Each figure is `null` when nothing was measured —
/// never `0`, which would draw an idle service where there is none. Both keys
/// are serialised unconditionally, so a client parses one shape.
/// Test: `service_sample_serialises_absent_figures_as_null`.
/// The state a service is in, as the transition log records it (#6641).
///
/// Why: `ServiceHealth` is what a service says about itself and cannot say
/// "not heard from"; the timeline needs that fourth state.
/// What: `Up` / `Degraded` map from `Ok` / `Degraded`; `Down` covers a
/// self-reported `Error` and a report gone stale; `Unknown` is a service never
/// observed, or a spelling this build does not know. Serialised lowercase.
/// Test: `service_state_maps_from_self_reported_health`,
/// `history_snapshot_decodes_unknown_state_and_status_as_unknown`.
/// One recorded change of a service's state (#6641).
///
/// Why: the timeline draws a band per state, so it needs both ends of the
/// change and when it happened.
/// What: the service's id and display name, the state it left, the state it
/// entered, and the Unix second the console observed the change.
/// Test: `history_snapshot_golden_json`.
/// The `console.history` answer, and the body of
/// `GET /api/console/machine-status/history` and the SSE `history` event.
///
/// Why: one shape for every reader. The capacities and the sample interval
/// travel with the data because a graph's x-axis is derived from them.
/// What: host samples and transitions oldest-first; per-service sample rings
/// keyed by service id, oldest-first within each id (#6642, #10943); the
/// capacities; the sampler's interval; the count of attached browser SSE
/// streams (#6908 — not a UDS or bus client count); and the schema version.
/// Before the first sample every collection is empty, which is a complete
/// answer ("no samples yet"), not an error. Every field defaults on decode, so
/// a newer TUI reads an older console.
/// Test: `history_snapshot_golden_json`,
/// `history_snapshot_decodes_a_payload_missing_fields`.