@jive-sh/workflow-platform 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -17
- package/dist/cpu-limit/limiter.js +54 -11
- package/dist/cpu-limit/limiter.js.map +1 -1
- package/dist/index.js +40 -47
- package/dist/index.js.map +1 -1
- package/dist/keep-running.js +5 -5
- package/dist/keep-running.js.map +1 -1
- package/dist/pick-wakes.js +7 -1
- package/dist/pick-wakes.js.map +1 -1
- package/dist/queue-counts.js +47 -0
- package/dist/queue-counts.js.map +1 -0
- package/dist/steps.js +35 -15
- package/dist/steps.js.map +1 -1
- package/dist/storage/budgets.js +14 -0
- package/dist/storage/budgets.js.map +1 -1
- package/dist/storage/sqlite-store.js +39 -18
- package/dist/storage/sqlite-store.js.map +1 -1
- package/dist/views/workflows-view.js +76 -61
- package/dist/views/workflows-view.js.map +1 -1
- package/package.json +1 -1
- package/src/cpu-limit/limiter.ts +61 -11
- package/src/dashboard/dashboard.css +78 -0
- package/src/dashboard/index.tsx +3 -0
- package/src/dashboard/queue-chart.tsx +137 -0
- package/src/dashboard/step-graph.tsx +98 -0
- package/src/dashboard/workflow-dag.tsx +256 -0
- package/src/dashboard/workflows-dashboard.tsx +313 -0
- package/src/index.ts +43 -48
- package/src/keep-running.ts +5 -7
- package/src/pick-wakes.ts +9 -2
- package/src/queue-counts.ts +61 -0
- package/src/steps.ts +35 -23
- package/src/storage/budgets.ts +13 -0
- package/src/storage/sqlite-store.ts +45 -21
- package/src/storage/step-records.ts +7 -6
- package/src/views/workflows-view.ts +120 -89
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* EACH QUEUE OVER TIME, small: the tasks of a workflow in one bucket (queued, done, …) as a thin line, every line named in
|
|
5
|
+
* words at its end and in the key, and told apart by its dash as well as its shade, never by colour alone. Which lines show
|
|
6
|
+
* is the reader's choice, kept in this browser. Reads only the platform's view: its `queueHistory`, a row where a count moved,
|
|
7
|
+
* and each workflow's queue now.
|
|
8
|
+
*/
|
|
9
|
+
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
10
|
+
import type { QueueCount } from '../storage/task-store';
|
|
11
|
+
import type { WorkflowsView } from '../views/workflows-view';
|
|
12
|
+
import { localTime } from '../views/format';
|
|
13
|
+
|
|
14
|
+
/** A line: one workflow's tasks in one bucket. */
|
|
15
|
+
interface Series {
|
|
16
|
+
readonly key: string;
|
|
17
|
+
readonly workflow: string;
|
|
18
|
+
readonly bucket: string;
|
|
19
|
+
readonly points: ReadonlyArray<{ readonly at: number; readonly tasks: number }>;
|
|
20
|
+
readonly now: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const STORED_CHOICE = 'workflow-platform.queue-chart.lines';
|
|
24
|
+
/** Lines shown before the reader chooses: the queues that finished most. */
|
|
25
|
+
const SHOWN_AT_FIRST = 3;
|
|
26
|
+
const DASHES = ['', '5 3', '1.5 2.5', '7 2 1.5 2', '3 3'];
|
|
27
|
+
/** The host page's colours, so a line keeps its contrast in a light theme and a dark one alike. */
|
|
28
|
+
const SHADES = ['var(--wp-fg)', 'var(--wp-ok)', 'var(--accent, #5aa9e6)', 'var(--near, #dfa33c)', 'var(--wp-dim)'];
|
|
29
|
+
|
|
30
|
+
const keyOf = (workflow: string, bucket: string) => `${workflow} · ${bucket}`;
|
|
31
|
+
|
|
32
|
+
/** Every queue and bucket as a line: its recorded changes, then its count now. Pure. */
|
|
33
|
+
export const queueSeries = (view: WorkflowsView): ReadonlyArray<Series> =>
|
|
34
|
+
view.groups.flatMap((g) => g.workflows).flatMap((w) => (w.queue === null ? [] : Object.entries(w.queue).map(([bucket, now]) => ({
|
|
35
|
+
key: keyOf(w.name, bucket), workflow: w.name, bucket, now,
|
|
36
|
+
points: [...view.queueHistory.filter((r: QueueCount) => r.workflow === w.name && r.bucket === bucket).map((r) => ({ at: r.at, tasks: r.tasks })), { at: view.at, tasks: now }],
|
|
37
|
+
}))));
|
|
38
|
+
|
|
39
|
+
const readChoice = (): ReadonlyArray<string> | null => {
|
|
40
|
+
try {
|
|
41
|
+
const stored = window.localStorage.getItem(STORED_CHOICE);
|
|
42
|
+
return stored === null ? null : (JSON.parse(stored) as ReadonlyArray<string>);
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export function QueueChart({ view }: { readonly view: WorkflowsView }) {
|
|
49
|
+
const all = queueSeries(view);
|
|
50
|
+
const [chosen, setChosen] = useState<ReadonlyArray<string> | null>(null);
|
|
51
|
+
useEffect(() => setChosen(readChoice()), []);
|
|
52
|
+
const shownAtFirst = [...all].filter((s) => s.bucket === 'done' && s.now > 0).sort((a, b) => b.now - a.now).slice(0, SHOWN_AT_FIRST).map((s) => s.key);
|
|
53
|
+
const shownKeys = chosen ?? shownAtFirst;
|
|
54
|
+
const shown = all.filter((s) => shownKeys.includes(s.key));
|
|
55
|
+
const choose = (key: string, on: boolean) => {
|
|
56
|
+
const next = on ? [...shownKeys, key] : shownKeys.filter((k) => k !== key);
|
|
57
|
+
setChosen(next);
|
|
58
|
+
try { window.localStorage.setItem(STORED_CHOICE, JSON.stringify(next)); } catch { /* A browser that keeps nothing shows the first lines again. */ }
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Drawn at the width it is given, so its type stays small and sharp on a phone and a desktop alike.
|
|
62
|
+
const box = useRef<HTMLDivElement>(null);
|
|
63
|
+
const [width, setWidth] = useState(600);
|
|
64
|
+
useLayoutEffect(() => {
|
|
65
|
+
const el = box.current;
|
|
66
|
+
if (el === null) return;
|
|
67
|
+
const measure = () => setWidth(Math.max(280, el.clientWidth));
|
|
68
|
+
measure();
|
|
69
|
+
const watch = new ResizeObserver(measure);
|
|
70
|
+
watch.observe(el);
|
|
71
|
+
return () => watch.disconnect();
|
|
72
|
+
}, []);
|
|
73
|
+
|
|
74
|
+
const H = 120;
|
|
75
|
+
const P = { t: 8, r: 200, b: 18, l: 40 };
|
|
76
|
+
const from = Math.min(view.at - 3600, ...shown.flatMap((s) => s.points.map((p) => p.at)));
|
|
77
|
+
const max = Math.max(1, ...shown.flatMap((s) => s.points.map((p) => p.tasks)));
|
|
78
|
+
const X = (t: number) => P.l + ((t - from) / Math.max(1, view.at - from)) * (width - P.l - P.r);
|
|
79
|
+
const Y = (v: number) => H - P.b - (v / max) * (H - P.t - P.b);
|
|
80
|
+
// Each line's name at its end, moved apart so no two overlap.
|
|
81
|
+
const ends = shown.map((s, i) => ({ s, i, y: Y(s.now) })).sort((a, b) => a.y - b.y);
|
|
82
|
+
for (let k = 1; k < ends.length; k += 1) ends[k]!.y = Math.max(ends[k]!.y, ends[k - 1]!.y + 11);
|
|
83
|
+
const recordedSince = view.queueHistory.length === 0 ? null : view.queueHistory[0]!.at;
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<section className="wp-chart" aria-label="Each queue over time">
|
|
87
|
+
<div className="wp-chart-head">
|
|
88
|
+
<span>Queues over time</span>
|
|
89
|
+
<span className="wp-mut">{recordedSince === null ? 'recorded from now on, each time the figures are built' : `recorded since ${localTime(recordedSince)}`}</span>
|
|
90
|
+
<details className="wp-chart-choose">
|
|
91
|
+
<summary>choose lines ({shown.length} of {all.length})</summary>
|
|
92
|
+
<div className="wp-chart-options">
|
|
93
|
+
{[...new Set(all.map((s) => s.workflow))].map((workflow) => (
|
|
94
|
+
<div key={workflow}>
|
|
95
|
+
<span>{workflow}</span>
|
|
96
|
+
{all.filter((s) => s.workflow === workflow).map((s) => (
|
|
97
|
+
<label key={s.key}>
|
|
98
|
+
<input type="checkbox" checked={shownKeys.includes(s.key)} onChange={(e) => choose(s.key, e.target.checked)} />
|
|
99
|
+
{s.bucket} {s.now.toLocaleString('en-US')}
|
|
100
|
+
</label>
|
|
101
|
+
))}
|
|
102
|
+
</div>
|
|
103
|
+
))}
|
|
104
|
+
</div>
|
|
105
|
+
</details>
|
|
106
|
+
</div>
|
|
107
|
+
<div ref={box} className="wp-chart-box">
|
|
108
|
+
{shown.length === 0 ? <p className="wp-line wp-mut">No line chosen: choose one above.</p> : (
|
|
109
|
+
<svg width={width} height={H} role="img" aria-label={`Queues over time: ${shown.map((s) => `${s.key} ${s.now}`).join(', ')}`}>
|
|
110
|
+
{[0, 0.5, 1].map((f) => (
|
|
111
|
+
<g key={f}>
|
|
112
|
+
<line x1={P.l} x2={width - P.r} y1={Y(max * f)} y2={Y(max * f)} stroke="var(--wp-line)" strokeWidth="1" />
|
|
113
|
+
<text x={P.l - 4} y={Y(max * f) + 3} textAnchor="end" fontSize="9" fill="var(--wp-dim)">{Math.round(max * f).toLocaleString('en-US')}</text>
|
|
114
|
+
</g>
|
|
115
|
+
))}
|
|
116
|
+
<text x={P.l} y={H - 4} fontSize="9" fill="var(--wp-dim)">{localTime(from)}</text>
|
|
117
|
+
<text x={width - P.r} y={H - 4} fontSize="9" fill="var(--wp-dim)" textAnchor="end">now</text>
|
|
118
|
+
{shown.map((s, i) => (
|
|
119
|
+
<path
|
|
120
|
+
key={s.key}
|
|
121
|
+
d={s.points.map((p, k) => (k === 0 ? `M${X(p.at)},${Y(p.tasks)}` : `H${X(p.at)}V${Y(p.tasks)}`)).join('')}
|
|
122
|
+
fill="none" stroke={SHADES[i % SHADES.length]} strokeWidth="1.25" strokeDasharray={DASHES[i % DASHES.length]}
|
|
123
|
+
/>
|
|
124
|
+
))}
|
|
125
|
+
{shown.map((s, i) => <circle key={`now:${s.key}`} cx={X(view.at)} cy={Y(s.now)} r="2.2" fill={SHADES[i % SHADES.length]} />)}
|
|
126
|
+
{ends.map(({ s, i, y }) => (
|
|
127
|
+
<g key={s.key}>
|
|
128
|
+
<line x1={width - P.r + 4} x2={width - P.r + 18} y1={y} y2={y} stroke={SHADES[i % SHADES.length]} strokeWidth="1.25" strokeDasharray={DASHES[i % DASHES.length]} />
|
|
129
|
+
<text x={width - P.r + 22} y={y + 3} fontSize="9.5" fill="var(--wp-fg)">{s.workflow} {s.bucket} {s.now.toLocaleString('en-US')}</text>
|
|
130
|
+
</g>
|
|
131
|
+
))}
|
|
132
|
+
</svg>
|
|
133
|
+
)}
|
|
134
|
+
</div>
|
|
135
|
+
</section>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ONE WORKFLOW'S DECLARED STEP GRAPH, DRAWN SMALL: its steps one under another in the order a run takes them, an agent step
|
|
5
|
+
* marked "agent" in words, and every edge an arrow with a number. The numbers' list below says each edge's condition and
|
|
6
|
+
* how many runs took it in the window. An edge to the next row goes straight down; any other runs in a lane of its own on
|
|
7
|
+
* the right, so it crosses no step.
|
|
8
|
+
*/
|
|
9
|
+
import { useLayoutEffect, useRef, useState } from 'react';
|
|
10
|
+
import { A_STEP_FAILS, RUN_ENDS, RUN_STARTS } from '../graph';
|
|
11
|
+
import type { WorkflowGraph } from '../views/step-graph';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Each step's row, in the order the workflow declares its steps, which is the order a run takes them: the run's start (and
|
|
15
|
+
* "a step fails", where an edge leaves it) first, then each step on a row of its own, then the run's end. Pure.
|
|
16
|
+
*/
|
|
17
|
+
export const stepRows = (graph: WorkflowGraph): ReadonlyArray<ReadonlyArray<string>> => [
|
|
18
|
+
[RUN_STARTS, ...(graph.edges.some((e) => e.from === A_STEP_FAILS) ? [A_STEP_FAILS] : [])],
|
|
19
|
+
...graph.steps.map((s) => [s.name]),
|
|
20
|
+
[RUN_ENDS],
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
export function StepGraph({ graph }: { readonly graph: WorkflowGraph }) {
|
|
24
|
+
const box = useRef<HTMLDivElement>(null);
|
|
25
|
+
const [paths, setPaths] = useState<ReadonlyArray<{ readonly key: string; readonly n: number; readonly d: string; readonly at: { readonly x: number; readonly y: number } }>>([]);
|
|
26
|
+
const [size, setSize] = useState({ w: 0, h: 0 });
|
|
27
|
+
const rows = stepRows(graph);
|
|
28
|
+
const agent = new Set(graph.steps.filter((s) => s.agent).map((s) => s.name));
|
|
29
|
+
useLayoutEffect(() => {
|
|
30
|
+
const el = box.current;
|
|
31
|
+
if (el === null) return;
|
|
32
|
+
const draw = () => {
|
|
33
|
+
const at = el.getBoundingClientRect();
|
|
34
|
+
const rect = (name: string) => el.querySelector(`[data-step="${CSS.escape(name)}"]`)?.getBoundingClientRect() ?? null;
|
|
35
|
+
const right = Math.max(0, ...[...el.querySelectorAll('[data-step]')].map((s) => s.getBoundingClientRect().right - at.left));
|
|
36
|
+
let lanes = 0;
|
|
37
|
+
setSize({ w: el.scrollWidth, h: el.scrollHeight });
|
|
38
|
+
setPaths(graph.edges.flatMap((e, i) => {
|
|
39
|
+
const a = rect(e.from), b = rect(e.to);
|
|
40
|
+
if (a === null || b === null) return [];
|
|
41
|
+
const ax = a.left + a.width / 2 - at.left, bx = b.left + b.width / 2 - at.left;
|
|
42
|
+
const nextRow = b.top >= a.bottom - 1 && b.top - a.bottom < 40;
|
|
43
|
+
if (nextRow) {
|
|
44
|
+
const y1 = a.bottom - at.top, y2 = b.top - at.top;
|
|
45
|
+
return [{ key: `${e.from}>${e.to}`, n: i + 1, d: `M${ax},${y1} C${ax},${(y1 + y2) / 2} ${bx},${(y1 + y2) / 2} ${bx},${y2 - 5}`, at: { x: (ax + bx) / 2, y: (y1 + y2) / 2 } }];
|
|
46
|
+
}
|
|
47
|
+
// Back up, along its own row, or past a row: in a lane on the right.
|
|
48
|
+
const lane = right + 12 + 10 * lanes++;
|
|
49
|
+
const ay = a.top + a.height / 2 - at.top, by = b.top + b.height / 2 - at.top;
|
|
50
|
+
return [{ key: `${e.from}>${e.to}`, n: i + 1, d: `M${a.right - at.left},${ay} H${lane} V${by} H${b.right - at.left + 5}`, at: { x: lane, y: (ay + by) / 2 } }];
|
|
51
|
+
}));
|
|
52
|
+
};
|
|
53
|
+
draw();
|
|
54
|
+
const watch = new ResizeObserver(draw);
|
|
55
|
+
watch.observe(el);
|
|
56
|
+
return () => watch.disconnect();
|
|
57
|
+
}, [graph]);
|
|
58
|
+
|
|
59
|
+
if (!graph.declared) return <p className="wp-line wp-bad">No step graph declared yet.</p>;
|
|
60
|
+
return (
|
|
61
|
+
<div className="wp-steps">
|
|
62
|
+
<div ref={box} className="wp-steps-box">
|
|
63
|
+
<svg className="wp-steps-edges" width={size.w} height={size.h} aria-hidden="true">
|
|
64
|
+
<defs>
|
|
65
|
+
<marker id="wp-step-arrow" viewBox="0 0 8 8" refX="7" refY="4" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
|
|
66
|
+
<path d="M0,0 L8,4 L0,8 z" fill="var(--wp-edge)" />
|
|
67
|
+
</marker>
|
|
68
|
+
</defs>
|
|
69
|
+
{paths.map((p) => <path key={p.key} d={p.d} fill="none" stroke="var(--wp-edge)" strokeWidth="1.1" markerEnd="url(#wp-step-arrow)" />)}
|
|
70
|
+
{paths.map((p) => (
|
|
71
|
+
<g key={`n:${p.key}`}>
|
|
72
|
+
<circle cx={p.at.x} cy={p.at.y} r="7" fill="var(--wp-bg)" stroke="var(--wp-edge)" strokeWidth="0.8" />
|
|
73
|
+
<text x={p.at.x} y={p.at.y + 3} fontSize="8.5" textAnchor="middle" fill="var(--wp-fg)">{p.n}</text>
|
|
74
|
+
</g>
|
|
75
|
+
))}
|
|
76
|
+
</svg>
|
|
77
|
+
{rows.map((row) => (
|
|
78
|
+
<div key={row.join('|')} className="wp-steps-row">
|
|
79
|
+
{row.map((name) => (
|
|
80
|
+
<span key={name} data-step={name} className={name === RUN_STARTS || name === RUN_ENDS || name === A_STEP_FAILS ? 'wp-step wp-step-end' : 'wp-step'}
|
|
81
|
+
title={graph.steps.find((s) => s.name === name)?.description ?? name}>
|
|
82
|
+
{name}{agent.has(name) ? <em> · agent</em> : null}
|
|
83
|
+
</span>
|
|
84
|
+
))}
|
|
85
|
+
</div>
|
|
86
|
+
))}
|
|
87
|
+
</div>
|
|
88
|
+
<ol className="wp-steps-when">
|
|
89
|
+
{graph.edges.map((e) => (
|
|
90
|
+
<li key={`${e.from}>${e.to}`}>{e.from} → {e.to}: {e.when}{e.runs === 0 ? '' : ` (${e.runs.toLocaleString('en-US')} run${e.runs === 1 ? '' : 's'})`}</li>
|
|
91
|
+
))}
|
|
92
|
+
</ol>
|
|
93
|
+
{graph.undeclared.length === 0 ? null : (
|
|
94
|
+
<p className="wp-line wp-bad">Taken but not declared: {graph.undeclared.map((e) => `${e.from} → ${e.to} (${e.runs})`).join('; ')}</p>
|
|
95
|
+
)}
|
|
96
|
+
</div>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* THE PROJECT'S WORKFLOWS AS A DAG, laid out by ELK's layered algorithm (`elkjs`, which the host page brings, as it brings
|
|
5
|
+
* React): each workflow one node, in layers in topological order of the forward edges, so every forward arrow points down;
|
|
6
|
+
* each stage's layers kept together (ELK's partitions, one per stage in the project's order) and shown as a band named in
|
|
7
|
+
* a gutter on the left that no edge enters. Edges are routed orthogonally around the nodes, each with its label beside its
|
|
8
|
+
* own line and its arrowhead at its target. A send-back, the only edge against the flow, is routed apart by ELK as a feedback
|
|
9
|
+
* edge, dashed and labelled "sends back". Workflows no work flows to or from are listed under their stages below the map.
|
|
10
|
+
*
|
|
11
|
+
* Chosen over dagre (`@dagrejs/dagre`): ELK routes edges around nodes and places labels beside their own edges, lays out
|
|
12
|
+
* partitions in order and routes feedback edges apart; dagre does none of those, and its edges and labels cross nodes.
|
|
13
|
+
*/
|
|
14
|
+
import ELK, { type ElkNode } from 'elkjs/lib/elk.bundled.js';
|
|
15
|
+
import { useEffect, useLayoutEffect, useRef, useState, type CSSProperties, type ReactNode } from 'react';
|
|
16
|
+
import type { WorkflowFlow } from '../views/workflow-flows';
|
|
17
|
+
import type { WorkflowView, WorkflowsView } from '../views/workflows-view';
|
|
18
|
+
|
|
19
|
+
/** What starts a workflow's runs, in words. */
|
|
20
|
+
const startsLine = (w: WorkflowView): string =>
|
|
21
|
+
w.startsOn.kind === 'queue' ? 'runs on its queue'
|
|
22
|
+
: w.startsOn.kind === 'inputs' ? `runs when its inputs change: ${w.startsOn.says}`
|
|
23
|
+
: 'runs on a timer or a request';
|
|
24
|
+
|
|
25
|
+
/** A queue's progress as a bar: done, running, queued and the rest, each segment lighter or darker than its neighbours. */
|
|
26
|
+
function QueueBar({ queue }: { readonly queue: NonNullable<WorkflowView['queue']> }) {
|
|
27
|
+
const rest = queue.waiting + queue.stuck + queue.held;
|
|
28
|
+
const total = Math.max(1, queue.done + queue.running + queue.queued + rest);
|
|
29
|
+
const segments = [
|
|
30
|
+
{ n: queue.done, cls: 'wp-seg-done' },
|
|
31
|
+
{ n: queue.running, cls: 'wp-seg-running' },
|
|
32
|
+
{ n: queue.queued, cls: 'wp-seg-queued' },
|
|
33
|
+
{ n: rest, cls: 'wp-seg-rest' },
|
|
34
|
+
];
|
|
35
|
+
const words = [
|
|
36
|
+
`${queue.queued.toLocaleString('en-US')} queued`,
|
|
37
|
+
`${queue.running.toLocaleString('en-US')} running`,
|
|
38
|
+
`${queue.done.toLocaleString('en-US')} done`,
|
|
39
|
+
...(['waiting', 'stuck', 'held'] as const).filter((b) => queue[b] > 0).map((b) => `${queue[b].toLocaleString('en-US')} ${b}`),
|
|
40
|
+
];
|
|
41
|
+
return (
|
|
42
|
+
<div className="wp-queue">
|
|
43
|
+
<div className="wp-bar" role="img" aria-label={words.join(', ')}>
|
|
44
|
+
{segments.map((s) => (s.n === 0 ? null : <span key={s.cls} className={s.cls} style={{ width: `${(100 * s.n) / total}%` }} />))}
|
|
45
|
+
</div>
|
|
46
|
+
<div className="wp-queue-words">{words.join(' · ')}</div>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** An edge's label: what it is, and along a wait how many wait. */
|
|
52
|
+
const flowLabel = (f: WorkflowFlow): string => (f.kind === 'waits on' ? `${f.waiting.toLocaleString('en-US')} wait on it` : f.kind);
|
|
53
|
+
/** An edge's line: solid feeds, dashed sends back, dotted gives input, dash-dot waits on. */
|
|
54
|
+
const DASH: Readonly<Record<WorkflowFlow['kind'], string | undefined>> = { feeds: undefined, 'sends back': '7 4', 'gives input': '1.5 3.5', 'waits on': '7 3 1.5 3' };
|
|
55
|
+
|
|
56
|
+
/** The node's width; its height is measured from what it shows. */
|
|
57
|
+
const NODE_WIDTH = 250;
|
|
58
|
+
/** The left gutter the stages are named in. */
|
|
59
|
+
const GUTTER = 150;
|
|
60
|
+
const LABEL_HEIGHT = 15;
|
|
61
|
+
const labelWidth = (text: string) => Math.ceil(text.length * 6.3) + 10;
|
|
62
|
+
|
|
63
|
+
interface Laid {
|
|
64
|
+
readonly nodes: ReadonlyMap<string, { readonly x: number; readonly y: number; readonly h: number }>;
|
|
65
|
+
readonly edges: ReadonlyArray<{ readonly flow: WorkflowFlow; readonly points: ReadonlyArray<{ readonly x: number; readonly y: number }>; readonly label: { readonly x: number; readonly y: number; readonly w: number } | null }>;
|
|
66
|
+
readonly width: number;
|
|
67
|
+
readonly height: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const elk = new ELK();
|
|
71
|
+
|
|
72
|
+
/** The layout of the linked workflows, given each node's height. */
|
|
73
|
+
const layOut = async (input: {
|
|
74
|
+
/** In pipeline order: by stage, then by depth through forward edges within it. */
|
|
75
|
+
readonly workflows: ReadonlyArray<{ readonly name: string; readonly stage: number; readonly height: number }>;
|
|
76
|
+
readonly flows: ReadonlyArray<WorkflowFlow>;
|
|
77
|
+
}): Promise<Laid> => {
|
|
78
|
+
const asked: ElkNode = {
|
|
79
|
+
id: 'workflows',
|
|
80
|
+
layoutOptions: {
|
|
81
|
+
'elk.algorithm': 'layered',
|
|
82
|
+
'elk.direction': 'DOWN',
|
|
83
|
+
'elk.edgeRouting': 'ORTHOGONAL',
|
|
84
|
+
'elk.partitioning.activate': 'true',
|
|
85
|
+
// One layering for all, so every stage's layers come before the next stage's; laid out apart, unconnected groups sat side by side.
|
|
86
|
+
'elk.separateConnectedComponents': 'false',
|
|
87
|
+
'elk.layered.feedbackEdges': 'true',
|
|
88
|
+
// The platform's pipeline order decides which edges go against the flow: only one to a workflow earlier in it, a
|
|
89
|
+
// send-back. Left to itself, ELK kept Retry near-misses' send-back as forward and drew Decompile's feed going up.
|
|
90
|
+
'elk.layered.cycleBreaking.strategy': 'MODEL_ORDER',
|
|
91
|
+
'elk.layered.considerModelOrder.strategy': 'NODES_AND_EDGES',
|
|
92
|
+
'elk.layered.spacing.nodeNodeBetweenLayers': '64',
|
|
93
|
+
'elk.spacing.nodeNode': '36',
|
|
94
|
+
'elk.spacing.edgeNode': '18',
|
|
95
|
+
'elk.spacing.edgeEdge': '12',
|
|
96
|
+
'elk.spacing.edgeLabel': '4',
|
|
97
|
+
'elk.layered.edgeLabels.sideSelection': 'SMART_DOWN',
|
|
98
|
+
'elk.padding': `[top=34,left=${GUTTER},bottom=16,right=16]`,
|
|
99
|
+
},
|
|
100
|
+
children: input.workflows.map((w) => ({ id: w.name, width: NODE_WIDTH, height: w.height, layoutOptions: { 'elk.partitioning.partition': String(w.stage) } })),
|
|
101
|
+
edges: input.flows.map((f, i) => ({ id: `edge-${i}`, sources: [f.from], targets: [f.to], labels: [{ id: `label-${i}`, text: flowLabel(f), width: labelWidth(flowLabel(f)), height: LABEL_HEIGHT }] })),
|
|
102
|
+
};
|
|
103
|
+
const graph = await elk.layout(asked);
|
|
104
|
+
const nodes = new Map((graph.children ?? []).map((c) => [c.id, { x: c.x ?? 0, y: c.y ?? 0, h: c.height ?? 0 }]));
|
|
105
|
+
const edges = (graph.edges ?? []).map((e, i) => {
|
|
106
|
+
const s = e.sections?.[0];
|
|
107
|
+
const label = e.labels?.[0];
|
|
108
|
+
return {
|
|
109
|
+
flow: input.flows[i]!,
|
|
110
|
+
points: s === undefined ? [] : [s.startPoint, ...(s.bendPoints ?? []), s.endPoint],
|
|
111
|
+
label: label === undefined || label.x === undefined || label.y === undefined ? null : { x: label.x, y: label.y, w: label.width ?? 0 },
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
return { nodes, edges, width: graph.width ?? 0, height: graph.height ?? 0 };
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export interface WorkflowDagProps {
|
|
118
|
+
readonly view: WorkflowsView;
|
|
119
|
+
readonly phaseLabel: (phase: string) => string;
|
|
120
|
+
/** A stage's figures, beside its name. */
|
|
121
|
+
readonly phaseLine: (phase: string) => ReactNode;
|
|
122
|
+
/** A node's runs and agents now, with its limit controls. */
|
|
123
|
+
readonly nodeStatus: (w: WorkflowView) => ReactNode;
|
|
124
|
+
/** A node's link to its definition. */
|
|
125
|
+
readonly nodeHref: (w: WorkflowView) => string;
|
|
126
|
+
/** The open node's steps, graph and costs. */
|
|
127
|
+
readonly detail: (w: WorkflowView) => ReactNode;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function Node({ w, flows, open, setOpen, nodeStatus, nodeHref, style }: { readonly w: WorkflowView; readonly flows: ReadonlyArray<WorkflowFlow>; readonly open: boolean; readonly setOpen: (open: boolean) => void; readonly nodeStatus: (w: WorkflowView) => ReactNode; readonly nodeHref: (w: WorkflowView) => string; readonly style?: CSSProperties }) {
|
|
131
|
+
const into = flows.filter((f) => f.to === w.name);
|
|
132
|
+
const out = flows.filter((f) => f.from === w.name);
|
|
133
|
+
const word = (f: WorkflowFlow) => (f.kind === 'feeds' ? '' : f.kind === 'waits on' ? ` (${f.waiting.toLocaleString('en-US')} waiting)` : ` (${f.kind})`);
|
|
134
|
+
return (
|
|
135
|
+
<div data-workflow={w.name} className={open ? 'wp-node wp-node-open' : 'wp-node'} style={style}>
|
|
136
|
+
<div className="wp-node-head">
|
|
137
|
+
<button type="button" className="wp-node-name" aria-expanded={open} onClick={() => setOpen(!open)} title={w.description}>{open ? '▾' : '▸'} {w.name}</button>
|
|
138
|
+
<a className="wp-mut" href={nodeHref(w)} title="Its definition">source</a>
|
|
139
|
+
</div>
|
|
140
|
+
<div className="wp-node-what" title={w.description}>{w.description}</div>
|
|
141
|
+
<div className="wp-node-starts wp-mut" title={startsLine(w)}>{startsLine(w)}</div>
|
|
142
|
+
{into.length === 0 && out.length === 0 ? null : (
|
|
143
|
+
<div className="wp-node-flows wp-mut">
|
|
144
|
+
{into.length === 0 ? null : <span>from {into.map((f) => `${f.from}${word(f)}`).join(', ')}</span>}
|
|
145
|
+
{out.length === 0 ? null : <span>to {out.map((f) => `${f.to}${word(f)}`).join(', ')}</span>}
|
|
146
|
+
</div>
|
|
147
|
+
)}
|
|
148
|
+
{w.queue === null ? null : <QueueBar queue={w.queue} />}
|
|
149
|
+
<div className="wp-node-status">{nodeStatus(w)}</div>
|
|
150
|
+
</div>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function WorkflowDag({ view, phaseLabel, phaseLine, nodeStatus, nodeHref, detail }: WorkflowDagProps) {
|
|
155
|
+
const [open, setOpenName] = useState<string | null>(null);
|
|
156
|
+
const all = view.groups.flatMap((g) => g.workflows);
|
|
157
|
+
const linkedNames = new Set(view.flows.flatMap((f) => [f.from, f.to]));
|
|
158
|
+
const linked = all.filter((w) => linkedNames.has(w.name));
|
|
159
|
+
const stageOf = new Map(view.groups.map((g, i) => [g.phase, i]));
|
|
160
|
+
const openWorkflow = all.find((w) => w.name === open) ?? null;
|
|
161
|
+
const panel = useRef<HTMLDivElement>(null);
|
|
162
|
+
const setOpen = (name: string) => (on: boolean) => setOpenName(on ? name : null);
|
|
163
|
+
useEffect(() => { if (open !== null) panel.current?.scrollIntoView({ block: 'nearest', behavior: 'smooth' }); }, [open]);
|
|
164
|
+
|
|
165
|
+
// Each node's height, measured as drawn, then the layout from them; laid out again whenever a height or the flows change.
|
|
166
|
+
const map = useRef<HTMLDivElement>(null);
|
|
167
|
+
const [heights, setHeights] = useState<string>('');
|
|
168
|
+
const [laid, setLaid] = useState<Laid | null>(null);
|
|
169
|
+
const [failed, setFailed] = useState<string | null>(null);
|
|
170
|
+
useLayoutEffect(() => {
|
|
171
|
+
const el = map.current;
|
|
172
|
+
if (el === null) return;
|
|
173
|
+
const measure = () => setHeights(JSON.stringify([...el.querySelectorAll('[data-workflow]')].map((n) => [n.getAttribute('data-workflow'), Math.ceil(n.getBoundingClientRect().height)])));
|
|
174
|
+
measure();
|
|
175
|
+
const watch = new ResizeObserver(measure);
|
|
176
|
+
for (const n of el.querySelectorAll('[data-workflow]')) watch.observe(n);
|
|
177
|
+
return () => watch.disconnect();
|
|
178
|
+
}, [view.at, linked.length]);
|
|
179
|
+
const flowsKey = JSON.stringify(view.flows.map((f) => [f.from, f.to, f.kind, flowLabel(f)]));
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
if (heights === '') return;
|
|
182
|
+
const h = new Map(JSON.parse(heights) as Array<[string, number]>);
|
|
183
|
+
let current = true;
|
|
184
|
+
const inOrder = [...linked].sort((a, b) => a.place.stage - b.place.stage || a.place.depth - b.place.depth || a.name.localeCompare(b.name));
|
|
185
|
+
layOut({ workflows: inOrder.map((w) => ({ name: w.name, stage: stageOf.get(w.phase) ?? 0, height: h.get(w.name) ?? 120 })), flows: view.flows })
|
|
186
|
+
.then((l) => { if (current) { setLaid(l); setFailed(null); } })
|
|
187
|
+
.catch((e: unknown) => { if (current) setFailed(e instanceof Error ? e.message : String(e)); });
|
|
188
|
+
return () => { current = false; };
|
|
189
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `linked` and `stageOf` follow `heights` and `flowsKey`.
|
|
190
|
+
}, [heights, flowsKey]);
|
|
191
|
+
|
|
192
|
+
// Each stage's band: from the top of its first node to the bottom of its last.
|
|
193
|
+
const bands = laid === null ? [] : view.groups.flatMap((g) => {
|
|
194
|
+
const ys = linked.filter((w) => w.phase === g.phase).flatMap((w) => { const n = laid.nodes.get(w.name); return n === undefined ? [] : [{ top: n.y, bottom: n.y + n.h }]; });
|
|
195
|
+
return ys.length === 0 ? [] : [{ phase: g.phase, top: Math.min(...ys.map((y) => y.top)) - 24, bottom: Math.max(...ys.map((y) => y.bottom)) + 14 }];
|
|
196
|
+
});
|
|
197
|
+
const unlinked = view.groups.map((g) => ({ phase: g.phase, workflows: g.workflows.filter((w) => !linkedNames.has(w.name)) })).filter((g) => g.workflows.length > 0);
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<div className="wp-dag-wrap">
|
|
201
|
+
<p className="wp-line wp-mut">
|
|
202
|
+
Each box is one workflow; open one for its steps. Arrows point the way work goes, down the page: a solid arrow feeds the next
|
|
203
|
+
workflow's tasks; dashed, “sends back”, returns work to an earlier one. Each stage is a band, named on the left.
|
|
204
|
+
</p>
|
|
205
|
+
{failed === null ? null : <p className="wp-line wp-bad">The map could not be laid out: {failed}</p>}
|
|
206
|
+
<div className="wp-dag-scroll">
|
|
207
|
+
<div ref={map} className="wp-dag" style={{ width: laid === null ? undefined : laid.width, height: laid === null ? undefined : laid.height }}>
|
|
208
|
+
{laid === null ? null : (
|
|
209
|
+
<svg className="wp-dag-edges" width={laid.width} height={laid.height} aria-hidden="true">
|
|
210
|
+
<defs>
|
|
211
|
+
<marker id="wp-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="11" markerHeight="11" markerUnits="userSpaceOnUse" orient="auto">
|
|
212
|
+
<path d="M0,0 L10,5 L0,10 z" fill="var(--wp-edge)" />
|
|
213
|
+
</marker>
|
|
214
|
+
</defs>
|
|
215
|
+
{bands.map((b, i) => (
|
|
216
|
+
<g key={b.phase}>
|
|
217
|
+
<rect x={0} y={b.top} width={laid.width} height={b.bottom - b.top} fill={i % 2 === 0 ? 'var(--wp-band)' : 'transparent'} />
|
|
218
|
+
<foreignObject x={6} y={b.top + 6} width={GUTTER - 16} height={Math.max(40, b.bottom - b.top - 12)}>
|
|
219
|
+
<div className="wp-band-name">{phaseLabel(b.phase)}<span className="wp-mut"> · {phaseLine(b.phase)}</span></div>
|
|
220
|
+
</foreignObject>
|
|
221
|
+
</g>
|
|
222
|
+
))}
|
|
223
|
+
{laid.edges.map(({ flow, points }) => (
|
|
224
|
+
<polyline key={`${flow.from}>${flow.to}`} points={points.map((p) => `${p.x},${p.y}`).join(' ')} fill="none" stroke="var(--wp-edge)"
|
|
225
|
+
strokeWidth={flow.kind === 'feeds' ? 1.8 : 1.5} strokeDasharray={DASH[flow.kind]} markerEnd="url(#wp-arrow)" />
|
|
226
|
+
))}
|
|
227
|
+
{laid.edges.map(({ flow, label }) => (label === null ? null : (
|
|
228
|
+
<g key={`label:${flow.from}>${flow.to}`}>
|
|
229
|
+
<rect x={label.x} y={label.y} width={label.w} height={LABEL_HEIGHT} rx={3} fill="var(--wp-bg)" stroke="var(--wp-edge)" strokeWidth="0.7" />
|
|
230
|
+
<text x={label.x + 5} y={label.y + 11} fontSize="10.5" fill="var(--wp-fg)">{flowLabel(flow)}{flow.why === null ? null : <title>{flow.why}</title>}</text>
|
|
231
|
+
</g>
|
|
232
|
+
)))}
|
|
233
|
+
</svg>
|
|
234
|
+
)}
|
|
235
|
+
{linked.map((w) => {
|
|
236
|
+
const at = laid?.nodes.get(w.name);
|
|
237
|
+
return (
|
|
238
|
+
<Node key={w.name} w={w} flows={view.flows} open={open === w.name} setOpen={setOpen(w.name)} nodeStatus={nodeStatus} nodeHref={nodeHref}
|
|
239
|
+
style={at === undefined ? { position: 'absolute', left: 0, top: 0, visibility: 'hidden' } : { position: 'absolute', left: at.x, top: at.y }} />
|
|
240
|
+
);
|
|
241
|
+
})}
|
|
242
|
+
</div>
|
|
243
|
+
</div>
|
|
244
|
+
{openWorkflow === null || !linkedNames.has(openWorkflow.name) ? null : <div ref={panel} className="wp-node-detail">{detail(openWorkflow)}</div>}
|
|
245
|
+
{unlinked.map((g) => (
|
|
246
|
+
<section key={g.phase} className="wp-stage">
|
|
247
|
+
<h2 className="wp-h2">{phaseLabel(g.phase)}: workflows no work flows to or from<span className="wp-mut"> · {phaseLine(g.phase)}</span></h2>
|
|
248
|
+
<div className="wp-nodes">
|
|
249
|
+
{g.workflows.map((w) => <Node key={w.name} w={w} flows={view.flows} open={open === w.name} setOpen={setOpen(w.name)} nodeStatus={nodeStatus} nodeHref={nodeHref} />)}
|
|
250
|
+
</div>
|
|
251
|
+
{openWorkflow !== null && openWorkflow.phase === g.phase && !linkedNames.has(openWorkflow.name) ? <div ref={panel} className="wp-node-detail">{detail(openWorkflow)}</div> : null}
|
|
252
|
+
</section>
|
|
253
|
+
))}
|
|
254
|
+
</div>
|
|
255
|
+
);
|
|
256
|
+
}
|