|
| 1 | +import type { FlatTree, FlatTreeItem } from "~/components/primitives/TreeView/TreeView"; |
| 2 | +import type { SessionTraceRun, SessionTraceSpan } from "./sessionTraceTypes"; |
| 3 | + |
| 4 | +/** Quiet periods longer than this between two runs fold into one row. */ |
| 5 | +export const GAP_FOLD_MS = 10 * 60_000; |
| 6 | + |
| 7 | +export type SessionTreeRow<TSpanData> = |
| 8 | + | { kind: "session"; runCount: number } |
| 9 | + | { kind: "loadEarlier"; count: number } |
| 10 | + | { kind: "notLoaded"; count: number; olderRunFriendlyId: string; newerRunFriendlyId: string } |
| 11 | + | { kind: "gap"; durationMs: number } |
| 12 | + | { kind: "run"; run: SessionTraceRun; spansLoaded: boolean } |
| 13 | + | { kind: "span"; runFriendlyId: string; spanId: string; span: TSpanData }; |
| 14 | + |
| 15 | +type SessionTreeNode<TSpanData> = FlatTreeItem<SessionTreeRow<TSpanData>>; |
| 16 | + |
| 17 | +const SESSION_NODE_ID = "session"; |
| 18 | +const LOAD_EARLIER_NODE_ID = "loadEarlier"; |
| 19 | + |
| 20 | +export function runNodeId(runFriendlyId: string) { |
| 21 | + return `run:${runFriendlyId}`; |
| 22 | +} |
| 23 | + |
| 24 | +export function spanNodeId(runFriendlyId: string, spanId: string) { |
| 25 | + return `${runFriendlyId}:${spanId}`; |
| 26 | +} |
| 27 | + |
| 28 | +/** Rows that only label the list: arrow keys skip them and they can't be selected. */ |
| 29 | +export function isSelectableRow(row: SessionTreeRow<unknown>) { |
| 30 | + return row.kind === "session" || row.kind === "run" || row.kind === "span"; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * One flat list for the whole session: the session row, then runs oldest first, each followed |
| 35 | + * by its loaded spans. Runs can come from several pages; duplicates are merged by friendlyId, |
| 36 | + * the later copy winning, and missing run numbers between pages become a "not loaded" row. |
| 37 | + */ |
| 38 | +export function buildSessionTree<TSpanData>({ |
| 39 | + runs, |
| 40 | + totalCount, |
| 41 | + spansByRun, |
| 42 | +}: { |
| 43 | + runs: SessionTraceRun[]; |
| 44 | + totalCount: number; |
| 45 | + /** Loaded events per run friendlyId, in trace order (parents before children). */ |
| 46 | + spansByRun: ReadonlyMap<string, SessionTraceSpan<TSpanData>[]>; |
| 47 | +}): FlatTree<SessionTreeRow<TSpanData>> { |
| 48 | + const session: SessionTreeNode<TSpanData> = { |
| 49 | + id: SESSION_NODE_ID, |
| 50 | + children: [], |
| 51 | + hasChildren: false, |
| 52 | + level: 0, |
| 53 | + data: { kind: "session", runCount: totalCount }, |
| 54 | + }; |
| 55 | + const tree: SessionTreeNode<TSpanData>[] = [session]; |
| 56 | + |
| 57 | + const addSessionChild = (id: string, data: SessionTreeRow<TSpanData>) => { |
| 58 | + session.children.push(id); |
| 59 | + const node: SessionTreeNode<TSpanData> = { |
| 60 | + id, |
| 61 | + parentId: SESSION_NODE_ID, |
| 62 | + children: [], |
| 63 | + hasChildren: false, |
| 64 | + level: 1, |
| 65 | + data, |
| 66 | + }; |
| 67 | + tree.push(node); |
| 68 | + return node; |
| 69 | + }; |
| 70 | + |
| 71 | + const ordered = uniqueRunsOldestFirst(runs); |
| 72 | + const oldest = ordered[0]; |
| 73 | + if (oldest && oldest.number > 1) { |
| 74 | + addSessionChild(LOAD_EARLIER_NODE_ID, { kind: "loadEarlier", count: oldest.number - 1 }); |
| 75 | + } |
| 76 | + |
| 77 | + let previous: SessionTraceRun | undefined; |
| 78 | + for (const run of ordered) { |
| 79 | + if (previous) { |
| 80 | + const missing = run.number - previous.number - 1; |
| 81 | + if (missing > 0) { |
| 82 | + addSessionChild(`notLoaded:${run.friendlyId}`, { |
| 83 | + kind: "notLoaded", |
| 84 | + count: missing, |
| 85 | + olderRunFriendlyId: previous.friendlyId, |
| 86 | + newerRunFriendlyId: run.friendlyId, |
| 87 | + }); |
| 88 | + } else { |
| 89 | + const gapMs = quietGapMs(previous, run); |
| 90 | + if (gapMs !== undefined && gapMs > GAP_FOLD_MS) { |
| 91 | + addSessionChild(`gap:${run.friendlyId}`, { kind: "gap", durationMs: gapMs }); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const spans = spansByRun.get(run.friendlyId); |
| 97 | + const runNode = addSessionChild(runNodeId(run.friendlyId), { |
| 98 | + kind: "run", |
| 99 | + run, |
| 100 | + spansLoaded: spans !== undefined, |
| 101 | + }); |
| 102 | + if (spans) graftSpans(tree, runNode, run.friendlyId, spans); |
| 103 | + previous = run; |
| 104 | + } |
| 105 | + |
| 106 | + session.hasChildren = session.children.length > 0; |
| 107 | + return tree; |
| 108 | +} |
| 109 | + |
| 110 | +function uniqueRunsOldestFirst(runs: SessionTraceRun[]) { |
| 111 | + const byId = new Map<string, SessionTraceRun>(); |
| 112 | + for (const run of runs) byId.set(run.friendlyId, run); |
| 113 | + return [...byId.values()].sort((a, b) => a.number - b.number); |
| 114 | +} |
| 115 | + |
| 116 | +function quietGapMs(previous: SessionTraceRun, next: SessionTraceRun) { |
| 117 | + if (!previous.finishedAt || !next.createdAt) return undefined; |
| 118 | + return new Date(next.createdAt).getTime() - new Date(previous.finishedAt).getTime(); |
| 119 | +} |
| 120 | + |
| 121 | +// The run's root span is hidden: the run row stands in for it, so its children move up to the run. |
| 122 | +function graftSpans<TSpanData>( |
| 123 | + tree: SessionTreeNode<TSpanData>[], |
| 124 | + runNode: SessionTreeNode<TSpanData>, |
| 125 | + runFriendlyId: string, |
| 126 | + spans: SessionTraceSpan<TSpanData>[] |
| 127 | +) { |
| 128 | + const grafted = new Map<string, SessionTreeNode<TSpanData>>(); |
| 129 | + |
| 130 | + for (const span of spans) { |
| 131 | + if (span.level === 0) continue; |
| 132 | + |
| 133 | + const parent = (span.parentId && grafted.get(span.parentId)) || runNode; |
| 134 | + const node: SessionTreeNode<TSpanData> = { |
| 135 | + id: spanNodeId(runFriendlyId, span.id), |
| 136 | + parentId: parent.id, |
| 137 | + children: [], |
| 138 | + hasChildren: false, |
| 139 | + level: parent.level + 1, |
| 140 | + data: { kind: "span", runFriendlyId, spanId: span.id, span: span.data }, |
| 141 | + }; |
| 142 | + parent.children.push(node.id); |
| 143 | + parent.hasChildren = true; |
| 144 | + grafted.set(span.id, node); |
| 145 | + tree.push(node); |
| 146 | + } |
| 147 | +} |
0 commit comments