rigtop-core 0.19.0

Data collection core of rigtop: CPU, memory, GPU (NVML), sensors, storage health (SMART, btrfs, mdadm), network and processes as serializable snapshots
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
//! Front-end helpers shared by the TUI and the GUI: graph history, process
//! table sorting/filtering joined with GPU usage, and small naming helpers.
//!
//! Nothing here depends on a UI toolkit.

use std::collections::{HashMap, HashSet, VecDeque};
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::PathBuf;
use std::time::{Duration, SystemTime};

use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};

use crate::config::AlertsConfig;
use crate::format;
use crate::snapshot::{GpuStats, OomKill, PcieLink, ProcessInfo, Snapshot};
use crate::source::ProcessSignal;

/// Number of samples kept for graphs: over an hour at the default interval
/// of 1 s, and wider than any realistic terminal in braille dots (2 samples
/// per cell).
pub const HISTORY_LEN: usize = 4096;

/// Interface name prefixes skipped when choosing the default network
/// interface (container bridges and virtual links).
const VIRTUAL_IFACE_PREFIXES: &[&str] = &["lo", "docker", "veth", "br-", "virbr"];

/// Samples averaged into one point of the coarse history (a minute at the
/// default interval of 1 s).
pub const COARSE_FACTOR: usize = 60;
/// Points kept in the coarse history (a day at the default interval).
pub const COARSE_LEN: usize = 1440;

/// Fixed-size history of samples, newest last, plus a coarse history of
/// averages for long time spans.
#[derive(Debug, Default, Clone)]
pub struct History {
    samples: VecDeque<f64>,
    coarse: VecDeque<f64>,
    /// Sum and count of the samples not yet averaged into `coarse`.
    pending: (f64, usize),
}

impl History {
    /// Appends a sample, dropping the oldest when full.
    pub fn push(&mut self, value: f64) {
        if self.samples.len() == HISTORY_LEN {
            self.samples.pop_front();
        }
        self.samples.push_back(value);
        self.pending.0 += value;
        self.pending.1 += 1;
        if self.pending.1 == COARSE_FACTOR {
            if self.coarse.len() == COARSE_LEN {
                self.coarse.pop_front();
            }
            self.coarse.push_back(self.pending.0 / COARSE_FACTOR as f64);
            self.pending = (0.0, 0);
        }
    }

    /// The newest `n` samples (or fewer), oldest first.
    pub fn last(&self, n: usize) -> Vec<f64> {
        let skip = self.samples.len().saturating_sub(n);
        self.samples.iter().skip(skip).copied().collect()
    }

    /// Largest of the newest `n` samples.
    pub fn max_of_last(&self, n: usize) -> f64 {
        self.last(n).into_iter().fold(0.0, f64::max)
    }

    /// Values for a graph `columns` wide, oldest first. [`GraphSpan::Fit`]
    /// gives the newest sample per column; a time span gives the samples of
    /// that span (one every `interval`) spread over the columns, averaged
    /// where several share a column and interpolated where there are fewer
    /// samples than columns. While the history is shorter than the span,
    /// fewer values come back, so the graph fills up from the right.
    pub fn graph(&self, columns: usize, span: GraphSpan, interval: Duration) -> Vec<f64> {
        let GraphSpan::Secs(secs) = span else {
            return self.last(columns);
        };
        let wanted = ((secs as f64 / interval.as_secs_f64().max(0.001)).round() as usize).max(1);
        if wanted <= HISTORY_LEN {
            spread(&self.last(wanted), wanted, columns)
        } else {
            // Longer than the samples kept: from the averages.
            let wanted = wanted.div_ceil(COARSE_FACTOR);
            let skip = self.coarse.len().saturating_sub(wanted);
            let points: Vec<f64> = self.coarse.iter().skip(skip).copied().collect();
            spread(&points, wanted, columns)
        }
    }
}

/// `samples` (the newest of `wanted`) spread over `columns`: averaged where
/// several share a column, interpolated where there are fewer samples than
/// columns, and over a proportional share of the columns at the right while
/// fewer than `wanted` exist.
fn spread(samples: &[f64], wanted: usize, columns: usize) -> Vec<f64> {
    if columns == 0 || samples.is_empty() {
        return Vec::new();
    }
    // The share of the columns that the available samples cover.
    let shown = (columns as f64 * samples.len() as f64 / wanted.max(1) as f64).round() as usize;
    let shown = shown.clamp(1, columns);
    let per_column = samples.len() as f64 / shown as f64;
    if per_column < 1.0 {
        // Stretched: a straight line between neighbouring samples.
        let last = samples.len() - 1;
        return (0..shown)
            .map(|i| {
                let at = ((i as f64 + 0.5) * per_column - 0.5).clamp(0.0, last as f64);
                let (below, frac) = (at.floor() as usize, at.fract());
                let above = (below + 1).min(last);
                samples[below] * (1.0 - frac) + samples[above] * frac
            })
            .collect();
    }
    (0..shown)
        .map(|i| {
            let end = (((i + 1) as f64 * per_column) as usize).clamp(1, samples.len());
            let start = ((i as f64 * per_column) as usize).min(end - 1);
            let part = &samples[start..end];
            part.iter().sum::<f64>() / part.len() as f64
        })
        .collect()
}

/// How much time the graphs cover.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GraphSpan {
    /// One sample per graph column, so the time covered grows with the
    /// width and the interval.
    #[default]
    Fit,
    /// The last this many seconds, spread over the width.
    Secs(u64),
}

impl GraphSpan {
    /// The choices [`GraphSpan::next`] steps through.
    pub const CHOICES: [GraphSpan; 7] = [
        GraphSpan::Fit,
        GraphSpan::Secs(60),
        GraphSpan::Secs(5 * 60),
        GraphSpan::Secs(15 * 60),
        GraphSpan::Secs(60 * 60),
        GraphSpan::Secs(6 * 60 * 60),
        GraphSpan::Secs(24 * 60 * 60),
    ];

    /// The choice after this one, back to the first after the last.
    pub fn next(self) -> Self {
        let index = Self::CHOICES
            .iter()
            .position(|c| *c == self)
            .map_or(0, |i| i + 1);
        Self::CHOICES[index % Self::CHOICES.len()]
    }

    /// Short name: "fit", "1m", "1h".
    pub fn label(self) -> String {
        match self {
            GraphSpan::Fit => "fit".to_owned(),
            GraphSpan::Secs(secs) if secs > 0 && secs % 3600 == 0 => format!("{}h", secs / 3600),
            GraphSpan::Secs(secs) if secs > 0 && secs % 60 == 0 => format!("{}m", secs / 60),
            GraphSpan::Secs(secs) => format!("{secs}s"),
        }
    }

    /// Seconds covered, 0 for [`GraphSpan::Fit`] (for storing the choice).
    pub fn as_secs(self) -> u64 {
        match self {
            GraphSpan::Fit => 0,
            GraphSpan::Secs(secs) => secs,
        }
    }

    /// The inverse of [`GraphSpan::as_secs`].
    pub fn from_secs(secs: u64) -> Self {
        if secs == 0 {
            GraphSpan::Fit
        } else {
            GraphSpan::Secs(secs)
        }
    }
}

/// Columns the process table can be sorted by.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortKey {
    Pid,
    Name,
    Threads,
    User,
    /// Container name; processes outside containers last.
    Container,
    Memory,
    Cpu,
    /// Disk reads plus writes.
    Disk,
    Gpu,
    Vram,
}

impl SortKey {
    /// Sort keys in column order, used to step left/right.
    pub const ALL: [SortKey; 10] = [
        SortKey::Pid,
        SortKey::Name,
        SortKey::Threads,
        SortKey::User,
        SortKey::Container,
        SortKey::Memory,
        SortKey::Cpu,
        SortKey::Disk,
        SortKey::Gpu,
        SortKey::Vram,
    ];

    /// Whether "descending" is the natural order (largest consumers first).
    pub fn descending_by_default(self) -> bool {
        !matches!(
            self,
            SortKey::Pid | SortKey::Name | SortKey::User | SortKey::Container
        )
    }
}

/// One row of the process table: the process plus its GPU usage.
#[derive(Debug, Clone)]
pub struct ProcessRow<'a> {
    pub info: &'a ProcessInfo,
    /// Highest per-GPU SM utilization of the process.
    pub gpu_percent: Option<f32>,
    /// VRAM used by the process across all GPUs.
    pub vram_bytes: Option<u64>,
    /// In the tree view, the lines leading to the process ("│ ├ "), to be
    /// put before its name; empty otherwise.
    pub tree_prefix: String,
}

/// Graph histories of one host, updated from each snapshot.
#[derive(Debug, Default, Clone)]
pub struct MetricHistory {
    /// Total CPU usage (%).
    pub cpu: History,
    /// Used memory (% of total).
    pub memory: History,
    /// Utilization (%) per GPU index.
    pub gpus: Vec<History>,
    /// Receive rate of `net_interface` (bytes/s).
    pub net_rx: History,
    /// Transmit rate of `net_interface` (bytes/s).
    pub net_tx: History,
    /// Interface whose traffic is recorded (`None`: chosen on the next snapshot).
    pub net_interface: Option<String>,
}

impl MetricHistory {
    /// Appends the values of a new snapshot.
    pub fn record(&mut self, snapshot: &Snapshot) {
        self.cpu.push(snapshot.cpu.total_percent as f64);
        let memory = &snapshot.memory;
        if memory.total_bytes > 0 {
            self.memory
                .push(memory.used_bytes as f64 / memory.total_bytes as f64 * 100.0);
        }
        self.gpus.resize_with(snapshot.gpus.len(), History::default);
        for (history, gpu) in self.gpus.iter_mut().zip(&snapshot.gpus) {
            history.push(gpu.utilization_percent.unwrap_or(0.0) as f64);
        }
        if self.net_interface.is_none() {
            self.net_interface = default_interface(snapshot);
        }
        let (rx, tx) = self
            .net_interface
            .as_deref()
            .and_then(|name| snapshot.networks.iter().find(|n| n.name == name))
            .map(|n| (n.rx_bytes_per_sec, n.tx_bytes_per_sec))
            .unwrap_or((0.0, 0.0));
        self.net_rx.push(rx);
        self.net_tx.push(tx);
    }

    /// Switches the recorded network interface, clearing its history.
    pub fn select_interface(&mut self, name: &str) {
        if self.net_interface.as_deref() != Some(name) {
            self.net_interface = Some(name.to_owned());
            // The old samples belong to another interface.
            self.net_rx = History::default();
            self.net_tx = History::default();
        }
    }

    /// Switches to the interface after the current one in `snapshot` and
    /// returns its name.
    pub fn next_interface(&mut self, snapshot: &Snapshot) -> Option<String> {
        let names: Vec<&str> = snapshot.networks.iter().map(|n| n.name.as_str()).collect();
        if names.is_empty() {
            return None;
        }
        let current = self
            .net_interface
            .as_deref()
            .and_then(|name| names.iter().position(|n| *n == name));
        let next = names[current.map_or(0, |i| (i + 1) % names.len())].to_owned();
        self.select_interface(&next);
        Some(next)
    }
}

/// How urgent an [`Alert`] is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    /// Worth a look (e.g. a filesystem filling up, a RAID resync).
    Warning,
    /// Data at risk (errors, a missing device, a failing drive).
    Critical,
}

/// A condition on a host that needs attention, for overviews.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Alert {
    pub severity: Severity,
    pub text: String,
}

/// Identifies an alert across samples: its severity and text with numbers
/// masked, so "3 I/O errors" becoming "4 I/O errors" or a temperature
/// changing does not count as a new alert.
pub fn alert_key(alert: &Alert) -> String {
    let mut key = format!("{:?}:", alert.severity);
    let mut in_number = false;
    for c in alert.text.chars() {
        if c.is_ascii_digit() || (in_number && c == '.') {
            if !in_number {
                key.push('#');
            }
            in_number = true;
        } else {
            key.push(c);
            in_number = false;
        }
    }
    key
}

/// Whether an [`AlertEvent`] is an alert appearing or going away, or
/// something that happened once.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AlertChange {
    Raised,
    Cleared,
    /// A one-off event, not a lasting condition (a watched process exited):
    /// never cleared. Older rigtops skip these lines of a saved log.
    Notice,
}

/// One entry of the [`AlertLog`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AlertEvent {
    /// When the change was seen, in milliseconds since the Unix epoch.
    pub at_ms: u64,
    pub host: String,
    pub change: AlertChange,
    /// The alert as raised, or as last seen before it cleared.
    pub alert: Alert,
}

/// Events kept by an [`AlertLog`].
const ALERT_LOG_LEN: usize = 500;

/// The alerts of every host as they appear and clear, so that what
/// happened while nobody watched can be looked up later.
#[derive(Debug, Default)]
pub struct AlertLog {
    /// Newest last.
    events: VecDeque<AlertEvent>,
    /// Each host's alerts at its previous update.
    active: HashMap<String, Vec<Alert>>,
    /// Alerts raised since [`AlertLog::mark_read`], and the worst of them.
    unread: (usize, Option<Severity>),
    /// Where events are kept between runs (JSON lines), if anywhere.
    file: Option<PathBuf>,
}

/// Where a front-end ("tui", "gui") keeps its alert history: the state
/// directory (`~/.local/state/rigtop/` on Linux), else the local data
/// directory (macOS, Windows).
pub fn alert_log_path(front_end: &str) -> Option<PathBuf> {
    dirs::state_dir()
        .or_else(dirs::data_local_dir)
        .map(|dir| dir.join("rigtop").join(format!("alerts-{front_end}.jsonl")))
}

impl AlertLog {
    /// A log kept in `path`: the events of earlier runs are loaded, alerts
    /// they left raised count as still present (so a restart does not log
    /// them again), and new events are appended. A file that cannot be
    /// read or written leaves the log working in memory.
    pub fn persistent(path: PathBuf) -> Self {
        let mut log = Self::default();
        let loaded: Vec<AlertEvent> = fs::read_to_string(&path)
            .map(|text| {
                text.lines()
                    .filter_map(|line| serde_json::from_str(line).ok())
                    .collect()
            })
            .unwrap_or_default();
        let keep = loaded.len().saturating_sub(ALERT_LOG_LEN);
        for event in loaded.iter().filter(|e| e.change != AlertChange::Notice) {
            let active = log.active.entry(event.host.clone()).or_default();
            let key = alert_key(&event.alert);
            active.retain(|a| alert_key(a) != key);
            if event.change == AlertChange::Raised {
                active.push(event.alert.clone());
            }
        }
        log.events = loaded.into_iter().skip(keep).collect();
        // Rewrite a file grown well past what is shown.
        if keep > ALERT_LOG_LEN {
            let text: String = log
                .events
                .iter()
                .filter_map(|e| serde_json::to_string(e).ok())
                .map(|line| line + "\n")
                .collect();
            let tmp = path.with_extension("jsonl.tmp");
            let _ = fs::write(&tmp, text).and_then(|()| fs::rename(&tmp, &path));
        }
        log.file = Some(path);
        log
    }

    /// Appends `event` to the file, if the log has one.
    fn save(&self, event: &AlertEvent) {
        let Some(path) = &self.file else {
            return;
        };
        let Ok(line) = serde_json::to_string(event) else {
            return;
        };
        if let Some(dir) = path.parent() {
            let _ = fs::create_dir_all(dir);
        }
        // One write per line, so that lines from concurrent runs never mix.
        let _ = OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)
            .and_then(|mut file| file.write_all(format!("{line}\n").as_bytes()));
    }

    /// Records how the alerts of `host` changed since its previous update,
    /// and returns those changes.
    pub fn update(&mut self, host: &str, alerts: &[Alert], at_ms: u64) -> Vec<AlertEvent> {
        let previous = self.active.remove(host).unwrap_or_default();
        let keys = |list: &[Alert]| -> HashSet<String> { list.iter().map(alert_key).collect() };
        let (before, now) = (keys(&previous), keys(alerts));
        let raised = alerts.iter().filter(|a| !before.contains(&alert_key(a)));
        let cleared = previous.iter().filter(|a| !now.contains(&alert_key(a)));
        let mut changes: Vec<(AlertChange, &Alert)> = raised
            .map(|a| (AlertChange::Raised, a))
            .chain(cleared.map(|a| (AlertChange::Cleared, a)))
            .collect();
        // Cleared first, so that a changed alert reads "went away, came back".
        changes.sort_by_key(|(change, _)| *change == AlertChange::Raised);
        let mut new_events = Vec::with_capacity(changes.len());
        for (change, alert) in changes {
            new_events.push(self.push(AlertEvent {
                at_ms,
                host: host.to_owned(),
                change,
                alert: alert.clone(),
            }));
        }
        self.active.insert(host.to_owned(), alerts.to_vec());
        new_events
    }

    /// Records a one-off event of `host` ([`AlertChange::Notice`]), such as
    /// a watched process exiting, and returns it.
    pub fn record(&mut self, host: &str, alert: Alert, at_ms: u64) -> AlertEvent {
        self.push(AlertEvent {
            at_ms,
            host: host.to_owned(),
            change: AlertChange::Notice,
            alert,
        })
    }

    /// Adds `event` to the log (and its file), counting it as unread unless
    /// it is a clearing.
    fn push(&mut self, event: AlertEvent) -> AlertEvent {
        if event.change != AlertChange::Cleared {
            self.unread.0 += 1;
            self.unread.1 = self.unread.1.max(Some(event.alert.severity));
        }
        if self.events.len() == ALERT_LOG_LEN {
            self.events.pop_front();
        }
        self.save(&event);
        self.events.push_back(event.clone());
        event
    }

    /// All events, oldest first.
    pub fn events(&self) -> impl DoubleEndedIterator<Item = &AlertEvent> {
        self.events.iter()
    }

    /// Alerts raised since the log was last read, and the most severe one.
    pub fn unread(&self) -> (usize, Option<Severity>) {
        self.unread
    }

    /// Marks every event as read.
    pub fn mark_read(&mut self) {
        self.unread = (0, None);
    }
}

/// When an [`AlertEvent`] happened, as short as is clear: "14:03:12"
/// today, "Tue 14:03" this week, "Oct 21 14:03" before.
pub fn event_time(at_ms: u64, now: SystemTime) -> String {
    let at: DateTime<Local> = (SystemTime::UNIX_EPOCH + Duration::from_millis(at_ms)).into();
    let today: DateTime<Local> = now.into();
    let days = (today.date_naive() - at.date_naive()).num_days();
    if days == 0 {
        at.format("%H:%M:%S").to_string()
    } else if (1..6).contains(&days) {
        at.format("%a %H:%M").to_string()
    } else {
        at.format("%b %d %H:%M").to_string()
    }
}

/// A process to announce when it exits.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WatchedProcess {
    pub pid: u32,
    /// Its name when the watch began; another name under the same pid
    /// means it exited and the pid was reused.
    pub name: String,
    /// When it started (milliseconds since the Unix epoch, by the host's
    /// run time).
    pub started_ms: u64,
}

