Coverage / src/flowview.ts 94.6%
140 of 148 lines run, 16 of 16 functions, 59 of 70 branches · run #24 on main · run missed
| 1 | 0× | // OxygenFlow in the editor, without VS Code: how runs and jobs look (icon, |
| 2 | 0× | // colour, labels) and which runs just finished. Shared by the Flow view, the |
| 3 | 0× | // status bar item and the tests. |
| 4 | 1× | |
| 5 | import type { FlowJob, FlowRun, TestCase } from "./hubapi"; | |
| 6 | 1× | |
| 7 | 0× | /** A codicon and a theme colour id for a run's or job's status. */ |
| 8 | export interface Look { | |
| 9 | /** Codicon id; `~spin` makes it turn. */ | |
| 10 | icon: string; | |
| 11 | /** A theme colour id, e.g. `testing.iconPassed`. */ | |
| 12 | color: string; | |
| 13 | /** A short word for screen readers and tooltips. */ | |
| 14 | word: string; | |
| 15 | } | |
| 16 | 1× | |
| 17 | 1× | export function look(status: string): Look { |
| 18 | 1× | switch (status) { |
| 19 | 1× | case "passed": |
| 20 | 1× | return { icon: "pass-filled", color: "testing.iconPassed", word: "passed" }; |
| 21 | 1× | case "failed": |
| 22 | 1× | return { icon: "error", color: "testing.iconFailed", word: "failed" }; |
| 23 | 1× | case "error": |
| 24 | 1× | return { icon: "warning", color: "testing.iconErrored", word: "could not run" }; |
| 25 | 1× | case "running": |
| 26 | 1× | return { icon: "sync~spin", color: "oxygenhub.flowRunning", word: "running" }; |
| 27 | 1× | case "queued": |
| 28 | 1× | return { icon: "clock", color: "testing.iconQueued", word: "queued" }; |
| 29 | 1× | case "skipped": |
| 30 | 1× | return { icon: "debug-step-over", color: "testing.iconSkipped", word: "skipped" }; |
| 31 | 1× | default: |
| 32 | 1× | return { icon: "circle-large-outline", color: "descriptionForeground", word: status }; |
| 33 | 1× | } |
| 34 | 1× | } |
| 35 | 1× | |
| 36 | 1× | export function live(status: string): boolean { |
| 37 | 8× | return status === "queued" || status === "running"; |
| 38 | 4× | } |
| 39 | 4× | |
| 40 | 4× | /** "3.2 s", "4 min 10 s". */ |
| 41 | 4× | export function duration(ms: number): string { |
| 42 | 4× | const s = Math.max(0, ms) / 1000; |
| 43 | 4× | if (s < 60) { |
| 44 | 2× | return `${s.toFixed(1)} s`; |
| 45 | 2× | } |
| 46 | 4× | return `${Math.floor(s / 60)} min ${Math.floor(s % 60)} s`; |
| 47 | 4× | } |
| 48 | 1× | |
| 49 | 1× | /** "just now", "5 minutes ago", as the hub says it. */ |
| 50 | 1× | export function ago(ms: number, now = Date.now()): string { |
| 51 | 4× | const s = Math.max(0, Math.floor((now - ms) / 1000)); |
| 52 | 4× | const unit = (n: number, w: string) => `${n} ${w}${n === 1 ? "" : "s"} ago`; |
| 53 | 4× | if (s < 45) { |
| 54 | 1× | return "just now"; |
| 55 | 1× | } |
| 56 | 4× | if (s < 90) { |
| 57 | 0× | return "a minute ago"; |
| 58 | 0× | } |
| 59 | 4× | if (s < 2700) { |
| 60 | 1× | return unit(Math.ceil(s / 60), "minute"); |
| 61 | 1× | } |
| 62 | 4× | if (s < 5400) { |
| 63 | 0× | return "an hour ago"; |
| 64 | 0× | } |
| 65 | 1× | if (s < 79200) { |
| 66 | 1× | return unit(Math.ceil(s / 3600), "hour"); |
| 67 | 1× | } |
| 68 | 4× | return unit(Math.ceil(s / 86400), "day"); |
| 69 | 4× | } |
| 70 | 1× | |
| 71 | 1× | /** What a run ran on: its bookmark, or the change for a run on one change. */ |
| 72 | 1× | export function subject(run: FlowRun): string { |
| 73 | 2× | return run.bookmark || `change ${(run.heads[0] ?? "").slice(0, 12)}`; |
| 74 | 2× | } |
| 75 | 2× | |
| 76 | 2× | /** "#12 main". */ |
| 77 | 2× | export function runLabel(run: FlowRun): string { |
| 78 | 2× | return `#${run.number} ${subject(run)}`; |
| 79 | 2× | } |
| 80 | 1× | |
| 81 | 1× | /** "passed in 2 min 3 s · push by andy · 5 minutes ago". */ |
| 82 | 1× | export function runDescription(run: FlowRun, now = Date.now()): string { |
| 83 | 2× | const took = run.finished_ms ? ` in ${duration(run.finished_ms - run.created_ms)}` : ""; |
| 84 | 2× | const queued = run.status === "queued" && run.queue_position ? ` (#${run.queue_position} in line)` : ""; |
| 85 | 2× | const trigger = run.trigger.startsWith("rerun:") ? `re-run of #${run.trigger.slice(6)}` : run.trigger; |
| 86 | 2× | return `${look(run.status).word}${took}${queued} · ${trigger} by ${run.actor} · ${ago(run.created_ms, now)}`; |
| 87 | 1× | } |
| 88 | 1× | |
| 89 | 1× | /** |
| 90 | 1× | * Runs that were live at the last look and are finished now, so the editor |
| 91 | 1× | * can say so once. `before` maps a run's key to its last status. |
| 92 | 1× | */ |
| 93 | 1× | export function newlyFinished(before: Map<string, string>, runs: { key: string; run: FlowRun }[]): { key: string; run: FlowRun }[] { |
| 94 | 1× | return runs.filter(({ key, run }) => { |
| 95 | 4× | const was = before.get(key); |
| 96 | 4× | return was !== undefined && live(was) && !live(run.status); |
| 97 | 1× | }); |
| 98 | 3× | } |
| 99 | 3× | |
| 100 | 3× | /** Whether a job waits on someone's approval before it runs (oxygenflow#57). */ |
| 101 | 3× | export function waitingApproval(job: FlowJob): boolean { |
| 102 | 3× | return job.approval === "waiting" && job.status === "queued"; |
| 103 | 3× | } |
| 104 | 1× | |
| 105 | 1× | /** MiB as the hub's pages say it: "900 MiB", "2.1 GiB". */ |
| 106 | 2× | function mib(n: number): string { |
| 107 | 2× | return n >= 1024 ? `${(n / 1024).toFixed(1)} GiB` : `${n} MiB`; |
| 108 | 2× | } |
| 109 | 2× | |
| 110 | 2× | /** |
| 111 | 2× | * Where a job's time went (#348): how long it waited for a runner, each |
| 112 | 2× | * phase its runner reported, and its peak disk and memory. Empty when the |
| 113 | 2× | * hub reported none of it. |
| 114 | 2× | */ |
| 115 | 2× | export function jobTimings(job: FlowJob): string[] { |
| 116 | 2× | const out: string[] = []; |
| 117 | 2× | if (job.queued_ms && job.started_ms) { |
| 118 | 1× | out.push(`waited ${duration(job.started_ms - job.queued_ms)}`); |
| 119 | 1× | } |
| 120 | 2× | for (const p of job.phases ?? []) { |
| 121 | 2× | out.push(`${p.name} ${p.ms < 1000 ? `${p.ms} ms` : duration(p.ms)}`); |
| 122 | 2× | } |
| 123 | 2× | const peaks = [job.peak_disk_mib ? `disk ${mib(job.peak_disk_mib)}` : "", job.peak_memory_mib ? `memory ${mib(job.peak_memory_mib)}` : ""].filter(Boolean); |
| 124 | 2× | if (peaks.length) { |
| 125 | 1× | out.push(`peak ${peaks.join(", ")}`); |
| 126 | 1× | } |
| 127 | 2× | return out; |
| 128 | 2× | } |
| 129 | 1× | |
| 130 | 1× | /** A test report's cases by suite, in the order they came, for the Testing view. */ |
| 131 | 1× | export function casesBySuite(cases: TestCase[]): Map<string, TestCase[]> { |
| 132 | 1× | const out = new Map<string, TestCase[]>(); |
| 133 | 1× | for (const c of cases) { |
| 134 | 4× | const suite = c.suite || c.classname || "tests"; |
| 135 | 4× | out.set(suite, [...(out.get(suite) ?? []), c]); |
| 136 | 4× | } |
| 137 | 1× | return out; |
| 138 | 1× | } |
| 139 | 1× | |
| 140 | 1× | /** A case's id in the Testing view: stable across reports, so its history lines up. */ |
| 141 | 1× | export function caseId(repo: string, c: TestCase): string { |
| 142 | 1× | return `${repo}::${c.suite || c.classname || "tests"}::${c.classname}::${c.name}`; |
| 143 | 1× | } |
| 144 | 1× | |
| 145 | 1× | /** What VS Code should show for a case's status: passed, failed, errored or skipped. */ |
| 146 | 1× | export function caseOutcome(status: string): "passed" | "failed" | "errored" | "skipped" { |
| 147 | 4× | return status === "passed" ? "passed" : status === "failed" ? "failed" : status === "error" ? "errored" : "skipped"; |
| 148 | 4× | } |