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

121 lines
4.1 KiB
TypeScript

import { computed, ref, watch, type Ref } from "vue";
import { api } from "@/lib/api";
import { useLiveResource } from "@/stores/session";
import type { StageRun } from "./workflow-graph";
import type { Task } from "@/tasks/task";
export type WorkflowRun = Task & { workflow?: string };
interface AttemptsView {
attempts?: { attempt: number; stages?: StageRun[] }[];
}
interface DebugView {
task?: { workflow_definition_yaml?: string };
}
const RECENT_RUNS = 20;
/**
* The recent runs of one workflow and the step states of the one being
* watched, kept current from the live task stream. The newest run is watched
* until the operator picks another.
*/
export function useWorkflowRuns(workflow: Ref<string>) {
const runs = ref<WorkflowRun[]>([]);
const watched = ref("");
const stages = ref<StageRun[]>([]);
const attempt = ref(0);
// The task list leaves the pinned definition out, so the watched run's is
// read from its own record.
const pinned = ref<string | undefined>(undefined);
let picked = false;
const error = ref("");
const loading = ref(false);
let requestNumber = 0;
async function loadRuns(): Promise<void> {
// The server filters by workflow: filtering a global page here would miss
// every run older than that page.
const requested = workflow.value;
const request = ++requestNumber;
loading.value = true;
try {
const page = requested ? await api.taskPage<WorkflowRun>({ workflow: requested, limit: RECENT_RUNS }) : null;
if (workflow.value !== requested || request !== requestNumber) return;
runs.value = (page?.tasks ?? []).sort((a, b) => Number(b.id) - Number(a.id));
error.value = "";
if (!picked || (watched.value && !runs.value.some((run) => String(run.id) === watched.value)))
watched.value = runs.value[0] ? String(runs.value[0].id) : "";
} catch (cause) {
if (workflow.value === requested && request === requestNumber) error.value = String((cause as Error).message || cause);
} finally {
if (request === requestNumber) loading.value = false;
}
}
async function loadStages(): Promise<void> {
if (!watched.value) {
if (stages.value.length) stages.value = [];
attempt.value = 0;
pinned.value = undefined;
return;
}
const requested = watched.value;
const [view, debug] = await Promise.all([
api.taskAttempts<AttemptsView>(requested).catch(() => null),
pinned.value === undefined ? api.taskDebug<DebugView>(requested).catch(() => null) : null,
]);
if (watched.value !== requested) return;
if (debug) pinned.value = debug.task?.workflow_definition_yaml ?? "";
const latest = [...(view?.attempts ?? [])].sort((a, b) => b.attempt - a.attempt)[0];
// A live refresh that changed nothing must not redraw the canvas, which
// would close any menu open on it.
const next = latest?.stages ?? [];
if (JSON.stringify(next) !== JSON.stringify(stages.value)) stages.value = next;
attempt.value = latest?.attempt ?? 0;
}
// "Current definition" is a choice too, and a refresh must not undo it.
function pick(id: string): void {
picked = true;
watched.value = id;
}
async function refresh(): Promise<void> {
await loadRuns();
await loadStages();
}
watch(workflow, () => {
picked = false;
runs.value = [];
watched.value = "";
error.value = "";
void refresh();
}, { immediate: true });
watch(watched, () => {
stages.value = [];
attempt.value = 0;
pinned.value = undefined;
void loadStages();
});
useLiveResource("tasks", () => void refresh(), 300);
const watchedRun = computed(() => {
const run = runs.value.find((run) => String(run.id) === watched.value);
return run && pinned.value ? { ...run, workflow_definition_yaml: pinned.value } : run;
});
/** Watches a run that just started, once the task list has it. */
async function follow(id: number | string): Promise<void> {
picked = true;
watched.value = String(id);
await refresh();
}
return { runs, error, loading, watched, watchedRun, stages, attempt, pick, follow, refresh };
}