Coverage / src/flowview.ts 91.8%
90 of 98 lines run, 10 of 10 functions, 32 of 40 branches · run #23 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 { FlowRun } 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 | 6× | export function look(status: string): Look { |
| 18 | 6× | switch (status) { |
| 19 | 6× | case "passed": |
| 20 | 6× | return { icon: "pass-filled", color: "testing.iconPassed", word: "passed" }; |
| 21 | 6× | case "failed": |
| 22 | 6× | return { icon: "error", color: "testing.iconFailed", word: "failed" }; |
| 23 | 6× | case "error": |
| 24 | 6× | return { icon: "warning", color: "testing.iconErrored", word: "could not run" }; |
| 25 | 6× | case "running": |
| 26 | 6× | return { icon: "sync~spin", color: "oxygenhub.flowRunning", word: "running" }; |
| 27 | 6× | case "queued": |
| 28 | 6× | return { icon: "clock", color: "testing.iconQueued", word: "queued" }; |
| 29 | 6× | case "skipped": |
| 30 | 6× | return { icon: "debug-step-over", color: "testing.iconSkipped", word: "skipped" }; |
| 31 | 6× | default: |
| 32 | 6× | return { icon: "circle-large-outline", color: "descriptionForeground", word: status }; |
| 33 | 6× | } |
| 34 | 6× | } |
| 35 | 1× | |
| 36 | 1× | export function live(status: string): boolean { |
| 37 | 8× | return status === "queued" || status === "running"; |
| 38 | 2× | } |
| 39 | 2× | |
| 40 | 2× | /** "3.2 s", "4 min 10 s". */ |
| 41 | 2× | export function duration(ms: number): string { |
| 42 | 2× | const s = Math.max(0, ms) / 1000; |
| 43 | 2× | if (s < 60) { |
| 44 | 1× | return `${s.toFixed(1)} s`; |
| 45 | 1× | } |
| 46 | 2× | return `${Math.floor(s / 60)} min ${Math.floor(s % 60)} s`; |
| 47 | 2× | } |
| 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 | 3× | const s = Math.max(0, Math.floor((now - ms) / 1000)); |
| 52 | 3× | const unit = (n: number, w: string) => `${n} ${w}${n === 1 ? "" : "s"} ago`; |
| 53 | 3× | if (s < 45) { |
| 54 | 1× | return "just now"; |
| 55 | 1× | } |
| 56 | 3× | if (s < 90) { |
| 57 | 0× | return "a minute ago"; |
| 58 | 0× | } |
| 59 | 3× | if (s < 2700) { |
| 60 | 1× | return unit(Math.ceil(s / 60), "minute"); |
| 61 | 1× | } |
| 62 | 3× | 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 | 3× | return unit(Math.ceil(s / 86400), "day"); |
| 69 | 3× | } |
| 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 | 2× | |
| 81 | 2× | /** "passed in 2 min 3 s · push by andy · 5 minutes ago". */ |
| 82 | 2× | 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 | 2× | } |
| 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 | 1× | } |