sys_query is conditions joined by ^, each field=value, field!=value or fieldLIKEtext. A bad condition or a field a list does not offer is a 400 naming the fields it accepts, so a typo never reads as an empty page. Runs, workflows, proposals, guards and action types take it, and the dashboard keeps the filter in the URL so a filtered list is a link. The guard control API is list-guard-controls at /api/controls/guards: list-guards belongs to the connectors extension list, and the resource is a guard. JudgeStanding keeps its name, since it is the model judgement a guard recorded.
107 lines
4.4 KiB
TypeScript
107 lines
4.4 KiB
TypeScript
import { computed, onMounted, watch, type Ref } from "vue";
|
|
|
|
import { api, type Trigger } from "@/lib/api";
|
|
import { usePoll, useRead } from "@/lib/use-read";
|
|
import { useLiveResource } from "@/stores/session";
|
|
|
|
export interface WorkflowRow {
|
|
id: string;
|
|
name: string;
|
|
/** The published version; 0 for a workflow with only a draft. */
|
|
version: number;
|
|
/** The published document, which says what a run is asked for. */
|
|
body: string;
|
|
steps: number;
|
|
published: string;
|
|
by?: string;
|
|
enabled: boolean;
|
|
/** Has an unpublished edit. */
|
|
draft: boolean;
|
|
/** What starts it, one entry per distinct way. */
|
|
starts: string[];
|
|
runs: number;
|
|
succeeded: number;
|
|
running: number;
|
|
last?: { id: string; state: string; created_at: string };
|
|
}
|
|
|
|
const time = (at?: string) => (at ? Date.parse(at) : 0);
|
|
|
|
/** "24h0m0s" as the server prints a Go duration, "24h" as a person reads it. */
|
|
export function interval(duration: string): string {
|
|
return duration.replace(/(?<=\D)0s$/, "").replace(/(?<=\D)0m$/, "");
|
|
}
|
|
|
|
/** How a trigger starts its workflow, in words. */
|
|
export function startedBy(trigger: Trigger): string {
|
|
return trigger.every ? `Every ${interval(trigger.every)}` : `${trigger.source} ${trigger.declared_event_type}`;
|
|
}
|
|
|
|
/** Running workflows first, then by how recently they ran, then by name. */
|
|
export function byActivity(a: WorkflowRow, b: WorkflowRow): number {
|
|
const live = (b.running ? 1 : 0) - (a.running ? 1 : 0);
|
|
if (live) return live;
|
|
return time(b.last?.created_at) - time(a.last?.created_at) || a.name.localeCompare(b.name);
|
|
}
|
|
|
|
/** The workflows, published or only drafted, with what starts each and how its
|
|
* runs went. The counts come from the server, which counts every run. The
|
|
* server filters the published list, so a filter narrows that. */
|
|
export function useWorkflowList(sysQuery: Ref<string> | string = "") {
|
|
const filter = computed(() => (typeof sysQuery === "string" ? sysQuery : sysQuery.value));
|
|
const workflows = useRead(() => api.workflowList(filter.value));
|
|
watch(filter, () => void workflows.reload());
|
|
const drafts = useRead(() => api.workflowDrafts());
|
|
const triggers = useRead(() => api.triggers());
|
|
const stats = useRead(() => api.workflowStats());
|
|
const reload = () => Promise.all([workflows.reload(), drafts.reload(), triggers.reload(), stats.reload()]).then(() => undefined);
|
|
onMounted(reload);
|
|
useLiveResource(null, () => void reload());
|
|
usePoll(reload);
|
|
|
|
const rows = computed<WorkflowRow[]>(() => {
|
|
const counted = new Map((stats.data.value ?? []).map((s) => [s.workflow_id, s]));
|
|
const held = new Set((drafts.data.value ?? []).map((d) => d.workflow_id));
|
|
const row = (id: string, name: string, own: Partial<WorkflowRow>): WorkflowRow => {
|
|
const s = counted.get(id);
|
|
return {
|
|
id,
|
|
name,
|
|
version: 0,
|
|
body: "",
|
|
steps: 0,
|
|
published: "",
|
|
enabled: true,
|
|
draft: held.has(id),
|
|
starts: [...new Set((triggers.data.value ?? []).filter((t) => t.workflow_id === id).map(startedBy))],
|
|
runs: s?.total ?? 0,
|
|
succeeded: s?.succeeded ?? 0,
|
|
running: s?.active ?? 0,
|
|
last: s ? { id: s.last_run.id, state: s.last_run.state, created_at: s.last_run.at } : undefined,
|
|
...own,
|
|
};
|
|
};
|
|
const published = (workflows.data.value ?? []).map((w) =>
|
|
row(w.id, w.name, { version: w.version, body: w.body, steps: w.steps?.length ?? 0, published: w.created_at, by: w.created_by, enabled: w.enabled }),
|
|
);
|
|
const known = new Set(published.map((r) => r.id));
|
|
// The server filters published workflows only, so a draft with no version
|
|
// is left out of a filtered list rather than shown unfiltered.
|
|
const drafted = (drafts.data.value ?? [])
|
|
.filter((d) => !known.has(d.workflow_id) && !filter.value)
|
|
.map((d) => row(d.workflow_id, d.name, { body: d.body, published: d.updated_at, by: d.updated_by }));
|
|
return [...published, ...drafted].sort(byActivity);
|
|
});
|
|
/** Triggers, drafts or counts that failed to load: rows show without them, beside this. */
|
|
const partial = computed(() => triggers.error.value || stats.error.value || drafts.error.value);
|
|
return {
|
|
rows,
|
|
error: workflows.error,
|
|
partial,
|
|
loading: computed(() => !workflows.data.value && !workflows.error.value),
|
|
reload,
|
|
triggersKnown: computed(() => !!triggers.data.value),
|
|
runsKnown: computed(() => !!stats.data.value),
|
|
};
|
|
}
|