Skip to content

eBPF-based security platform

tools/xtop: sort the rows without a tree apart from those one step behind

John Fastabend2f5db9ed9050
diff --git a/tools/xtop/src/components/NodesView.tsx b/tools/xtop/src/components/NodesView.tsx
index d91d1cf5..24c7da95 100644
--- a/tools/xtop/src/components/NodesView.tsx
+++ b/tools/xtop/src/components/NodesView.tsx
@@ -15,6 +15,7 @@ import {
healthPercent,
imageLabel,
labelPairs,
+ treeSortKey,
type FleetNode,
type FleetReport,
type ImageGroup,
@@ -127,7 +128,7 @@ const NODE_COLUMNS: readonly Column<FleetNode>[] = [
title: "TREE",
width: 10,
defaultDir: "asc",
- sortKey: (n) => (n.tree.lastSeq === null ? -1 : n.tree.lastSeq - (n.tree.headSeq ?? n.tree.lastSeq)),
+ sortKey: (n) => treeSortKey(n.tree),
render: (n) => <TreeCell last={n.tree.lastSeq} head={n.tree.headSeq} />,
},
{ id: "shields", title: "SHIELDS", width: 12, defaultDir: "desc", sortKey: (n) => shieldsKey(n.shields), render: (n) => <ShieldsCell s={n.shields} /> },
@@ -215,7 +216,7 @@ const IMAGE_COLUMNS: readonly Column<ImageGroup>[] = [
title: "TREE",
width: 11,
defaultDir: "asc",
- sortKey: (g) => (g.tree.lastSeq === null ? -1 : g.tree.lastSeq - (g.tree.headSeq ?? g.tree.lastSeq)),
+ sortKey: (g) => treeSortKey(g.tree),
render: (g) => <TreeCell last={g.tree.lastSeq} head={g.tree.headSeq} />,
},
{ id: "shields", title: "SHIELDS", width: 12, defaultDir: "desc", sortKey: (g) => shieldsKey(g.shields), render: (g) => <ShieldsCell s={g.shields} /> },
diff --git a/tools/xtop/src/lib/fleet.ts b/tools/xtop/src/lib/fleet.ts
index 6bc9b338..ec42da73 100644
--- a/tools/xtop/src/lib/fleet.ts
+++ b/tools/xtop/src/lib/fleet.ts
@@ -135,7 +135,7 @@ export function healthPercent(nodes: readonly Pick<FleetNode, "status">[]): numb
const UNKNOWN_LAG = 0.5;
/** head − last: how far behind its own tree a member is. */
-const lagOf = (m: FleetNode) => (m.tree.headSeq === null ? UNKNOWN_LAG : m.tree.headSeq - m.tree.lastSeq!);
+const lagOf = (t: ImageGroup["tree"]) => (t.headSeq === null ? UNKNOWN_LAG : t.headSeq - t.lastSeq!);
/**
* The tree position of the member furthest behind its own tree, all-null
@@ -147,10 +147,19 @@ const lagOf = (m: FleetNode) => (m.tree.headSeq === null ? UNKNOWN_LAG : m.tree.
function behindTree(members: readonly FleetNode[]): ImageGroup["tree"] {
const worst = members
.filter((m) => m.tree.lastSeq !== null)
- .sort((a, b) => lagOf(b) - lagOf(a) || a.tree.lastSeq! - b.tree.lastSeq!)[0];
+ .sort((a, b) => lagOf(b.tree) - lagOf(a.tree) || a.tree.lastSeq! - b.tree.lastSeq!)[0];
return { lastSeq: worst?.tree.lastSeq ?? null, headSeq: worst?.tree.headSeq ?? null };
}
+/**
+ * What the TREE column sorts by: the lag turned around, so the row
+ * furthest behind comes first, a row whose head is unknown between
+ * those behind and those caught up, as in an image's roll-up, and
+ * before them all the rows that follow no tree, whose key is below any
+ * distance a row can be behind.
+ */
+export const treeSortKey = (t: ImageGroup["tree"]) => (t.lastSeq === null ? -Infinity : 0 - lagOf(t));
+
export function groupByImage(nodes: readonly FleetNode[]): ImageGroup[] {
const byImage = new Map<string, FleetNode[]>();
for (const n of nodes) {
diff --git a/tools/xtop/test/fleet.test.ts b/tools/xtop/test/fleet.test.ts
index 81a5efad..f5a5b81f 100644
--- a/tools/xtop/test/fleet.test.ts
+++ b/tools/xtop/test/fleet.test.ts
@@ -8,7 +8,7 @@
import { describe, expect, test } from "bun:test";
import { toFleetNode, toFleetReport, type NodeRow } from "../src/lib/backend.ts";
import { configFromEnv, queryRows } from "../src/lib/clickhouse.ts";
-import { failedCheckName, groupByImage, healthPercent, imageLabel, labelPairs } from "../src/lib/fleet.ts";
+import { failedCheckName, groupByImage, healthPercent, imageLabel, labelPairs, treeSortKey } from "../src/lib/fleet.ts";
const BUILD = "d6d918ad22ff597579e6678bef75adca84d30f9661e0b7bbdbf534eebc138c1f";
const LOG = [
@@ -274,6 +274,28 @@ describe("groupByImage", () => {
});
});
+describe("treeSortKey", () => {
+ const sorted = (trees: { lastSeq: number | null; headSeq: number | null }[]) =>
+ [...trees].sort((a, b) => treeSortKey(a) - treeSortKey(b) || 0);
+
+ test("keeps the rows without a tree apart from those one step behind", () => {
+ const none = { lastSeq: null, headSeq: null };
+ const one = { lastSeq: 2, headSeq: 3 };
+ const far = { lastSeq: 1, headSeq: 9 };
+ const head = { lastSeq: 5, headSeq: 5 };
+ expect(treeSortKey(none)).toBeLessThan(treeSortKey(far));
+ expect(sorted([one, none, head, far, none, one])).toEqual([none, none, far, one, one, head]);
+ });
+
+ test("sorts an unknown head between the rows behind and those at their head", () => {
+ const unknown = { lastSeq: 7, headSeq: null };
+ const one = { lastSeq: 2, headSeq: 3 };
+ const head = { lastSeq: 5, headSeq: 5 };
+ expect(sorted([head, unknown, one, head])).toEqual([one, unknown, head, head]);
+ expect(treeSortKey(head)).toBe(0);
+ });
+});
+
describe("toFleetReport", () => {
test("dates the report by the newest computation", () => {
expect(toFleetReport(ROWS).generatedAt.getTime()).toBe(1_795_000_030_000);