/// What is known about a [`WatchedProcess`] from a snapshot.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WatchState {
    Running,
    Exited,
    /// The source does not report it (yet): an agent before 0.19, or the
    /// request has not reached the source.
    Unknown,
}

/// The processes of one host to announce when they exit. The front-end
/// passes [`ExitWatch::pids`] to the source
/// ([`crate::DataSource::watch_processes`]), which then keeps them in
/// snapshots whatever the process limit.
#[derive(Debug, Clone, Default)]
pub struct ExitWatch {
    processes: Vec<WatchedProcess>,
}

impl ExitWatch {
    /// Whether `pid` is watched.
    pub fn contains(&self, pid: u32) -> bool {
        self.processes.iter().any(|w| w.pid == pid)
    }

    /// Starts or stops watching `process`; true when it is now watched.
    pub fn toggle(&mut self, process: &ProcessInfo, now_ms: u64) -> bool {
        if self.contains(process.pid) {
            self.processes.retain(|w| w.pid != process.pid);
            return false;
        }
        self.processes.push(WatchedProcess {
            pid: process.pid,
            name: process.name.clone(),
            started_ms: now_ms.saturating_sub(process.run_time_secs * 1000),
        });
        true
    }

    /// The watched pids, for the source.
    pub fn pids(&self) -> Vec<u32> {
        self.processes.iter().map(|w| w.pid).collect()
    }

    /// What `snapshot` says about the watched process `pid`.
    pub fn state(&self, snapshot: &Snapshot, pid: u32) -> WatchState {
        match self.processes.iter().find(|w| w.pid == pid) {
            Some(watched) => watch_state(snapshot, watched),
            None => WatchState::Unknown,
        }
    }

    /// Stops watching the processes that exited by `snapshot` and returns
    /// an alert for each: "train.py (pid 1234) exited after 3h12m".
    pub fn check(&mut self, snapshot: &Snapshot, now_ms: u64) -> Vec<Alert> {
        let mut exited = Vec::new();
        self.processes.retain(|watched| {
            if watch_state(snapshot, watched) != WatchState::Exited {
                return true;
            }
            let ran = now_ms.saturating_sub(watched.started_ms) / 1000;
            exited.push(Alert {
                severity: Severity::Warning,
                text: format!(
                    "{} (pid {}) exited after {}",
                    watched.name,
                    watched.pid,
                    format::duration(ran)
                ),
            });
            false
        });
        exited
    }
}

/// Whether `watched` still runs according to `snapshot`. Running only
/// when the source reports it, since only then will its exit be seen.
fn watch_state(snapshot: &Snapshot, watched: &WatchedProcess) -> WatchState {
    let reported = snapshot.watched_pids.contains(&watched.pid);
    match snapshot.processes.iter().find(|p| p.pid == watched.pid) {
        // The pid now belongs to another program.
        Some(process) if process.name != watched.name => WatchState::Exited,
        Some(_) if reported => WatchState::Running,
        None if reported => WatchState::Exited,
        _ => WatchState::Unknown,
    }
}

/// What a process detail view shows, as label and value rows: who started
/// it, who runs it, since when, what it uses, and its whole command line.
/// `None` once the process is gone.
pub fn process_details(
    snapshot: &Snapshot,
    pid: u32,
    now: SystemTime,
) -> Option<Vec<(&'static str, String)>> {
    let by_pid: HashMap<u32, &ProcessInfo> =
        snapshot.processes.iter().map(|p| (p.pid, p)).collect();
    let process = by_pid.get(&pid)?;
    let mut rows = vec![("Program", format!("{} (pid {})", process.name, process.pid))];

    // Parents up to the first process, oldest first; bounded in case of a
    // loop in inconsistent data.
    let mut chain = Vec::new();
    let mut parent = process.parent_pid;
    while let Some(ppid) = parent.filter(|_| chain.len() < 32) {
        match by_pid.get(&ppid) {
            Some(p) => {
                chain.push(format!("{} ({})", p.name, p.pid));
                parent = p.parent_pid.filter(|&next| next != p.pid);
            }
            None => {
                chain.push(ppid.to_string());
                break;
            }
        }
    }
    if !chain.is_empty() {
        chain.reverse();
        rows.push(("Parents", chain.join(" › ")));
    }
    let children = snapshot
        .processes
        .iter()
        .filter(|p| p.parent_pid == Some(pid))
        .count();
    if children > 0 {
        rows.push(("Children", children.to_string()));
    }
    rows.push(("User", process.user.clone()));
    if let Some(container) = &process.container {
        rows.push(("Container", container.clone()));
    }
    if !process.state.is_empty() {
        rows.push(("State", process.state.clone()));
    }
    let started = now - Duration::from_secs(process.run_time_secs);
    rows.push(("Running", since_note(started, now)));
    rows.push(("CPU", format!("{:.1}%", process.cpu_percent)));
    rows.push(("Memory", format::bytes(process.memory_bytes)));
    if let Some(threads) = process.threads {
        rows.push(("Threads", threads.to_string()));
    }
    rows.push((
        "Disk",
        format!(
            "read {}, write {}",
            format::rate(process.read_bytes_per_sec),
            format::rate(process.write_bytes_per_sec)
        ),
    ));
    for gpu in &snapshot.gpus {
        if let Some(usage) = gpu.processes.iter().find(|p| p.pid == pid) {
            let mut text = format!("GPU {}", gpu.index);
            if let Some(sm) = usage.sm_percent {
                text.push_str(&format!(" {sm:.0}%"));
            }
            if let Some(memory) = usage.memory_bytes {
                text.push_str(&format!(", {} VRAM", format::bytes(memory)));
            }
            rows.push(("GPU", text));
        }
    }
    let command = if process.command.is_empty() {
        format!("[{}]", process.name)
    } else {
        process.command.clone()
    };
    rows.push(("Command", command));
    Some(rows)
}

/// Where a host goes in an overview sorted by attention: 0 for one that is
/// unreachable or has a critical alert, 1 for one with stale data or a
/// warning, 2 for the rest.
pub fn attention_rank(alerts: &[Alert], unreachable: bool, stale: bool) -> u8 {
    let worst = alerts.iter().map(|a| a.severity).max();
    if unreachable || worst == Some(Severity::Critical) {
        0
    } else if stale || worst == Some(Severity::Warning) {
        1
    } else {
        2
    }
}

/// Short notes for a GPU's title line, with how much attention each needs
/// (`None`: just information): running at its power limit, slowed down by
/// the hardware, ECC memory errors.
pub fn gpu_notes(gpu: &GpuStats) -> Vec<(String, Option<Severity>)> {
    let mut notes = Vec::new();
    let slowdown: Vec<&str> = gpu.slowdown_reasons().collect();
    if !slowdown.is_empty() {
        notes.push((
            format!("slowed: {}", slowdown.join(", ")),
            Some(Severity::Warning),
        ));
    } else if !gpu.throttle_reasons.is_empty() {
        notes.push(("power cap".to_owned(), None));
    }
    if let Some(ecc) = gpu.ecc {
        if ecc.uncorrected > 0 {
            notes.push((
                format!("ECC {} uncorrected", ecc.uncorrected),
                Some(Severity::Critical),
            ));
        } else if ecc.corrected > 0 {
            notes.push((format!("ECC {} corrected", ecc.corrected), None));
        }
    }
    notes
}

/// A GPU's PCIe link for a tooltip: "PCIe gen 4 x8 (up to gen 4 x16)".
pub fn pcie_text(link: &PcieLink) -> String {
    let now = format!("PCIe gen {} x{}", link.generation, link.width);
    if (link.generation, link.width) == (link.max_generation, link.max_width) {
        now
    } else {
        format!(
            "{now} (up to gen {} x{}; the link slows down when idle)",
            link.max_generation, link.max_width
        )
    }
}

/// How long it has been since `since` (e.g. a host's last data) and when
/// that was, in local time: "5m (since 14:03)", "2d03h (since Tue 14:03)".
pub fn since_note(since: SystemTime, now: SystemTime) -> String {
    let secs = now.duration_since(since).map_or(0, |d| d.as_secs());
    let span = match secs {
        0..60 => format!("{secs}s"),
        60..3_600 => format!("{}m", secs / 60),
        3_600..86_400 => format!("{}h{:02}m", secs / 3_600, secs % 3_600 / 60),
        _ => format!("{}d{:02}h", secs / 86_400, secs % 86_400 / 3_600),
    };
    let at: DateTime<Local> = since.into();
    let today: DateTime<Local> = now.into();
    let clock = if at.date_naive() == today.date_naive() {
        at.format("%H:%M")
    } else if secs < 6 * 86_400 {
        at.format("%a %H:%M")
    } else {
        at.format("%b %d %H:%M")
    };
    format!("{span} (since {clock})")
}

/// "agent 0.8.2" when `snapshot` comes from a rigtop of another version
/// than this one (an agent not updated yet), else `None`.
pub fn version_note(snapshot: &Snapshot) -> Option<String> {
    let version = &snapshot.host.rigtop_version;
    (!version.is_empty() && version != env!("CARGO_PKG_VERSION"))
        .then(|| format!("agent {version}"))
}

/// Hover text explaining a [`version_note`].
pub fn version_hint(snapshot: &Snapshot) -> String {
    format!(
        "this host runs rigtop {}, this machine {}: install the same version on both \
         for every reading",
        snapshot.host.rigtop_version,
        env!("CARGO_PKG_VERSION")
    )
}

/// Shown next to the uptime of a host that needs a reboot to finish an
/// update ([`crate::snapshot::HostInfo::reboot_required`]).
pub const REBOOT_NOTE: &str = "reboot needed";

