Public: anyone can read this; the crew can change it.

The terminal's System dialog fits on any host: busiest four of each, then "and N more" (#16587) #16588

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

fix/system-dialog-fits → main 0 commits 1 thread · 1 unresolved

  • Merged. fix/system-dialog-fits was merged into main as 85dd524.

Review

Sign in to review this change.
2 files changed +36 −3
src/tui/screens/system.ts +18 −3
@@ -81,6 +81,21 @@ const WIDTH = 72;
8181 const GROUPS = 6;
8282 const CORES = 8;
8383 const PROCS = 2;
84 /**
85 * Disks, block devices and network interfaces each show at most this many, busiest first: a host
86 * with dozens of container bridges must not push the rest of the dialog past its foot.
87 */
88 const DEVICES = 4;
89
90 /**
91 * The busiest `n` of `rows` (by `load`, unknown last, ties in their order) and a line for the rest.
92 */
93 function busiest<T>(rows: readonly T[], load: (row: T) => number | null, line: (row: T) => string, noun: string, n = DEVICES): string[] {
94 const ranked = rows.map((row, i) => ({ row, i, at: load(row) })).sort((a, b) => (b.at ?? -1) - (a.at ?? -1) || a.i - b.i);
95 const rest = rows.length - n;
96 return [...ranked.slice(0, n).map(({ row }) => line(row)), ...(rest > 0 ? [`… and ${rest} more ${noun}`] : [])];
97 }
98 const sum = (a: number | null, b: number | null): number | null => (a === null && b === null ? null : (a ?? 0) + (b ?? 0));
8499
85100 const rate = (n: number | null): string => (n === null ? "—" : `${bytes(n)}/s`);
86101
@@ -135,9 +150,9 @@ export function systemLines(v: SystemView): string[] {
135150 ...(v.cpu.cores.length > CORES ? [`… and ${v.cpu.cores.length - CORES} more cores`] : []),
136151 `hottest ${v.hottestC === null ? "—" : `${v.hottestC.toFixed(0)}°C`} · memory ${v.memory ? `${bytes(v.memory.usedBytes)} of ${bytes(v.memory.totalBytes)}` : "—"} · ${v.processes} processes`,
137152 `swap ${v.memory ? (v.memory.swapTotalBytes === 0 ? "none" : `${bytes(v.memory.swapUsedBytes)} of ${bytes(v.memory.swapTotalBytes)}`) : "—"}`,
138 ...v.disks.map((d) => `${pad(d.mount, 20)} ${Math.round((d.usedBytes / Math.max(1, d.totalBytes)) * 100)}% of ${bytes(d.totalBytes)}`),
139 ...v.io.map((d) => `${pad(d.name, 20)} R ${rate(d.readBytesPerSecond)} W ${rate(d.writeBytesPerSecond)}`),
140 ...v.net.map((n) => `${pad(n.name, 20)} ↓ ${rate(n.rxBytesPerSecond)} ↑ ${rate(n.txBytesPerSecond)}`),
153 ...busiest(v.disks, (d) => d.usedBytes / Math.max(1, d.totalBytes), (d) => `${pad(d.mount, 20)} ${Math.round((d.usedBytes / Math.max(1, d.totalBytes)) * 100)}% of ${bytes(d.totalBytes)}`, "disks"),

The used-percent expression appears twice here, once for ranking and once for display. Pull it into a small usedPct(d) helper so the two cannot drift apart.

154 ...busiest(v.io, (d) => sum(d.readBytesPerSecond, d.writeBytesPerSecond), (d) => `${pad(d.name, 20)} R ${rate(d.readBytesPerSecond)} W ${rate(d.writeBytesPerSecond)}`, "devices"),
155 ...busiest(v.net, (n) => sum(n.rxBytesPerSecond, n.txBytesPerSecond), (n) => `${pad(n.name, 20)} ↓ ${rate(n.rxBytesPerSecond)} ↑ ${rate(n.txBytesPerSecond)}`, "interfaces"),
141156 "",
142157 ...v.groups.slice(0, GROUPS).flatMap((g) => [
143158 `${pad(g.label, 24)} ${pad(String(g.procs), 4, "right")} procs ${capOf(g)}`.trimEnd(),
test/tui-system.test.ts +18
@@ -522,6 +522,24 @@ describe("the machine line says when a cgroup holds the forge under the host (#1
522522 test("the host's own size: no such words", () => {
523523 expect(systemLines(view).join("\n")).not.toContain("cgroup");
524524 });
525
526 test("a host with dozens of bridges shows the busiest four interfaces and how many more, so the dialog fits", () => {
527 const net: SystemView["net"] = Array.from({ length: 60 }, (_, i) => ({ name: `br-${String(i).padStart(12, "0")}`, rxBytesPerSecond: null, txBytesPerSecond: null }));
528 net[40] = { name: "ens2f0", rxBytesPerSecond: 5_000_000, txBytesPerSecond: 1_000 };
529 net[50] = { name: "tailscale0", rxBytesPerSecond: 2_000, txBytesPerSecond: 3_000 };
530 const io = Array.from({ length: 12 }, (_, i) => ({ name: `sd${String.fromCharCode(97 + i)}`, readBytesPerSecond: i === 7 ? 9_000 : 0, writeBytesPerSecond: 0 }));
531 const disks = Array.from({ length: 9 }, (_, i) => ({ mount: `/m${i}`, fs: "ext4", totalBytes: 100, usedBytes: i * 10 }));
532 const lines = systemLines({ ...view, net, io, disks });
533 const nics = lines.filter((l) => l.includes("↓"));
534 expect(nics.map((l) => l.split(" ")[0])).toEqual(["ens2f0", "tailscale0", "br-000000000000", "br-000000000001"]);
535 expect(lines).toContain("… and 56 more interfaces");
536 expect(lines.filter((l) => / R .* W /.test(l))[0]).toStartWith("sdh");
537 expect(lines).toContain("… and 8 more devices");
538 expect(lines.filter((l) => /% of /.test(l)).map((l) => l.split(" ")[0])).toEqual(["/m8", "/m7", "/m6", "/m5"]);
539 expect(lines).toContain("… and 5 more disks");
540 // Bounded however many the host has: the dialog's own lines and Close stay on screen.
541 expect(lines.length).toBeLessThan(30);
542 });
525543 });
526544
527545 describe("tui system least used routes (#15073)", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs