pamoja-dashboard 0.1.17

Local-first device dashboard for pamoja: a node serves a hand-built, localized web UI over its own hotspot from a language-neutral state snapshot, fully offline.
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
//! The no-JavaScript floor: a server-rendered, meta-refreshing status table.
//!
//! The smallest tier serves the embedded [`lite.html`](../web/lite.html), which renders the
//! fleet with a tiny script. When scripting is off entirely, that page bounces to `GET
//! /lite`, served from here: the same readable status table built once on the device and
//! refreshed by a `<meta http-equiv="refresh">`, with no script at all. It is plain, but it
//! is legible and it works on any browser. This is the only place the device formats HTML at
//! runtime, kept to a single small table on purpose.

use crate::state::{Reading, State, Status};

/// How often the no-script page reloads itself, in seconds.
const REFRESH_SECS: u32 = 15;

/// Renders the fleet snapshot as a complete, self-contained, no-script HTML page.
///
/// The page carries its own styles inline and a `<meta http-equiv="refresh">`, so it needs
/// no other asset and stays current without any client code.
///
/// # Arguments
///
/// * `state` - the fleet snapshot to render.
///
/// # Returns
///
/// A full HTML document as a string.
pub(crate) fn render_lite(state: &State) -> String {
    let (word, sym, class) = status_bits(state.status);
    let mut out = String::with_capacity(2048);
    out.push_str(&page_head());
    out.push_str(&format!(
        "<header><h1>pamoja</h1><p class=\"status {class}\">{sym} {word}</p></header>\n"
    ));

    let mut any = false;
    for org in &state.orgs {
        any = true;
        let org_status = org
            .groups
            .iter()
            .map(|group| group.status)
            .fold(Status::Ok, Status::worst);
        let (oword, osym, oclass) = status_bits(org_status);
        let groups = org.groups.len();
        out.push_str(&format!(
            "<section class=\"org\">\n\
             <h2>{name} <span class=\"badge {oclass}\">{osym} {oword}</span> \
             <span class=\"count\">{groups} group{gp}</span></h2>\n",
            name = esc(&org.name),
            gp = if groups == 1 { "" } else { "s" },
        ));
        for group in &org.groups {
            let (gword, gsym, gclass) = status_bits(group.status);
            let online = if group.link.online {
                "online"
            } else {
                "offline"
            };
            let sensors = group.sensors.iter().filter(|s| !s.reading.stat).count();
            out.push_str(&format!(
                "<section class=\"group\">\n\
                 <h3>{name} <span class=\"badge {gclass}\">{gsym} {gword}</span></h3>\n\
                 <p class=\"muted\">{kind} <span class=\"bars\">{bars}</span> \u{b7} {online} \u{b7} {sensors} sensor{sp}</p>\n",
                name = esc(&group.name),
                kind = esc(link_kind(group.link.kind)),
                bars = bars(group.link.strength),
                sp = if sensors == 1 { "" } else { "s" },
            ));
            if sensors > 0 {
                out.push_str(
                    "<table><thead><tr><th>Sensor</th><th class=\"reading\">Reading</th><th class=\"state\">Status</th></tr></thead><tbody>\n",
                );
                for sensor in group.sensors.iter().filter(|s| !s.reading.stat) {
                    let (sword, ssym, sclass) = status_bits(sensor.reading.status);
                    out.push_str(&format!(
                        "<tr><th scope=\"row\">{id}</th><td class=\"reading\">{reading}</td><td class=\"state {sclass}\">{ssym} {sword}</td></tr>\n",
                        id = esc(&sensor.id),
                        reading = reading_cell(&sensor.reading),
                    ));
                }
                out.push_str("</tbody></table>\n");
            }
            let stats: Vec<String> = group
                .sensors
                .iter()
                .filter(|s| s.reading.stat)
                .map(|s| format!("{} {}", esc(&label(&s.reading.key)), stat_text(&s.reading)))
                .collect();
            if !stats.is_empty() {
                out.push_str(&format!(
                    "<p class=\"muted stats\">{}</p>\n",
                    stats.join(" \u{b7} ")
                ));
            }
            out.push_str("</section>\n");
        }
        out.push_str("</section>\n");
    }
    if !any {
        out.push_str("<p class=\"muted\">No sensors are reporting yet.</p>\n");
    }

    out.push_str("<footer><a href=\"./\">Full dashboard</a></footer>\n</body></html>\n");
    out
}