/// Hover text for [`REBOOT_NOTE`], with the reason when known.
pub fn reboot_hint(reason: &str) -> String {
    let mut hint = "a reboot is needed to finish an update".to_owned();
    if !reason.is_empty() {
        hint.push_str(&format!(" ({reason})"));
    }
    hint
}

/// [`alerts_with`] the default thresholds.
pub fn alerts(snapshot: &Snapshot) -> Vec<Alert> {
    alerts_with(snapshot, &AlertsConfig::default())
}

/// Conditions in `snapshot` that need attention, most severe first:
/// storage errors and failing drives, degraded RAID, full filesystems, OOM
/// kills, full memory, hot CPUs, GPUs and drives, judged by `limits` (0
/// turns a threshold off).
pub fn alerts_with(snapshot: &Snapshot, limits: &AlertsConfig) -> Vec<Alert> {
    // A threshold of 0 (or less) is turned off.
    let reached = |value: f64, limit: f64| limit > 0.0 && value >= limit;
    let mut alerts = Vec::new();
    let mut add = |severity, text: String| alerts.push(Alert { severity, text });
    for fs in &snapshot.raid.btrfs {
        let name = fs
            .mount_points
            .first()
            .map_or(fs.fsid.as_str(), String::as_str);
        let missing = fs.device_stats.iter().filter(|d| d.missing).count();
        if missing > 0 {
            add(
                Severity::Critical,
                format!("btrfs {name}: {missing} device(s) missing"),
            );
        }
        let errors: u64 = fs.device_stats.iter().map(|d| d.total_errors()).sum();
        if errors > 0 {
            // Which drives: the one to replace.
            let devices: Vec<String> = fs
                .device_stats
                .iter()
                .filter(|d| d.total_errors() > 0)
                .map(|d| d.label())
                .collect();
            add(
                Severity::Critical,
                format!("btrfs {name}: {errors} I/O errors ({})", devices.join(", ")),
            );
        }
        if let Some(scrub) = &fs.scrub {
            if scrub.uncorrectable > 0 {
                add(
                    Severity::Critical,
                    format!(
                        "btrfs {name}: scrub found {} uncorrectable errors",
                        scrub.uncorrectable
                    ),
                );
            } else if scrub.errors > 0 {
                add(
                    Severity::Warning,
                    format!("btrfs {name}: scrub corrected {} errors", scrub.corrected),
                );
            }
        }
    }
    for md in &snapshot.raid.md {
        if md.active && md.is_degraded() {
            add(
                Severity::Critical,
                format!("{} ({}) degraded", md.name, md.level),
            );
        } else if !md.active {
            add(Severity::Warning, format!("{} inactive", md.name));
        }
        if let Some(sync) = &md.sync {
            add(
                Severity::Warning,
                format!("{} {} {:.0}%", md.name, sync.action, sync.percent),
            );
        }
    }
    for pool in &snapshot.raid.zfs {
        if pool.is_failing() {
            add(
                Severity::Critical,
                format!("zfs {} {}", pool.name, pool.state),
            );
        } else if !pool.is_online() {
            add(
                Severity::Warning,
                format!("zfs {} {}", pool.name, pool.state),
            );
        }
    }
    for unit in &snapshot.failed_units {
        if !limits.ignored_units.contains(unit) {
            add(Severity::Warning, format!("systemd: {unit} failed"));
        }
    }
    for smart in &snapshot.smart {
        if let Some(error) = &smart.error {
            add(
                Severity::Warning,
                format!("{}: SMART unreadable ({error})", smart.device),
            );
        } else if smart.passed == Some(false) {
            add(
                Severity::Critical,
                format!("{}: SMART failed", smart.device),
            );
        } else if smart.needs_attention() {
            add(
                Severity::Critical,
                format!("{}: SMART warning", smart.device),
            );
        }
    }
    for fs in &snapshot.filesystems {
        let used = fs.total_bytes.saturating_sub(fs.available_bytes);
        let percent = used as f64 / fs.total_bytes.max(1) as f64 * 100.0;
        let name = fs
            .mount_points
            .first()
            .map_or(fs.device.as_str(), String::as_str);
        if reached(percent, limits.filesystem_critical_percent) {
            add(Severity::Critical, format!("{name} {percent:.0}% full"));
        } else if reached(percent, limits.filesystem_warning_percent) {
            add(Severity::Warning, format!("{name} {percent:.0}% full"));
        }
        let soon = limits.filesystem_full_warning_hours * 3600.0;
        if let Some(secs) = fs.full_in_secs
            && soon > 0.0
            && secs as f64 <= soon
        {
            add(
                Severity::Warning,
                format!("{name} full in ~{}", format::duration(secs)),
            );
        }
    }
    if let Some(text) = oom_alert(&snapshot.memory.oom_kills) {
        add(Severity::Critical, text);
    }
    let memory = &snapshot.memory;
    let percent = memory.used_bytes as f64 / memory.total_bytes.max(1) as f64 * 100.0;
    if memory.total_bytes > 0 && reached(percent, limits.memory_warning_percent) {
        add(Severity::Warning, format!("memory {percent:.0}% used"));
    }
    let hottest_cpu = snapshot
        .cpu
        .temperatures
        .iter()
        .map(|t| t.celsius)
        .fold(f32::NAN, f32::max);
    if reached(hottest_cpu as f64, limits.cpu_warning_celsius as f64) {
        add(Severity::Warning, format!("CPU at {hottest_cpu:.0}°C"));
    }
    for gpu in &snapshot.gpus {
        if let Some(t) = gpu
            .temperature_c
            .filter(|t| reached(*t as f64, limits.gpu_warning_celsius as f64))
        {
            add(Severity::Warning, format!("GPU {} at {t:.0}°C", gpu.index));
        }
        if let (Some(used), Some(total)) = (gpu.memory_used_bytes, gpu.memory_total_bytes)
            && total > 0
        {
            let percent = used as f64 / total as f64 * 100.0;
            if reached(percent, limits.gpu_memory_warning_percent) {
                add(
                    Severity::Warning,
                    format!("GPU {} memory {percent:.0}% used", gpu.index),
                );
            }
        }
        let slowdown: Vec<&str> = gpu.slowdown_reasons().collect();
        if !slowdown.is_empty() {
            add(
                Severity::Warning,
                format!("GPU {} slowed down ({})", gpu.index, slowdown.join(", ")),
            );
        }
        if let Some(ecc) = gpu.ecc.filter(|e| e.uncorrected > 0) {
            add(
                Severity::Critical,
                format!(
                    "GPU {}: {} uncorrected ECC errors",
                    gpu.index, ecc.uncorrected
                ),
            );
        }
    }
    for (device, celsius) in drive_temperatures(snapshot) {
        if reached(celsius as f64, limits.drive_warning_celsius as f64) {
            add(Severity::Warning, format!("{device} at {celsius:.0}°C"));
        }
    }
    // Stable sort keeps the order above within each severity.
    alerts.sort_by_key(|a| std::cmp::Reverse(a.severity));
    alerts
}

/// The alert for processes the OOM killer stopped: the likely ones by
/// name ("OOM killer stopped python3 (likely)"), so that another victim is
/// a new alert.
fn oom_alert(kills: &[OomKill]) -> Option<String> {
    let mut names: Vec<&str> = Vec::new();
    for name in kills
        .iter()
        .rev()
        .filter_map(|k| k.likely_process.as_deref())
    {
        if !names.contains(&name) {
            names.push(name);
        }
    }
    let likely = if names.is_empty() {
        String::new()
    } else {
        format!(" (likely {})", names.join(", "))
    };
    match kills.len() {
        0 => None,
        1 => Some(match names.first() {
            Some(name) => format!("OOM killer stopped {name} (likely)"),
            None => "OOM killer stopped a process".to_owned(),
        }),
        n => Some(format!("OOM killer stopped {n} processes{likely}")),
    }
}

/// (device, °C) of each drive: SMART results, then NVMe drives without
/// them from their hwmon sensor ("Composite", the drive's own summary).
fn drive_temperatures(snapshot: &Snapshot) -> Vec<(String, f32)> {
    let mut drives: Vec<(String, f32)> = snapshot
        .smart
        .iter()
        .filter_map(|s| Some((s.device.clone(), s.temperature_c?)))
        .collect();
    for chip in snapshot.sensors.iter().filter(|c| c.name == "nvme") {
        // The chip key is "nvme:<device>".
        let device = chip.key.split_once(':').map_or(chip.key.as_str(), |k| k.1);
        if drives.iter().any(|(d, _)| d == device) {
            continue;
        }
        let composite = chip
            .temperatures
            .iter()
            .find(|t| t.label == "Composite")
            .or(chip.temperatures.first());
        if let Some(t) = composite {
            drives.push((device.to_owned(), t.celsius));
        }
    }
    drives
}

/// How the process table is filtered and sorted.
#[derive(Debug, Clone, Copy)]
pub struct ProcessQuery<'q> {
    /// Column to sort by.
    pub sort_key: SortKey,
    /// Largest first when true.
    pub descending: bool,
    /// Case-insensitive substring of name, command or user, or an exact pid.
    pub filter: &'q str,
    /// Include kernel threads (`kworker` and friends).
    pub show_kernel_threads: bool,
    /// Children under their parents, siblings sorted by `sort_key`. A
    /// filter keeps the ancestors of what it matches.
    pub tree: bool,
}

