Files
archie/ui/src/workflows/workflow-summary.ts
T

33 lines
1.4 KiB
TypeScript

import { parse } from "yaml";
import type { WorkflowRow } from "./workflow-rows";
/** The steps a definition runs: parallel branches count their own steps, not
* the step that fans out. Undefined when the YAML does not parse. */
export function stepCount(source: string): number | undefined {
let doc: unknown;
try {
doc = parse(source);
} catch {
return undefined;
}
const count = (steps: unknown): number =>
Array.isArray(steps)
? steps.reduce<number>((total, step) => {
const branches = step && typeof step === "object" ? (step as { parallel?: Record<string, unknown> }).parallel : undefined;
return total + (branches && typeof branches === "object" ? Object.values(branches).reduce<number>((n, branch) => n + count(branch), 0) : 1);
}, 0)
: 0;
return doc && typeof doc === "object" ? count((doc as { steps?: unknown }).steps) : undefined;
}
/** Running workflows first, then by how recently they ran, then those that
* never ran by name: what is happening or just happened is what an operator
* opens this page to find. */
export function byActivity(a: WorkflowRow, b: WorkflowRow): number {
const live = (b.running ? 1 : 0) - (a.running ? 1 : 0);
if (live) return live;
const recent = (b.last_run_at ? Date.parse(b.last_run_at) : 0) - (a.last_run_at ? Date.parse(a.last_run_at) : 0);
return recent || a.id.localeCompare(b.id);
}