/// Renders a minimal page for the rare case the snapshot cannot be read.
///
/// # Returns
///
/// A full HTML document reporting that the status is briefly unavailable.
pub(crate) fn render_unavailable() -> String {
    format!(
        "{}<header><h1>pamoja</h1><p class=\"status warn\">\u{25B2} Status unavailable</p></header>\n</body></html>\n",
        page_head()
    )
}

// The shared document head: doctype, meta-refresh, and the inline styles. The styles mirror
// lite.html so the scripted and no-script floors read the same.
fn page_head() -> String {
    format!(
        "<!doctype html>\n<html lang=\"en\" dir=\"ltr\">\n<head>\n\
         <meta charset=\"utf-8\" />\n\
         <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n\
         <meta name=\"color-scheme\" content=\"light dark\" />\n\
         <meta http-equiv=\"refresh\" content=\"{REFRESH_SECS}\" />\n\
         <title>pamoja status</title>\n<style>{STYLE}</style>\n</head>\n<body>\n",
    )
}

// Word, shape, and color class for a status: redundant encoding so it never relies on color.
fn status_bits(status: Status) -> (&'static str, &'static str, &'static str) {
    match status {
        Status::Alarm => ("ALARM", "\u{2715}", "alarm"),
        Status::Warn => ("WARN", "\u{25B2}", "warn"),
        Status::Ok => ("OK", "\u{2713}", "ok"),
    }
}

// The reading cell: the leaf of a discrete state code, or a rounded value with its unit
// abbreviated to a short symbol and set in a muted span, so the value reads first.
fn reading_cell(reading: &Reading) -> String {
    if let Some(state) = &reading.state {
        let leaf = state.rsplit('.').next().unwrap_or(state);
        return esc(leaf);
    }
    let rounded = (reading.value as f64 * 100.0).round() / 100.0;
    let unit = unit_symbol(&reading.unit);
    if unit.is_empty() {
        format!("{rounded}")
    } else {
        format!("{rounded} <span class=\"unit\">{}</span>", esc(unit))
    }
}

// Abbreviates a canonical unit name to its conventional short symbol, so a reading reads
// like an instrument ("6.79 °C", "5.14 L/min") rather than carrying a long wire label. An
// unmapped unit falls back to its raw name; a non-physical unit (count, record, state)
// renders as no symbol at all.
fn unit_symbol(unit: &str) -> &str {
    match unit {
        "celsius" => "\u{b0}C",
        "fahrenheit" => "\u{b0}F",
        "kelvin" => "K",
        "percent" => "%",
        "volt" => "V",
        "millivolt" => "mV",
        "ampere" => "A",
        "milliampere" => "mA",
        "watt" => "W",
        "kilowatt" => "kW",
        "watt_hour" => "Wh",
        "kilowatt_hour" => "kWh",
        "hectopascal" => "hPa",
        "pascal" => "Pa",
        "kilopascal" => "kPa",
        "meter" => "m",
        "millimeter" => "mm",
        "centimeter" => "cm",
        "kilometer" => "km",
        "meter_per_second" => "m/s",
        "kilometer_per_hour" => "km/h",
        "liter" => "L",
        "liter_per_minute" => "L/min",
        "liter_per_hour" => "L/h",
        "lux" => "lx",
        "decibel" => "dB",
        "hertz" => "Hz",
        "degree" => "\u{b0}",
        "count" | "record" | "state" => "",
        other => other,
    }
}

// A signal-strength meter drawn as filled and empty dots over 0..=4, so it reads without
// color and does not look like a sensor count the way a bare "3/4" can.
fn bars(strength: u8) -> String {
    let filled = usize::from(strength.min(4));
    (0..4)
        .map(|i| if i < filled { '\u{25CF}' } else { '\u{25CB}' })
        .collect()
}

// A node or network stat as short text for the stats line: the state leaf, or the bare
// rounded value with no unit, since a stat like a neighbour count reads better plain.
fn stat_text(reading: &Reading) -> String {
    if let Some(state) = &reading.state {
        let leaf = state.rsplit('.').next().unwrap_or(state);
        return esc(leaf);
    }
    let rounded = (reading.value as f64 * 100.0).round() / 100.0;
    format!("{rounded}")
}

// Turns a stable reading key into a short label for the stats line, such as
// `"messages_relayed"` into `"messages relayed"`.
fn label(key: &str) -> String {
    key.replace('_', " ")
}