/// Processes of `snapshot` after filtering and sorting, joined with GPU usage.
pub fn process_rows<'a>(snapshot: &'a Snapshot, query: &ProcessQuery<'_>) -> Vec<ProcessRow<'a>> {
    // pid -> (max SM %, total VRAM) across GPUs.
    let mut gpu_usage: HashMap<u32, (Option<f32>, Option<u64>)> = HashMap::new();
    for gpu in &snapshot.gpus {
        for p in &gpu.processes {
            let entry = gpu_usage.entry(p.pid).or_default();
            if let Some(sm) = p.sm_percent {
                entry.0 = Some(entry.0.map_or(sm, |v| v.max(sm)));
            }
            if let Some(mem) = p.memory_bytes {
                entry.1 = Some(entry.1.unwrap_or(0) + mem);
            }
        }
    }

    let filter = query.filter.to_lowercase();
    let matches = |p: &ProcessInfo| {
        filter.is_empty()
            || p.name.to_lowercase().contains(&filter)
            || p.command.to_lowercase().contains(&filter)
            || p.user.to_lowercase().contains(&filter)
            || p.container
                .as_ref()
                .is_some_and(|c| c.to_lowercase().contains(&filter))
            || p.pid.to_string() == filter
    };
    let candidates = snapshot
        .processes
        .iter()
        .filter(|p| query.show_kernel_threads || !p.kernel_thread);
    let shown: Vec<&ProcessInfo> = if query.tree && !filter.is_empty() {
        with_ancestors(candidates.collect(), matches)
    } else {
        candidates.filter(|p| matches(p)).collect()
    };
    let mut rows: Vec<ProcessRow<'a>> = shown
        .into_iter()
        .map(|info| {
            let (gpu_percent, vram_bytes) = gpu_usage.get(&info.pid).copied().unwrap_or_default();
            ProcessRow {
                info,
                gpu_percent,
                vram_bytes,
                tree_prefix: String::new(),
            }
        })
        .collect();

    let key = query.sort_key;
    rows.sort_by(|a, b| {
        let ordering = match key {
            SortKey::Pid => a.info.pid.cmp(&b.info.pid),
            SortKey::Name => a.info.name.to_lowercase().cmp(&b.info.name.to_lowercase()),
            SortKey::Threads => a.info.threads.cmp(&b.info.threads),
            SortKey::User => a.info.user.cmp(&b.info.user),
            SortKey::Container => match (&a.info.container, &b.info.container) {
                (Some(a), Some(b)) => a.cmp(b),
                (Some(_), None) => std::cmp::Ordering::Less,
                (None, Some(_)) => std::cmp::Ordering::Greater,
                (None, None) => std::cmp::Ordering::Equal,
            },
            SortKey::Memory => a.info.memory_bytes.cmp(&b.info.memory_bytes),
            SortKey::Cpu => a.info.cpu_percent.total_cmp(&b.info.cpu_percent),
            SortKey::Disk => disk_rate(a.info).total_cmp(&disk_rate(b.info)),
            SortKey::Gpu => a
                .gpu_percent
                .unwrap_or(-1.0)
                .total_cmp(&b.gpu_percent.unwrap_or(-1.0)),
            SortKey::Vram => a.vram_bytes.cmp(&b.vram_bytes),
        }
        // Stable tie-breaker so rows do not jump between frames.
        .then(a.info.pid.cmp(&b.info.pid));
        if query.descending {
            ordering.reverse()
        } else {
            ordering
        }
    });
    if query.tree { into_tree(rows) } else { rows }
}

/// The processes `matches` accepts, plus their ancestors among `processes`
/// (so a filtered tree keeps its shape), in the order of `processes`.
fn with_ancestors(
    processes: Vec<&ProcessInfo>,
    matches: impl Fn(&ProcessInfo) -> bool,
) -> Vec<&ProcessInfo> {
    let by_pid: HashMap<u32, &ProcessInfo> = processes.iter().map(|p| (p.pid, *p)).collect();
    let mut keep: HashSet<u32> = HashSet::new();
    for process in processes.iter().filter(|p| matches(p)) {
        let mut current = Some(*process);
        // Stops at a process already kept, which also ends parent loops.
        while let Some(p) = current
            && keep.insert(p.pid)
        {
            current = p.parent_pid.and_then(|pid| by_pid.get(&pid).copied());
        }
    }
    processes
        .into_iter()
        .filter(|p| keep.contains(&p.pid))
        .collect()
}

/// Reorders sorted `rows` depth-first under their parents, keeping the
/// sorted order among siblings, and fills in [`ProcessRow::tree_prefix`].
/// Processes whose parent is not among the rows are roots.
fn into_tree(rows: Vec<ProcessRow<'_>>) -> Vec<ProcessRow<'_>> {
    let index: HashMap<u32, usize> = rows
        .iter()
        .enumerate()
        .map(|(i, row)| (row.info.pid, i))
        .collect();
    let mut children: Vec<Vec<usize>> = vec![Vec::new(); rows.len()];
    let mut roots = Vec::new();
    for (i, row) in rows.iter().enumerate() {
        match row.info.parent_pid.and_then(|pid| index.get(&pid)) {
            Some(&parent) if parent != i => children[parent].push(i),
            _ => roots.push(i),
        }
    }
    let mut order: Vec<(usize, String)> = Vec::with_capacity(rows.len());
    let mut visited = vec![false; rows.len()];
    // Processes in a parent loop have no root; they start trees of their own.
    let starts: Vec<usize> = roots.into_iter().chain(0..rows.len()).collect();
    for start in starts {
        // (row, lines of the ancestors, whether it is its parent's last child)
        let mut stack: Vec<(usize, String, Option<bool>)> = vec![(start, String::new(), None)];
        while let Some((i, lead, last)) = stack.pop() {
            if std::mem::replace(&mut visited[i], true) {
                continue;
            }
            let (prefix, child_lead) = match last {
                None => (String::new(), String::new()),
                Some(true) => (format!("{lead}â”” "), format!("{lead}  ")),
                Some(false) => (format!("{lead}├ "), format!("{lead}│ ")),
            };
            order.push((i, prefix));
            let kids = &children[i];
            for (k, &child) in kids.iter().enumerate().rev() {
                stack.push((child, child_lead.clone(), Some(k + 1 == kids.len())));
            }
        }
    }
    let mut rows: Vec<Option<ProcessRow<'_>>> = rows.into_iter().map(Some).collect();
    order
        .into_iter()
        .filter_map(|(i, prefix)| {
            let mut row = rows[i].take()?;
            row.tree_prefix = prefix;
            Some(row)
        })
        .collect()
}

/// Disk reads plus writes of a process (bytes/s), the Disk column.
pub fn disk_rate(process: &ProcessInfo) -> f64 {
    process.read_bytes_per_sec + process.write_bytes_per_sec
}

/// The Disk column's text: the total rate, blank when idle (also where the
/// counters cannot be read, e.g. other users' processes on Linux).
pub fn disk_rate_text(process: &ProcessInfo) -> String {
    let rate = disk_rate(process);
    if rate > 0.0 {
        format::rate(rate)
    } else {
        String::new()
    }
}

/// Shown where the CPU temperatures go when LibreHardwareMonitor cannot be
/// read (Windows; see [`Snapshot::lhm_error`]).
pub const LHM_HINT: &str = "temperatures need LibreHardwareMonitor";

/// Explanation behind [`LHM_HINT`], given the reason it could not be read.
pub fn lhm_help(error: &str) -> String {
    format!(
        "Windows needs LibreHardwareMonitor for temperatures, fans and CPU power.\n\
         Start it and turn on Options → Remote Web Server → Run.\n\
         ([lhm] url = \"\" in the configuration hides this note.)\n\n\
         {error}"
    )
}

/// Name of a signal as shown to the user.
pub fn signal_name(signal: ProcessSignal) -> &'static str {
    match signal {
        ProcessSignal::Terminate => "SIGTERM",
        ProcessSignal::Kill => "SIGKILL",
    }
}

/// Returns true for container bridges, veth pairs and loopback, which are
/// skipped when choosing and listing interfaces.
pub fn is_virtual_interface(name: &str) -> bool {
    VIRTUAL_IFACE_PREFIXES.iter().any(|p| name.starts_with(p))
}

