Pull pipeline/ from boilerplate a80ad8a: cadence picker, queue badge, label refresh

The schedule cadence is a picker, the add row is behind a button and off
here, a running-jobs chip with Kill and Stop run sits in every header, and
a declared schedule's label is refreshed at startup.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-09-28 16:22:46 +02:00
1 parent cebe62ddc9
commit 3a240e51df
16 files changed
+828 -167

No files matched your search

+221 -72
View File
@@ -4,9 +4,16 @@
* <desk-schedules></desk-schedules>
*
* A row per schedule — name, kind, cadence, on or off, next run, last run and
* how that job ended — plus Run now, Delete, and a row at the bottom for
* adding one. The kinds come from `/api/jobs/kinds`, so a new `@job` is a new
* option here without this file being touched.
* how that job ended — plus Run now, Delete, and an "Add a schedule" button
* that opens a row for adding one. The kinds come from `/api/jobs/kinds`, so a
* new `@job` is a new option here without this file being touched.
*
* The cadence is picked, not typed: Every / Daily at / Weekly on, then an
* interval, a time or a day and a time. The picker spells the same canonical
* string the server parses (`every 6h`, `daily at 06:00`, `weekly mon 07:00`),
* so the grammar and the API are unchanged and the server's 422 is still the
* backstop. A stored cadence that is not a preset, such as `every 50m`, is
* added to the options rather than lost.
*
* It reads `/ws/jobs` for `schedules` messages, so a row edited in another tab
* redraws in this one, and a firing moves the times without anybody polling.
@@ -30,6 +37,8 @@
* Attributes, all optional:
* base="/api" prefix for the HTTP calls, if the router is mounted deeper
* socket="..." the socket's URL, if it is not `/ws/jobs` on this origin
* add="off" no way to add a schedule, for an app whose schedules are
* all declared in code. Editing and deleting stay.
*/
const CSS = `
@@ -72,10 +81,7 @@ td.actions { text-align: right; white-space: nowrap; }
.off { opacity: .55; }
.bad { color: var(--desk-jobs-error); }
/* The cadence is edited in place: a box that looks like text until you are in
* it, because a table of eight input fields reads as a form to fill in rather
* than as a list of what is running. */
input.cadence, input.text {
input.text {
font: inherit;
color: inherit;
background: none;
@@ -84,11 +90,10 @@ input.cadence, input.text {
padding: 1px 4px;
min-width: 9em;
}
input.cadence:hover { border-color: color-mix(in srgb, var(--desk-jobs-fg) 20%, transparent); }
input:focus { outline: none; border-color: var(--desk-jobs-accent); }
input:focus, select:focus { outline: none; border-color: var(--desk-jobs-accent); }
input::placeholder { color: var(--desk-jobs-dim); }
select {
select, input.time {
font: inherit;
color: inherit;
background: var(--desk-jobs-bg);
@@ -97,6 +102,15 @@ select {
padding: 1px 4px;
}
/* The cadence is edited in place: in a row its controls look like text until
* hovered, because a table of twenty dropdowns reads as a form to fill in
* rather than as a list of what is running. The add row keeps its borders. */
.picker { display: inline-flex; flex-wrap: wrap; gap: 4px; align-items: baseline; }
tr:not(.adder) .picker > * { border-color: transparent; }
tr:not(.adder) .picker > *:hover { border-color: color-mix(in srgb, var(--desk-jobs-fg) 20%, transparent); }
tr:not(.adder) .picker > *:focus { border-color: var(--desk-jobs-accent); }
button.open { margin: 8px 0 0; }
button {
font: inherit;
color: var(--desk-jobs-dim);
@@ -166,12 +180,12 @@ button[disabled] { opacity: .4; cursor: default; }
display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px;
}
td.actions button { margin: 0; }
/* A box that looks like text until hovered has nothing to hover with.
* box-sizing: border-box, because the base rule's padding and border are
* content-box -- width: 100% of a border-box cell plus that padding and
* border is wider than the cell, which is exactly the sliver this media
* query exists to not reintroduce. */
input.cadence { border-color: color-mix(in srgb, var(--desk-jobs-fg) 20%, transparent); min-width: 0; width: 100%; box-sizing: border-box; }
/* A control that looks like text until hovered has nothing to hover with.
* The picker's controls share the cell and wrap rather than overflow it:
* min-width: 0, because a select's natural width is its longest option. */
tr:not(.adder) .picker > * { border-color: color-mix(in srgb, var(--desk-jobs-fg) 20%, transparent); }
.picker { display: flex; }
.picker > * { flex: 1 1 auto; min-width: 0; }
/* The add row: label over field, each field the width of the card. */
tr.adder { grid-template-columns: 1fr; }
tr.adder td { grid-column: 1; }
@@ -182,7 +196,10 @@ button[disabled] { opacity: .4; cursor: default; }
color: var(--desk-jobs-dim);
font-weight: 600;
}
tr.adder input, tr.adder select {
/* box-sizing: border-box, because the base rule's padding and border are
* content-box -- width: 100% of a border-box cell plus that padding and
* border is wider than the cell. */
tr.adder td > input, tr.adder td > select {
width: 100%; min-width: 0; box-sizing: border-box;
border-color: color-mix(in srgb, var(--desk-jobs-fg) 20%, transparent);
}
@@ -194,12 +211,125 @@ button[disabled] { opacity: .4; cursor: default; }
const FIRST_WAIT = 500;
const LONGEST_WAIT = 30000;
/** The grammar, in the one place a person is asked to use it. Two of the five
* as the placeholder, because a box wide enough for all five is a box wide
* enough to unbalance the row it is in — and all five on hover, which is also
* what the server's refusal says if they get it wrong anyway. */
const FORMS = "every 15m · every 6h · every 2d · daily at 06:00 · weekly mon 07:00";
const HINT = "every 15m · daily at 06:00";
/** The intervals offered under Every, shortest first. `every 15m` is the
* shortest worth offering: the queue looks for due schedules every thirty
* seconds, but nothing a small app schedules wants to run more often. */
const INTERVALS = ["15m", "30m", "1h", "2h", "3h", "6h", "12h", "1d", "2d", "7d"];
const UNITS = { m: ["minute", 60], h: ["hour", 3600], d: ["day", 86400] };
/** Short names as `pipeline/schedule.py` stores them, long ones as shown. */
const DAYS = [["mon", "Monday"], ["tue", "Tuesday"], ["wed", "Wednesday"],
["thu", "Thursday"], ["fri", "Friday"], ["sat", "Saturday"],
["sun", "Sunday"]];
/** What a new schedule starts as, and what a form switched to starts with. */
const START = { form: "every", every: "1h", day: "mon", time: "06:00" };
/** A stored cadence, taken apart for the picker. The server stores only the
* canonical spelling, so this reads that and nothing looser; anything else
* (a row edited by hand) comes back `null` and is shown as it is. */
function parse(cadence) {
const said = String(cadence || "").trim().toLowerCase();
let m = said.match(/^every (\d+)([mhd])$/);
if (m) return { ...START, form: "every", every: `${Number(m[1])}${m[2]}` };
m = said.match(/^daily at (\d{2}:\d{2})$/);
if (m) return { ...START, form: "daily", time: m[1] };
m = said.match(/^weekly (mon|tue|wed|thu|fri|sat|sun) (\d{2}:\d{2})$/);
if (m) return { ...START, form: "weekly", day: m[1], time: m[2] };
return null;
}
/** The picker's state as the canonical string the server accepts. */
function spell(p) {
if (p.form === "daily") return `daily at ${p.time}`;
if (p.form === "weekly") return `weekly ${p.day} ${p.time}`;
return `every ${p.every}`;
}
/** `6h` as "6 hours", and its length, for ordering an extra option. */
function interval(text) {
const [, n, unit] = text.match(/^(\d+)([mhd])$/);
const [word, seconds] = UNITS[unit];
return { label: `${n} ${word}${n === "1" ? "" : "s"}`, seconds: Number(n) * seconds };
}
function choice(options, selected, label) {
const select = document.createElement("select");
select.setAttribute("aria-label", label);
for (const [value, text] of options) {
const option = document.createElement("option");
option.value = value;
option.textContent = text;
option.selected = value === selected;
select.append(option);
}
return select;
}
/** The cadence picker: a form, then what that form needs. `pick` is handed
* the canonical string each time a complete one is chosen.
*
* `lazy` is for a stored row, where each pick is a PATCH: the time is then
* taken on blur or Enter rather than on `change`, which a time input raises
* per segment typed — a save and a redraw between the hour and the minute. */
function picker(cadence, { disabled = false, lazy = false, pick }) {
const known = parse(cadence);
const p = known || { ...START };
const wrap = document.createElement("span");
wrap.className = "picker";
const form = choice([["every", "Every"], ["daily", "Daily at"], ["weekly", "Weekly on"]],
known ? p.form : "", "Cadence");
if (!known && cadence) {
// Not a spelling the picker makes: shown as it is, and replaced only when
// somebody picks something else.
const stored = document.createElement("option");
stored.value = "";
stored.textContent = cadence;
stored.selected = true;
form.prepend(stored);
}
const offered = INTERVALS.includes(p.every) ? INTERVALS
: [...INTERVALS, p.every].sort((a, b) => interval(a).seconds - interval(b).seconds);
const every = choice(offered.map((v) => [v, interval(v).label]), p.every, "Interval");
const day = choice(DAYS, p.day, "Day");
const time = document.createElement("input");
time.type = "time";
time.step = 900;
time.className = "time";
time.value = p.time;
time.setAttribute("aria-label", "Time");
const emit = () => pick(spell(p));
const draw = () => {
const parts = { every: [every], daily: [time], weekly: [day, time] }[form.value] || [];
wrap.replaceChildren(form, ...parts);
};
for (const control of [form, every, day, time]) control.disabled = disabled;
form.addEventListener("change", () => {
if (!form.value) return;
p.form = form.value;
draw();
emit();
});
every.addEventListener("change", () => { p.every = every.value; emit(); });
day.addEventListener("change", () => { p.day = day.value; emit(); });
const takeTime = () => {
if (!time.value || time.value === p.time) return;
p.time = time.value;
emit();
};
if (lazy) {
time.addEventListener("blur", takeTime);
time.addEventListener("keydown", (e) => { if (e.key === "Enter") time.blur(); });
} else {
time.addEventListener("change", takeTime);
}
draw();
return wrap;
}
/** A stamp as a person reads it. Today is a time, anything else is a date and
* a time, and "never" is said rather than left blank — an empty cell reads as
@@ -223,6 +353,13 @@ class DeskSchedules extends HTMLElement {
#offline = false;
#busy = new Set();
#loaded = false;
/* The add row: whether it is open, and what is in it. Kept here rather than
* in its inputs because every render rebuilds them, and a push arriving
* mid-typing would otherwise clear what somebody was writing. */
#adding = false;
#draft = { name: "", kind: "", cadence: spell(START) };
static get observedAttributes() { return ["add"]; }
constructor() {
super();
@@ -238,6 +375,10 @@ class DeskSchedules extends HTMLElement {
this.#connect();
}
attributeChangedCallback() {
if (this.isConnected) this.#render();
}
disconnectedCallback() {
clearTimeout(this.#timer);
this.#socket?.close();
@@ -246,6 +387,8 @@ class DeskSchedules extends HTMLElement {
get #base() { return this.getAttribute("base") ?? ""; }
get #canAdd() { return this.getAttribute("add") !== "off"; }
get #socketUrl() {
const given = this.getAttribute("socket");
if (given) return given;
@@ -419,14 +562,26 @@ class DeskSchedules extends HTMLElement {
table.append(this.#head());
const body = document.createElement("tbody");
for (const row of this.#rows) body.append(this.#row(row));
body.append(this.#adder());
if (this.#canAdd && this.#adding) body.append(this.#adder());
table.append(body);
panel.append(table);
if (this.#canAdd && !this.#adding) {
// Closed until asked for. An add row always drawn, with a kind already
// chosen and a cadence filled in, reads as a schedule that exists.
const open = document.createElement("button");
open.className = "open";
open.textContent = "Add a schedule";
open.disabled = this.#loaded && !this.#kinds.length;
if (open.disabled) open.title = "No job is registered to schedule.";
open.addEventListener("click", () => { this.#adding = true; this.#render(); });
panel.append(open);
}
if (this.#loaded && !this.#rows.length) {
const empty = document.createElement("div");
empty.className = "empty";
empty.textContent = "Nothing is scheduled. Add one below.";
empty.textContent = "Nothing is scheduled.";
panel.insertBefore(empty, table);
}
}
@@ -464,23 +619,17 @@ class DeskSchedules extends HTMLElement {
const cadence = document.createElement("td");
cadence.className = "cadence";
cadence.dataset.label = "Cadence";
const box = document.createElement("input");
box.className = "cadence";
box.value = schedule.cadence;
box.title = FORMS;
box.disabled = busy;
const save = () => {
const wanted = box.value.trim();
if (!wanted || wanted === schedule.cadence) {
box.value = schedule.cadence;
return;
}
this.#change(schedule.id, () => this.#ask(`/api/schedules/${schedule.id}`,
{ method: "PATCH", body: { cadence: wanted } }));
};
box.addEventListener("change", save);
box.addEventListener("keydown", (e) => { if (e.key === "Enter") box.blur(); });
cadence.append(box);
cadence.append(picker(schedule.cadence, {
disabled: busy,
lazy: true,
// A refusal redraws the row from what the server holds, so the picker
// goes back to the stored cadence and the 422 goes out as before.
pick: (wanted) => {
if (wanted === schedule.cadence) return;
this.#change(schedule.id, () => this.#ask(`/api/schedules/${schedule.id}`,
{ method: "PATCH", body: { cadence: wanted } }));
},
}));
row.append(cadence);
const onOff = document.createElement("td");
@@ -545,34 +694,29 @@ class DeskSchedules extends HTMLElement {
const row = document.createElement("tr");
row.className = "adder";
const busy = this.#busy.has(0);
const draft = this.#draft;
const name = document.createElement("input");
name.className = "text";
name.placeholder = "optional";
name.value = draft.name;
name.disabled = busy;
name.addEventListener("input", () => { draft.name = name.value; });
row.append(this.#wrap(name, "Name"));
const kind = document.createElement("select");
// Starts on nothing, so that what is added is a choice rather than
// whichever kind sorts first.
const kind = choice([["", this.#kinds.length ? "Choose a job…"
: this.#loaded ? "nothing registered" : "loading…"],
...this.#kinds.map((k) => [k.kind, k.label || k.kind])],
draft.kind, "Kind");
kind.disabled = busy || !this.#kinds.length;
for (const k of this.#kinds) {
const option = document.createElement("option");
option.value = k.kind;
option.textContent = k.label || k.kind;
kind.append(option);
}
if (!this.#kinds.length) {
const none = document.createElement("option");
none.textContent = this.#loaded ? "nothing registered" : "loading…";
kind.append(none);
}
row.append(this.#wrap(kind, "Kind"));
const cadence = document.createElement("input");
cadence.className = "text";
cadence.placeholder = HINT;
cadence.title = FORMS;
cadence.disabled = busy;
row.append(this.#wrap(cadence, "Cadence"));
row.append(this.#wrap(picker(draft.cadence, {
disabled: busy,
pick: (wanted) => { draft.cadence = wanted; },
}), "Cadence"));
row.append(document.createElement("td"), document.createElement("td"),
document.createElement("td"));
@@ -581,26 +725,31 @@ class DeskSchedules extends HTMLElement {
actions.className = "actions";
const add = document.createElement("button");
add.textContent = "Add";
add.disabled = busy;
add.disabled = busy || !draft.kind;
kind.addEventListener("change", () => {
draft.kind = kind.value;
add.disabled = busy || !draft.kind;
});
const submit = () => {
if (!kind.value) return;
if (!draft.kind) return;
this.#change(0, async () => {
await this.#ask("/api/schedules", {
method: "POST",
body: { name: name.value.trim(), kind: kind.value,
cadence: cadence.value.trim() },
body: { name: draft.name.trim(), kind: draft.kind, cadence: draft.cadence },
});
// Cleared only on success, so a refused cadence is still there to be
// corrected rather than retyped from memory.
name.value = "";
cadence.value = "";
// Closed and cleared only on success, so a refusal leaves what was
// chosen there to be corrected rather than chosen again.
this.#adding = false;
this.#draft = { name: "", kind: "", cadence: spell(START) };
});
};
add.addEventListener("click", submit);
for (const box of [name, cadence]) {
box.addEventListener("keydown", (e) => { if (e.key === "Enter") submit(); });
}
actions.append(add);
name.addEventListener("keydown", (e) => { if (e.key === "Enter") submit(); });
const cancel = document.createElement("button");
cancel.textContent = "Cancel";
cancel.disabled = busy;
cancel.addEventListener("click", () => { this.#adding = false; this.#render(); });
actions.append(add, cancel);
row.append(actions);
return row;
}