// A short, stable English label for a link kind. The floor page is single-locale by design.
fn link_kind(kind: crate::state::LinkKind) -> &'static str {
    use crate::state::LinkKind::*;
    match kind {
        Lora => "LoRa",
        Wifi => "Wi-Fi",
        Cellular => "Cellular",
        NbIot => "NB-IoT",
        Satellite => "Satellite",
        Ethernet => "Ethernet",
        Mesh => "Mesh",
    }
}

// Escapes the characters that would otherwise break out of HTML text content.
fn esc(text: &str) -> String {
    text.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

const STYLE: &str = "\
:root{--bg:#f6f7fb;--card:#fff;--line:#c8cedd;--text:#11151f;--muted:#5a6478;--ok:#137a4b;--warn:#8a5a00;--alarm:#b3261e}\
@media(prefers-color-scheme:dark){:root{--bg:#0b0f1a;--card:#131a2a;--line:#2a3552;--text:#eef2fb;--muted:#98a3bd;--ok:#34d399;--warn:#fbbf24;--alarm:#f87171}}\
*{box-sizing:border-box}\
body{margin:0;padding:1rem;background:var(--bg);color:var(--text);font:16px/1.5 system-ui,-apple-system,Segoe UI,Roboto,sans-serif}\
header{display:flex;align-items:baseline;justify-content:space-between;gap:1rem;flex-wrap:wrap;margin-bottom:1rem}\
h1{margin:0;font-size:1.3rem;letter-spacing:.02em}\
h2{margin:1.6rem 0 .5rem;font-size:1.15rem;display:flex;align-items:center;gap:.5rem;flex-wrap:wrap;border-bottom:1px solid var(--line);padding-bottom:.35rem}\
h3{margin:.9rem 0 .35rem;font-size:1rem;display:flex;align-items:center;gap:.5rem;flex-wrap:wrap}\
.status{font-weight:700;font-size:1.05rem}\
.badge{font-size:.78rem;font-weight:700;padding:.15rem .5rem;border-radius:999px;border:1px solid currentColor}\
.ok{color:var(--ok)}.warn{color:var(--warn)}.alarm{color:var(--alarm)}\
.muted{color:var(--muted);font-size:.85rem;margin:.1rem 0}\
.count{color:var(--muted);font-size:.78rem;font-weight:400}\
.bars{letter-spacing:.12em}\
.stats{font-size:.8rem;margin-top:.35rem}\
section.group{margin:.2rem 0 .9rem}\
table{width:100%;table-layout:fixed;border-collapse:collapse;background:var(--card);border:1px solid var(--line);border-radius:10px;overflow:hidden}\
th,td{text-align:start;padding:.55rem .7rem;border-bottom:1px solid var(--line)}\
th.reading,td.reading,th.state,td.state{text-align:end}\
th.reading{width:34%}th.state{width:22%}\
.unit{color:var(--muted)}\
thead th{font-size:.75rem;text-transform:uppercase;letter-spacing:.04em;color:var(--muted)}\
tbody tr:last-child th,tbody tr:last-child td{border-bottom:0}\
tbody th{font-weight:600}\
td.ok,td.warn,td.alarm{font-weight:700;white-space:nowrap}\
footer{margin-top:1.5rem}a{color:inherit}";

#[cfg(test)]
mod tests {
    use super::*;
    // The demo fleet is used only by the two tests below, so its import and they are behind
    // the `mock` feature; the direct-`State` render tests run in the default build.
    #[cfg(feature = "mock")]
    use crate::{Mock, Scenario, StateSource};

    #[cfg(feature = "mock")]
    #[test]
    fn renders_a_complete_document_with_meta_refresh() {
        let html = render_lite(&Mock::new(Scenario::Normal).snapshot());
        assert!(html.starts_with("<!doctype html>"));
        assert!(html.contains("http-equiv=\"refresh\""));
        assert!(html.trim_end().ends_with("</html>"));
        // No script tag at all: this is the no-JavaScript floor.
        assert!(!html.contains("<script"));
    }

    #[cfg(feature = "mock")]
    #[test]
    fn an_alarm_fleet_shows_the_alarm_word_not_just_a_color() {
        let html = render_lite(&Mock::new(Scenario::Alarm).snapshot());
        assert!(
            html.contains("ALARM"),
            "the status word must be present, not color alone"
        );
    }

    #[test]
    fn a_reading_shows_its_rounded_value_and_unit() {
        let mut state = State {
            orgs: vec![crate::Org {
                id: "o".into(),
                name: "Clinic".into(),
                groups: vec![crate::Group {
                    id: "g".into(),
                    name: "Cold chain".into(),
                    link: crate::Link {
                        kind: crate::LinkKind::Lora,
                        strength: 3,
                        online: true,
                    },
                    status: Status::Ok,
                    sensors: vec![crate::Sensor::new(
                        "fridge-1",
                        Reading::new("temperature", 6.789, "celsius"),
                    )],
                    lat: None,
                    lon: None,
                }],
            }],
            status: Status::Ok,
            uptime_secs: None,
            demo: false,
        };
        state.recompute_status();
        let html = render_lite(&state);
        assert!(html.contains("Clinic"), "the org is a section header");
        assert!(html.contains("fridge-1"));
        assert!(
            html.contains("6.79") && html.contains("\u{b0}C"),
            "value rounds to two places with an abbreviated unit: {html}"
        );
        // The link line names the kind and the sensor count, drawn without a bare "3/4".
        assert!(html.contains("LoRa"));
        assert!(html.contains("online"));
        assert!(
            html.contains("1 sensor"),
            "one measurement is counted: {html}"
        );
    }

    #[test]
    fn stats_are_listed_apart_from_sensors_and_not_counted() {
        // A group with one real sensor and two node/network stats: the count is one sensor,
        // and the stats appear on their own line rather than as sensor rows.
        let mut state = State {
            orgs: vec![crate::Org {
                id: "o".into(),
                name: "Mesh org".into(),
                groups: vec![crate::Group {
                    id: "g".into(),
                    name: "Relay".into(),
                    link: crate::Link {
                        kind: crate::LinkKind::Mesh,
                        strength: 4,
                        online: true,
                    },
                    status: Status::Ok,
                    sensors: vec![
                        crate::Sensor::new(
                            "river-1",
                            Reading::new("river_level", 1800.0, "millimeter"),
                        ),
                        crate::Sensor::new(
                            "neigh",
                            Reading::new("neighbours", 5.0, "count").as_stat(),
                        ),
                        crate::Sensor::new("hops", Reading::new("hops", 3.0, "count").as_stat()),
                    ],
                    lat: None,
                    lon: None,
                }],
            }],
            status: Status::Ok,
            uptime_secs: None,
            demo: false,
        };
        state.recompute_status();
        let html = render_lite(&state);
        assert!(html.contains("1 sensor"), "only the measurement is counted");
        assert!(html.contains("river-1"), "the measurement is a sensor row");
        // The stats show on the stats line by their key, not as sensor rows.
        assert!(html.contains("class=\"muted stats\""));
        assert!(html.contains("neighbours 5"));
        assert!(html.contains("hops 3"));
        assert!(
            !html.contains("scope=\"row\">neigh<"),
            "a stat is not a sensor row: {html}"
        );
    }

    #[test]
    fn names_are_html_escaped() {
        let html = render_lite(&State {
            orgs: vec![crate::Org {
                id: "o".into(),
                name: "Org".into(),
                groups: vec![crate::Group {
                    id: "g".into(),
                    name: "Acme & Co <x>".into(),
                    link: crate::Link {
                        kind: crate::LinkKind::Wifi,
                        strength: 4,
                        online: true,
                    },
                    status: Status::Ok,
                    sensors: Vec::new(),
                    lat: None,
                    lon: None,
                }],
            }],
            status: Status::Ok,
            uptime_secs: None,
            demo: false,
        });
        assert!(!html.contains("Acme & Co <x>"));
        assert!(html.contains("Acme &amp; Co &lt;x&gt;"));
    }

    #[test]
    fn a_discrete_state_shows_its_leaf() {
        let reading = Reading::new("valve", 0.0, "").with_state("state.open");
        assert_eq!(reading_cell(&reading), "open");
    }

    #[test]
    fn a_unit_is_abbreviated_to_its_symbol() {
        assert_eq!(
            reading_cell(&Reading::new("t", 5.0, "celsius")),
            "5 <span class=\"unit\">\u{b0}C</span>"
        );
        assert_eq!(
            reading_cell(&Reading::new("f", 5.14, "liter_per_minute")),
            "5.14 <span class=\"unit\">L/min</span>"
        );
    }
}