/// Picks the physical interface with the most traffic so far.
pub fn default_interface(snapshot: &Snapshot) -> Option<String> {
    snapshot
        .networks
        .iter()
        .filter(|n| !is_virtual_interface(&n.name))
        .max_by_key(|n| n.rx_total_bytes + n.tx_total_bytes)
        .or_else(|| snapshot.networks.first())
        .map(|n| n.name.clone())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::snapshot::{EccErrors, GpuProcess, NetworkStats};

    #[test]
    fn process_details_rows() {
        let mut snapshot = Snapshot::default();
        let mut shell = process(20, "zsh", 0.0, false);
        shell.parent_pid = Some(1);
        let mut python = process(30, "python", 99.5, false);
        python.parent_pid = Some(20);
        python.command = "python train.py --epochs 10".to_owned();
        python.container = Some("trainer".to_owned());
        python.run_time_secs = 3_660;
        snapshot.processes = vec![process(1, "systemd", 0.0, false), shell, python];
        snapshot.gpus = vec![GpuStats {
            index: 0,
            processes: vec![GpuProcess {
                pid: 30,
                memory_bytes: Some(2 << 30),
                sm_percent: Some(97.0),
            }],
            ..Default::default()
        }];
        let now = SystemTime::now();
        let rows = process_details(&snapshot, 30, now).unwrap();
        let get = |label: &str| {
            rows.iter()
                .find(|(l, _)| *l == label)
                .map(|(_, v)| v.as_str())
        };
        assert_eq!(get("Program"), Some("python (pid 30)"));
        assert_eq!(get("Parents"), Some("systemd (1) › zsh (20)"));
        assert_eq!(get("Children"), None);
        assert_eq!(get("Container"), Some("trainer"));
        assert!(get("Running").unwrap().starts_with("1h01m (since "));
        assert_eq!(get("GPU"), Some("GPU 0 97%, 2.0G VRAM"));
        assert_eq!(get("Command"), Some("python train.py --epochs 10"));
        let shell = process_details(&snapshot, 20, now).unwrap();
        assert!(shell.contains(&("Children", "1".to_owned())));
        assert!(process_details(&snapshot, 99, now).is_none());
    }

    #[test]
    fn attention_ranks() {
        let alert = |severity| Alert {
            severity,
            text: String::new(),
        };
        assert_eq!(attention_rank(&[], false, false), 2);
        assert_eq!(attention_rank(&[alert(Severity::Warning)], false, false), 1);
        assert_eq!(attention_rank(&[], false, true), 1);
        assert_eq!(
            attention_rank(
                &[alert(Severity::Warning), alert(Severity::Critical)],
                false,
                false
            ),
            0
        );
        assert_eq!(attention_rank(&[], true, false), 0);
    }

    #[test]
    fn gpu_notes_and_pcie() {
        let mut gpu = GpuStats {
            throttle_reasons: vec!["power cap".to_owned()],
            ..Default::default()
        };
        assert_eq!(gpu_notes(&gpu), [("power cap".to_owned(), None)]);
        gpu.throttle_reasons.push("thermal".to_owned());
        gpu.ecc = Some(EccErrors {
            corrected: 3,
            uncorrected: 0,
        });
        assert_eq!(
            gpu_notes(&gpu),
            [
                ("slowed: thermal".to_owned(), Some(Severity::Warning)),
                ("ECC 3 corrected".to_owned(), None)
            ]
        );
        let full = PcieLink {
            generation: 4,
            width: 16,
            max_generation: 4,
            max_width: 16,
        };
        assert_eq!(pcie_text(&full), "PCIe gen 4 x16");
        let idle = PcieLink {
            generation: 1,
            width: 8,
            ..full
        };
        assert!(pcie_text(&idle).starts_with("PCIe gen 1 x8 (up to gen 4 x16;"));
    }

    fn process(pid: u32, name: &str, cpu: f32, kernel: bool) -> ProcessInfo {
        ProcessInfo {
            pid,
            name: name.to_owned(),
            cpu_percent: cpu,
            kernel_thread: kernel,
            ..Default::default()
        }
    }

    fn query(sort_key: SortKey) -> ProcessQuery<'static> {
        ProcessQuery {
            sort_key,
            descending: sort_key.descending_by_default(),
            filter: "",
            show_kernel_threads: false,
            tree: false,
        }
    }

    #[test]
    fn history_keeps_newest_samples() {
        let mut history = History::default();
        for i in 0..(HISTORY_LEN + 5) {
            history.push(i as f64);
        }
        assert_eq!(
            history.last(2),
            vec![(HISTORY_LEN + 3) as f64, (HISTORY_LEN + 4) as f64]
        );
        assert_eq!(history.last(HISTORY_LEN * 2).len(), HISTORY_LEN);
        assert_eq!(history.max_of_last(3), (HISTORY_LEN + 4) as f64);
    }

    #[test]
    fn graph_spans_spread_samples_over_the_width() {
        let second = Duration::from_secs(1);
        let mut history = History::default();
        for i in 0..120 {
            history.push(i as f64);
        }
        // Fit: one sample per column, the newest ones.
        assert_eq!(
            history.graph(3, GraphSpan::Fit, second),
            vec![117.0, 118.0, 119.0]
        );
        // A minute on 30 columns: two samples averaged per column.
        let minute = history.graph(30, GraphSpan::Secs(60), second);
        assert_eq!(minute.len(), 30);
        assert_eq!(minute[0], 60.5);
        assert_eq!(minute[29], 118.5);
        // Fewer samples than columns: interpolated between them.
        let stretched = history.graph(120, GraphSpan::Secs(60), Duration::from_secs(2));
        assert_eq!(stretched.len(), 120);
        assert_eq!(&stretched[..2], &[90.0, 90.0]);
        assert_eq!(&stretched[2..4], &[90.125, 90.375]);
        assert_eq!(stretched[119], 119.0);
        assert!(stretched.windows(2).all(|w| w[0] <= w[1]));
        // An hour with two minutes recorded fills the right 1/30.
        let hour = history.graph(300, GraphSpan::Secs(3600), second);
        assert_eq!(hour.len(), 10);
        assert_eq!(hour[0], 5.5);
        assert!(
            History::default()
                .graph(10, GraphSpan::Secs(60), second)
                .is_empty()
        );
    }

    #[test]
    fn long_spans_draw_from_the_averages() {
        let second = Duration::from_secs(1);
        let mut history = History::default();
        // Two hours: the first at 0, the second at 100.
        for i in 0..7200 {
            history.push(if i < 3600 { 0.0 } else { 100.0 });
        }
        // 6 hours with 2 recorded: the right third, half low and half high.
        let six = history.graph(90, GraphSpan::Secs(6 * 3600), second);
        assert_eq!(six.len(), 30);
        assert_eq!((six[0], six[29]), (0.0, 100.0));
        // An hour still comes from the samples themselves.
        let hour = history.graph(60, GraphSpan::Secs(3600), second);
        assert!(hour.iter().all(|v| *v == 100.0));
        // The averages stay bounded.
        for i in 0..(COARSE_LEN * COARSE_FACTOR + 500) {
            history.push(i as f64);
        }
        assert_eq!(history.coarse.len(), COARSE_LEN);
    }

    #[test]
    fn graph_span_choices_cycle() {
        let labels: Vec<String> = std::iter::successors(Some(GraphSpan::Fit), |s| Some(s.next()))
            .take(6)
            .map(GraphSpan::label)
            .collect();
        assert_eq!(labels, ["fit", "1m", "5m", "15m", "1h", "6h"]);
        assert_eq!(GraphSpan::Secs(86_400).label(), "24h");
        assert_eq!(GraphSpan::Secs(86_400).next(), GraphSpan::Fit);
        assert_eq!(
            GraphSpan::from_secs(GraphSpan::Secs(300).as_secs()),
            GraphSpan::Secs(300)
        );
        assert_eq!(GraphSpan::from_secs(0), GraphSpan::Fit);
        // A stored span that is not a choice starts over.
        assert_eq!(GraphSpan::Secs(7).next(), GraphSpan::Fit);
    }

    #[test]
    fn rows_filter_sort_and_join_gpu_usage() {
        let snapshot = Snapshot {
            processes: vec![
                process(10, "bash", 5.0, false),
                process(2, "kworker/0:1", 50.0, true),
                process(30, "python", 1.0, false),
            ],
            gpus: vec![GpuStats {
                processes: vec![GpuProcess {
                    pid: 30,
                    memory_bytes: Some(1 << 30),
                    sm_percent: Some(70.0),
                }],
                ..Default::default()
            }],
            ..Default::default()
        };
        let pids = |q: &ProcessQuery| {
            process_rows(&snapshot, q)
                .iter()
                .map(|r| r.info.pid)
                .collect::<Vec<_>>()
        };
        assert_eq!(pids(&query(SortKey::Cpu)), vec![10, 30]);
        assert_eq!(pids(&query(SortKey::Vram)), vec![30, 10]);
        assert_eq!(
            pids(&ProcessQuery {
                show_kernel_threads: true,
                ..query(SortKey::Cpu)
            }),
            vec![2, 10, 30]
        );
        assert_eq!(
            pids(&ProcessQuery {
                filter: "PYT",
                ..query(SortKey::Pid)
            }),
            vec![30]
        );
        let rows = process_rows(&snapshot, &query(SortKey::Pid));
        assert_eq!(rows[1].vram_bytes, Some(1 << 30));
        assert_eq!(rows[1].gpu_percent, Some(70.0));
    }

    #[test]
    fn since_notes_say_how_long_and_since_when() {
        let now = SystemTime::now();
        let ago = |secs: u64| now - Duration::from_secs(secs);
        assert!(since_note(ago(42), now).starts_with("42s (since "));
        assert!(since_note(ago(5 * 60), now).starts_with("5m (since "));
        assert!(since_note(ago(3 * 3600 + 7 * 60), now).starts_with("3h07m (since "));
        assert!(since_note(ago(2 * 86_400 + 3 * 3600), now).starts_with("2d03h (since "));
        // A clock that moved backwards does not underflow.
        assert!(since_note(now + Duration::from_secs(5), now).starts_with("0s"));
    }

    #[test]
    fn tree_puts_children_under_parents() {
        let child = |pid: u32, parent: u32, name: &str, cpu: f32| ProcessInfo {
            parent_pid: Some(parent),
            ..process(pid, name, cpu, false)
        };
        let snapshot = Snapshot {
            processes: vec![
                process(1, "init", 0.0, false),
                child(10, 1, "sshd", 1.0),
                child(11, 10, "bash", 2.0),
                child(12, 11, "python", 50.0),
                child(20, 1, "cron", 3.0),
                // Parent not listed (e.g. not readable): a root of its own.
                child(30, 999, "orphan", 4.0),
                // A parent loop must not hang or drop the processes.
                child(40, 41, "loop-a", 0.0),
                child(41, 40, "loop-b", 0.0),
            ],
            ..Default::default()
        };
        let tree = |filter| {
            process_rows(
                &snapshot,
                &ProcessQuery {
                    filter,
                    tree: true,
                    ..query(SortKey::Cpu)
                },
            )
            .iter()
            .map(|r| format!("{}{}", r.tree_prefix, r.info.name))
            .collect::<Vec<_>>()
        };
        // Roots and siblings by CPU, largest first (ties: higher pid first).
        assert_eq!(
            tree(""),
            vec![
                "orphan",
                "init",
                "├ cron",
                "â”” sshd",
                "  â”” bash",
                "    â”” python",
                "loop-b",
                "â”” loop-a",
            ]
        );
        // A filter keeps the ancestors of what it matches.
        assert_eq!(
            tree("pyth"),
            vec!["init", "â”” sshd", "  â”” bash", "    â”” python"]
        );
    }

    #[test]
    fn disk_column_sums_reads_and_writes() {
        let reader = ProcessInfo {
            read_bytes_per_sec: 3.0 * 1024.0 * 1024.0,
            ..process(10, "reader", 0.0, false)
        };
        let writer = ProcessInfo {
            read_bytes_per_sec: 1024.0 * 1024.0,
            write_bytes_per_sec: 4.0 * 1024.0 * 1024.0,
            ..process(20, "writer", 0.0, false)
        };
        let idle = process(30, "idle", 0.0, false);
        assert_eq!(disk_rate_text(&writer), "5.0M/s");
        assert_eq!(disk_rate_text(&idle), "");
        let snapshot = Snapshot {
            processes: vec![idle, reader, writer],
            ..Default::default()
        };
        let pids: Vec<u32> = process_rows(&snapshot, &query(SortKey::Disk))
            .iter()
            .map(|r| r.info.pid)
            .collect();
        assert_eq!(pids, vec![20, 10, 30]);
    }

    #[test]
    fn metric_history_records_and_switches_interfaces() {
        let snapshot = Snapshot {
            networks: vec![
                NetworkStats {
                    name: "enp9s0".to_owned(),
                    rx_total_bytes: 10,
                    rx_bytes_per_sec: 100.0,
                    ..Default::default()
                },
                NetworkStats {
                    name: "wlp7s0".to_owned(),
                    tx_bytes_per_sec: 5.0,
                    ..Default::default()
                },
            ],
            gpus: vec![GpuStats {
                utilization_percent: Some(40.0),
                ..Default::default()
            }],
            ..Default::default()
        };
        let mut history = MetricHistory::default();
        history.record(&snapshot);
        assert_eq!(history.net_interface.as_deref(), Some("enp9s0"));
        assert_eq!(history.net_rx.last(1), vec![100.0]);
        assert_eq!(history.gpus[0].last(1), vec![40.0]);
        assert_eq!(history.next_interface(&snapshot).as_deref(), Some("wlp7s0"));
        // Switching clears the samples of the previous interface.
        assert!(history.net_rx.last(10).is_empty());
        history.record(&snapshot);
        assert_eq!(history.net_tx.last(1), vec![5.0]);
    }

    #[test]
    fn alerts_flag_storage_problems_first() {
        use crate::snapshot::{
            BtrfsDeviceStats, BtrfsFilesystem, FilesystemUsage, MdArray, RaidStats, SmartDevice,
            ZfsPool,
        };
        let snapshot = Snapshot {
            filesystems: vec![FilesystemUsage {
                mount_points: vec!["/data".to_owned()],
                total_bytes: 100,
                available_bytes: 8,
                ..Default::default()
            }],
            raid: RaidStats {
                btrfs: vec![BtrfsFilesystem {
                    mount_points: vec!["/".to_owned()],
                    device_stats: vec![BtrfsDeviceStats {
                        devid: 1,
                        corruption_errs: 2,
                        ..Default::default()
                    }],
                    ..Default::default()
                }],
                md: vec![MdArray {
                    name: "md0".to_owned(),
                    active: true,
                    level: "raid1".to_owned(),
                    expected_devices: Some(2),
                    working_devices: Some(1),
                    ..Default::default()
                }],
                zfs: vec![
                    ZfsPool {
                        name: "tank".to_owned(),
                        state: "DEGRADED".to_owned(),
                    },
                    ZfsPool {
                        name: "cold".to_owned(),
                        state: "OFFLINE".to_owned(),
                    },
                    ZfsPool {
                        name: "fast".to_owned(),
                        state: "ONLINE".to_owned(),
                    },
                ],
            },
            smart: vec![SmartDevice {
                device: "sda".to_owned(),
                passed: Some(true),
                ..Default::default()
            }],
            ..Default::default()
        };
        let alerts = alerts(&snapshot);
        let texts: Vec<&str> = alerts.iter().map(|a| a.text.as_str()).collect();
        assert_eq!(
            texts,
            vec![
                "btrfs /: 2 I/O errors (devid 1)",
                "md0 (raid1) degraded",
                "zfs tank DEGRADED",
                "zfs cold OFFLINE",
                "/data 92% full"
            ]
        );
        assert_eq!(alerts[0].severity, Severity::Critical);
        assert_eq!(alerts[2].severity, Severity::Critical);
        assert_eq!(alerts[3].severity, Severity::Warning);
        assert!(super::alerts(&Snapshot::default()).is_empty());
    }

    #[test]
    fn event_times_are_as_short_as_is_clear() {
        let now = SystemTime::now();
        let ms = |t: SystemTime| {
            t.duration_since(SystemTime::UNIX_EPOCH)
                .unwrap()
                .as_millis() as u64
        };
        // "14:03:12" today; a weekday this week; a date before.
        assert_eq!(event_time(ms(now), now).len(), 8);
        let week_ago = now - Duration::from_secs(10 * 86_400);
        assert!(event_time(ms(week_ago), now).contains(' '));
        assert_ne!(event_time(ms(week_ago), now).len(), 8);
    }

    #[test]
    fn alert_history_survives_a_restart() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("state/alerts-tui.jsonl");
        let degraded = Alert {
            severity: Severity::Critical,
            text: "md0 (raid1) degraded".to_owned(),
        };
        let full = Alert {
            severity: Severity::Warning,
            text: "/srv 92% full".to_owned(),
        };
        let mut log = AlertLog::persistent(path.clone());
        log.update("nas", &[degraded.clone(), full.clone()], 1_000);
        log.update("nas", std::slice::from_ref(&degraded), 2_000);
        assert_eq!(log.events().count(), 3);

        // The next run sees the same events, and the alert still present
        // is not logged again.
        let mut log = AlertLog::persistent(path.clone());
        assert_eq!(log.events().count(), 3);
        assert_eq!(log.unread(), (0, None));
        assert!(
            log.update("nas", std::slice::from_ref(&degraded), 3_000)
                .is_empty()
        );
        assert_eq!(log.events().count(), 3);
        let changes = log.update("nas", &[], 4_000);
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].alert, degraded);
        let last = log.events().last().unwrap();
        assert_eq!((last.change, last.at_ms), (AlertChange::Cleared, 4_000));
        assert_eq!(AlertLog::persistent(path.clone()).events().count(), 4);

        // A one-off notice is unread, kept, and not taken for a lasting
        // alert by the next run (which would log it cleared).
        let exited = Alert {
            severity: Severity::Warning,
            text: "train.py (pid 7) exited after 3h".to_owned(),
        };
        let mut log = AlertLog::persistent(path.clone());
        log.record("nas", exited.clone(), 5_000);
        assert_eq!(log.unread(), (1, Some(Severity::Warning)));
        let mut log = AlertLog::persistent(path);
        assert!(log.update("nas", &[], 6_000).is_empty());
        let last = log.events().last().unwrap();
        assert_eq!((last.change, &last.alert), (AlertChange::Notice, &exited));
    }

    #[test]
    fn exit_watch_reports_exits_it_can_tell() {
        let process = |pid: u32, name: &str| ProcessInfo {
            pid,
            name: name.to_owned(),
            run_time_secs: 3_600,
            ..Default::default()
        };
        let mut watch = ExitWatch::default();
        assert!(watch.toggle(&process(7, "train.py"), 10_000_000));
        assert!(watch.toggle(&process(8, "make"), 10_000_000));
        assert!(!watch.toggle(&process(8, "make"), 10_000_000));
        assert_eq!(watch.pids(), [7]);

        // An agent that does not watch (before 0.19): gone from a limited
        // list says nothing.
        let mut snapshot = Snapshot::default();
        assert_eq!(watch.state(&snapshot, 7), WatchState::Unknown);
        assert!(watch.check(&snapshot, 10_000_000).is_empty());
        // Listed but not reported: its exit would go unnoticed.
        snapshot.processes = vec![process(7, "train.py")];
        assert_eq!(watch.state(&snapshot, 7), WatchState::Unknown);

        snapshot.watched_pids = vec![7];
        snapshot.processes = vec![process(7, "train.py")];
        assert_eq!(watch.state(&snapshot, 7), WatchState::Running);
        assert!(watch.check(&snapshot, 10_000_000).is_empty());

        // Gone while watched: exited, after its run time plus the wait.
        snapshot.processes.clear();
        assert_eq!(watch.state(&snapshot, 7), WatchState::Exited);
        let alerts = watch.check(&snapshot, 10_000_000 + 1_800_000);
        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].text, "train.py (pid 7) exited after 1h30m");
        assert_eq!(watch.pids(), Vec::<u32>::new(), "reported once");

        // The pid taken by another program also means it exited.
        watch.toggle(&process(9, "rsync"), 0);
        snapshot.processes = vec![process(9, "bash")];
        assert_eq!(watch.check(&snapshot, 0).len(), 1);
    }

    #[test]
    fn failed_units_warn_unless_ignored() {
        let snapshot = Snapshot {
            failed_units: vec!["nginx.service".to_owned(), "wait-online.service".to_owned()],
            ..Default::default()
        };
        let limits = AlertsConfig {
            ignored_units: vec!["wait-online.service".to_owned()],
            ..Default::default()
        };
        let alerts = alerts_with(&snapshot, &limits);
        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].text, "systemd: nginx.service failed");
        assert_eq!(alerts[0].severity, Severity::Warning);
    }

    #[test]
    fn version_notes_only_for_other_versions() {
        let mut snapshot = Snapshot::default();
        assert_eq!(version_note(&snapshot), None);
        snapshot.host.rigtop_version = env!("CARGO_PKG_VERSION").to_owned();
        assert_eq!(version_note(&snapshot), None);
        snapshot.host.rigtop_version = "0.1.0".to_owned();
        assert_eq!(version_note(&snapshot).as_deref(), Some("agent 0.1.0"));
        assert!(version_hint(&snapshot).contains("rigtop 0.1.0"));
    }

    #[test]
    fn alert_log_records_alerts_appearing_and_clearing() {
        let alert = |severity, text: &str| Alert {
            severity,
            text: text.to_owned(),
        };
        let full = alert(Severity::Warning, "/data 91% full");
        let errors = alert(Severity::Critical, "btrfs /: 2 I/O errors (nvme1n1)");
        let mut log = AlertLog::default();
        log.update("nas", std::slice::from_ref(&full), 1_000);
        // Numbers changing is the same alert.
        log.update(
            "nas",
            &[alert(Severity::Warning, "/data 92% full"), errors.clone()],
            2_000,
        );
        log.update("zefat", &[], 2_000);
        assert_eq!(log.unread(), (2, Some(Severity::Critical)));
        log.mark_read();
        log.update("nas", std::slice::from_ref(&errors), 3_000);
        assert_eq!(log.unread(), (0, None));
        let summary: Vec<(u64, AlertChange, &str)> = log
            .events()
            .map(|e| (e.at_ms, e.change, e.alert.text.as_str()))
            .collect();
        assert_eq!(
            summary,
            [
                (1_000, AlertChange::Raised, "/data 91% full"),
                (
                    2_000,
                    AlertChange::Raised,
                    "btrfs /: 2 I/O errors (nvme1n1)"
                ),
                (3_000, AlertChange::Cleared, "/data 92% full"),
            ]
        );
        for i in 0..(ALERT_LOG_LEN as u64 + 10) {
            log.update(
                "flappy",
                &[alert(Severity::Warning, if i % 2 == 0 { "a" } else { "b" })],
                i,
            );
        }
        assert_eq!(log.events().count(), ALERT_LOG_LEN);
    }

    #[test]
    fn filesystems_filling_up_warn() {
        use crate::snapshot::FilesystemUsage;
        let filling = |full_in_secs| Snapshot {
            filesystems: vec![FilesystemUsage {
                mount_points: vec!["/srv".to_owned()],
                total_bytes: 100,
                available_bytes: 50,
                full_in_secs,
                ..Default::default()
            }],
            ..Default::default()
        };
        let texts = |snapshot: &Snapshot, limits: &AlertsConfig| -> Vec<String> {
            alerts_with(snapshot, limits)
                .into_iter()
                .map(|a| a.text)
                .collect()
        };
        let defaults = AlertsConfig::default();
        assert_eq!(
            texts(&filling(Some(5 * 3600)), &defaults),
            ["/srv full in ~5h00m"]
        );
        assert!(texts(&filling(Some(30 * 3600)), &defaults).is_empty());
        assert!(texts(&filling(None), &defaults).is_empty());
        let off = AlertsConfig {
            filesystem_full_warning_hours: 0.0,
            ..Default::default()
        };
        assert!(texts(&filling(Some(60)), &off).is_empty());
    }

    #[test]
    fn alert_thresholds_come_from_the_configuration() {
        use crate::snapshot::{FilesystemUsage, GpuStats, SensorChip, SmartDevice, Temperature};
        let temp = |label: &str, celsius| Temperature {
            label: label.to_owned(),
            celsius,
            ..Default::default()
        };
        let mut snapshot = Snapshot {
            filesystems: vec![FilesystemUsage {
                mount_points: vec!["/data".to_owned()],
                total_bytes: 100,
                available_bytes: 8,
                ..Default::default()
            }],
            gpus: vec![GpuStats {
                index: 0,
                temperature_c: Some(80.0),
                ..Default::default()
            }],
            smart: vec![SmartDevice {
                device: "nvme0".to_owned(),
                temperature_c: Some(72.0),
                ..Default::default()
            }],
            // nvme0 also has SMART (reported once); nvme1 only its sensor.
            sensors: vec![
                SensorChip {
                    key: "nvme:nvme0".to_owned(),
                    name: "nvme".to_owned(),
                    temperatures: vec![temp("Composite", 71.0)],
                    ..Default::default()
                },
                SensorChip {
                    key: "nvme:nvme1".to_owned(),
                    name: "nvme".to_owned(),
                    temperatures: vec![temp("Sensor 1", 90.0), temp("Composite", 74.0)],
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        snapshot.cpu.temperatures = vec![temp("Tctl", 97.0), temp("Tccd1", 90.0)];
        let texts_of = |snapshot: &Snapshot| -> Vec<String> {
            alerts_with(snapshot, &AlertsConfig::default())
                .into_iter()
                .map(|a| a.text)
                .collect()
        };
        let texts = |limits: &AlertsConfig| -> Vec<String> {
            alerts_with(&snapshot, limits)
                .into_iter()
                .map(|a| a.text)
                .collect()
        };
        assert_eq!(
            texts(&AlertsConfig::default()),
            [
                "/data 92% full",
                "CPU at 97°C",
                "nvme0 at 72°C",
                "nvme1 at 74°C"
            ]
        );
        let custom = AlertsConfig {
            filesystem_warning_percent: 0.0,
            cpu_warning_celsius: 0.0,
            gpu_warning_celsius: 75.0,
            drive_warning_celsius: 73.0,
            ..Default::default()
        };
        assert_eq!(texts(&custom), ["GPU 0 at 80°C", "nvme1 at 74°C"]);

        // Hardware slowdowns warn; running at the power limit does not.
        let mut gpu = snapshot.gpus[0].clone();
        gpu.throttle_reasons = vec!["power cap".to_owned()];
        let mut capped = snapshot.clone();
        capped.gpus = vec![gpu.clone()];
        assert!(!texts_of(&capped).iter().any(|t| t.contains("slowed")));
        gpu.throttle_reasons.push("hw thermal".to_owned());
        gpu.ecc = Some(EccErrors {
            corrected: 5,
            uncorrected: 2,
        });
        capped.gpus = vec![gpu];
        let alerts = alerts_with(&capped, &AlertsConfig::default());
        assert_eq!(alerts[0].text, "GPU 0: 2 uncorrected ECC errors");
        assert_eq!(alerts[0].severity, Severity::Critical);
        assert!(
            alerts
                .iter()
                .any(|a| a.text == "GPU 0 slowed down (hw thermal)")
        );
        let critical = AlertsConfig {
            filesystem_critical_percent: 90.0,
            ..custom
        };
        assert_eq!(
            alerts_with(&snapshot, &critical)[0],
            Alert {
                severity: Severity::Critical,
                text: "/data 92% full".to_owned()
            }
        );
    }

    #[test]
    fn memory_alerts() {
        use crate::snapshot::{MemoryStats, OomKill};
        let kill = |name: Option<&str>| OomKill {
            at_ms: 1,
            likely_process: name.map(str::to_owned),
        };
        let mut snapshot = Snapshot {
            memory: MemoryStats {
                total_bytes: 100,
                used_bytes: 93,
                oom_kills: vec![kill(Some("python3"))],
                ..Default::default()
            },
            gpus: vec![GpuStats {
                index: 1,
                memory_used_bytes: Some(99),
                memory_total_bytes: Some(100),
                ..Default::default()
            }],
            ..Default::default()
        };
        let texts = |snapshot: &Snapshot, limits: &AlertsConfig| -> Vec<(Severity, String)> {
            alerts_with(snapshot, limits)
                .into_iter()
                .map(|a| (a.severity, a.text))
                .collect()
        };
        // VRAM is off by default (frameworks reserve it all).
        assert_eq!(
            texts(&snapshot, &AlertsConfig::default()),
            [
                (
                    Severity::Critical,
                    "OOM killer stopped python3 (likely)".to_owned()
                ),
                (Severity::Warning, "memory 93% used".to_owned()),
            ]
        );
        let vram = AlertsConfig {
            memory_warning_percent: 0.0,
            gpu_memory_warning_percent: 98.0,
            ..Default::default()
        };
        snapshot.memory.oom_kills = vec![kill(Some("bash")), kill(None), kill(Some("python3"))];
        assert_eq!(
            texts(&snapshot, &vram),
            [
                (
                    Severity::Critical,
                    "OOM killer stopped 3 processes (likely python3, bash)".to_owned()
                ),
                (Severity::Warning, "GPU 1 memory 99% used".to_owned()),
            ]
        );
        snapshot.memory.oom_kills = vec![kill(None)];
        assert_eq!(texts(&snapshot, &vram)[0].1, "OOM killer stopped a process");
    }

    #[test]
    fn interface_helpers() {
        assert!(is_virtual_interface("veth5a74356"));
        assert!(!is_virtual_interface("enp9s0"));
        let snapshot = Snapshot {
            networks: vec![
                NetworkStats {
                    name: "docker0".to_owned(),
                    rx_total_bytes: 1 << 40,
                    ..Default::default()
                },
                NetworkStats {
                    name: "enp9s0".to_owned(),
                    rx_total_bytes: 1 << 30,
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        assert_eq!(default_interface(&snapshot).as_deref(), Some("enp9s0"));
        assert_eq!(signal_name(ProcessSignal::Kill), "SIGKILL");
    }
}