// OxygenFlow in the editor, without VS Code: how runs and jobs look (icon, // colour, labels) and which runs just finished. Shared by the Flow view, the // status bar item and the tests. import type { FlowJob, FlowRun, TestCase } from "./hubapi"; /** A codicon and a theme colour id for a run's or job's status. */ export interface Look { /** Codicon id; `~spin` makes it turn. */ icon: string; /** A theme colour id, e.g. `testing.iconPassed`. */ color: string; /** A short word for screen readers and tooltips. */ word: string; } export function look(status: string): Look { switch (status) { case "passed": return { icon: "pass-filled", color: "testing.iconPassed", word: "passed" }; case "failed": return { icon: "error", color: "testing.iconFailed", word: "failed" }; case "error": return { icon: "warning", color: "testing.iconErrored", word: "could not run" }; case "running": return { icon: "sync~spin", color: "oxygenhub.flowRunning", word: "running" }; case "queued": return { icon: "clock", color: "testing.iconQueued", word: "queued" }; case "skipped": return { icon: "debug-step-over", color: "testing.iconSkipped", word: "skipped" }; default: return { icon: "circle-large-outline", color: "descriptionForeground", word: status }; } } export function live(status: string): boolean { return status === "queued" || status === "running"; } /** "3.2 s", "4 min 10 s". */ export function duration(ms: number): string { const s = Math.max(0, ms) / 1000; if (s < 60) { return `${s.toFixed(1)} s`; } return `${Math.floor(s / 60)} min ${Math.floor(s % 60)} s`; } /** "just now", "5 minutes ago", as the hub says it. */ export function ago(ms: number, now = Date.now()): string { const s = Math.max(0, Math.floor((now - ms) / 1000)); const unit = (n: number, w: string) => `${n} ${w}${n === 1 ? "" : "s"} ago`; if (s < 45) { return "just now"; } if (s < 90) { return "a minute ago"; } if (s < 2700) { return unit(Math.ceil(s / 60), "minute"); } if (s < 5400) { return "an hour ago"; } if (s < 79200) { return unit(Math.ceil(s / 3600), "hour"); } return unit(Math.ceil(s / 86400), "day"); } /** What a run ran on: its bookmark, or the change for a run on one change. */ export function subject(run: FlowRun): string { return run.bookmark || `change ${(run.heads[0] ?? "").slice(0, 12)}`; } /** "#12 main". */ export function runLabel(run: FlowRun): string { return `#${run.number} ${subject(run)}`; } /** "passed in 2 min 3 s · push by andy · 5 minutes ago". */ export function runDescription(run: FlowRun, now = Date.now()): string { const took = run.finished_ms ? ` in ${duration(run.finished_ms - run.created_ms)}` : ""; const queued = run.status === "queued" && run.queue_position ? ` (#${run.queue_position} in line)` : ""; const trigger = run.trigger.startsWith("rerun:") ? `re-run of #${run.trigger.slice(6)}` : run.trigger; return `${look(run.status).word}${took}${queued} · ${trigger} by ${run.actor} · ${ago(run.created_ms, now)}`; } /** * Runs that were live at the last look and are finished now, so the editor * can say so once. `before` maps a run's key to its last status. */ export function newlyFinished(before: Map, runs: { key: string; run: FlowRun }[]): { key: string; run: FlowRun }[] { return runs.filter(({ key, run }) => { const was = before.get(key); return was !== undefined && live(was) && !live(run.status); }); } /** Whether a job waits on someone's approval before it runs (oxygenflow#57). */ export function waitingApproval(job: FlowJob): boolean { return job.approval === "waiting" && job.status === "queued"; } /** MiB as the hub's pages say it: "900 MiB", "2.1 GiB". */ function mib(n: number): string { return n >= 1024 ? `${(n / 1024).toFixed(1)} GiB` : `${n} MiB`; } /** * Where a job's time went (#348): how long it waited for a runner, each * phase its runner reported, and its peak disk and memory. Empty when the * hub reported none of it. */ export function jobTimings(job: FlowJob): string[] { const out: string[] = []; if (job.queued_ms && job.started_ms) { out.push(`waited ${duration(job.started_ms - job.queued_ms)}`); } for (const p of job.phases ?? []) { out.push(`${p.name} ${p.ms < 1000 ? `${p.ms} ms` : duration(p.ms)}`); } const peaks = [job.peak_disk_mib ? `disk ${mib(job.peak_disk_mib)}` : "", job.peak_memory_mib ? `memory ${mib(job.peak_memory_mib)}` : ""].filter(Boolean); if (peaks.length) { out.push(`peak ${peaks.join(", ")}`); } return out; } /** A test report's cases by suite, in the order they came, for the Testing view. */ export function casesBySuite(cases: TestCase[]): Map { const out = new Map(); for (const c of cases) { const suite = c.suite || c.classname || "tests"; out.set(suite, [...(out.get(suite) ?? []), c]); } return out; } /** A case's id in the Testing view: stable across reports, so its history lines up. */ export function caseId(repo: string, c: TestCase): string { return `${repo}::${c.suite || c.classname || "tests"}::${c.classname}::${c.name}`; } /** What VS Code should show for a case's status: passed, failed, errored or skipped. */ export function caseOutcome(status: string): "passed" | "failed" | "errored" | "skipped" { return status === "passed" ? "passed" : status === "failed" ? "failed" : status === "error" ? "errored" : "skipped"; }