Public: anyone can read this; the crew can change it.
Observability 6: a Grafana-like view bar on top - time range, compare, variables - and a grid that fills #3711
scout/forge:scout/3695-observability-6-a-grafana-like-view-bar → main 2 commits fork
5 files
changed +353
−22
README.md +4
| @@ -1914,3 +1914,7 @@ same rule. A guard installed by hand without the prefix answers `refused` | ||
| 1914 | 1914 | until it is re-created with `forge: ` in front of its message. |
| 1915 | 1915 | |
| 1916 | 1916 | `reach` is a boards source for an admin only (anyone else gets 403, an agent too): `visitors` per day, `lcp_p75` and `inp_p75` per route per day, `server_p50` and `server_p95` per route per day (the beacon's time to first byte, the only per-route server timing the forge keeps), and `accounts_active_1d`, `accounts_active_7d` and `accounts_total` as of now. Filter by `metric`, group by `route` or a time bucket. Percentiles are nearest-rank within one route and day, so a `max` or `avg` over several days is a summary of daily percentiles, not a percentile of the period. Account activity comes from a session's last-seen time, so it is a count as of now, not a history. No dependency was added. |
| 1917 | ||
| 1918 | ### The board's view bar | |
| 1919 | ||
| 1920 | `/boards/:slug` opens with a bar: time range (1h, 6h, 24h, 7d, 30d, all, or a custom From/To day range), compare to previous, live on/off, and the variables repo, app and crew member. All of it is the address (`?range=6h&compare=1&repo=owner/name`), a view and never a saved setting; the form below the grid only edits the board's defaults. A variable filters every panel whose source has the column (`repo_id`, `actor_id`/`agent_id`); one naming nothing that exists shows empty panels. Compare sets a count or sum beside the same-length window before it. The grid is twelve columns (width 1 = 4, 2 = 6, 3 = 12). Left out: keyboard shortcuts for the ranges, compare for averages and charts other than the total. No dependency was added (`src/web/pages/boards-bar.ts`). |
src/web/pages/boards-bar.ts added +178
| @@ -0,0 +1,178 @@ | ||
| 1 | /** | |
| 2 | * The view bar at the top of a board: the time range, compare-to-previous, | |
| 3 | * live on/off and the variables (repo, app, crew member) that filter every | |
| 4 | * panel. All of it is the address (`?range=6h&repo=forge/boards`), a view | |
| 5 | * and never a saved setting: the board file keeps only its own defaults. | |
| 6 | * `readView` is the one door in — a value that does not parse falls back to | |
| 7 | * the board's default — and the other functions turn a view into plain | |
| 8 | * query changes and plain HTML. | |
| 9 | */ | |
| 10 | import type { Database } from "bun:sqlite"; | |
| 11 | import { SOURCES } from "../../boards/sources.ts"; | |
| 12 | import type { Board } from "../../boards/store.ts"; | |
| 13 | import { getRepo } from "../../core/repos.ts"; | |
| 14 | import { getUserByName } from "../../core/users.ts"; | |
| 15 | import { html, raw, type Html } from "../html.ts"; | |
| 16 | import { btn } from "../ui.ts"; | |
| 17 | ||
| 18 | export const VIEW_RANGES = ["1h", "6h", "24h", "7d", "30d", "all"] as const; | |
| 19 | const RANGE_MS: Record<string, number> = { "1h": 3_600_000, "6h": 21_600_000, "24h": 86_400_000, "7d": 604_800_000, "30d": 2_592_000_000 }; | |
| 20 | const DAY = /^\d{4}-\d{2}-\d{2}$/; | |
| 21 | const NAME = /^[A-Za-z0-9._\/-]{1,80}$/; | |
| 22 | const OPTIONS = 200; | |
| 23 | ||
| 24 | export type View = { | |
| 25 | /** A quick range, or "custom" when `from` is set. */ | |
| 26 | range: string; | |
| 27 | from: string; | |
| 28 | to: string; | |
| 29 | compare: boolean; | |
| 30 | live: boolean; | |
| 31 | repo: string; | |
| 32 | app: string; | |
| 33 | member: string; | |
| 34 | }; | |
| 35 | ||
| 36 | /** The view an address asks for, over the board's defaults. */ | |
| 37 | export function readView(params: URLSearchParams, board: Board): View { | |
| 38 | const pick = (k: string, ok: (s: string) => boolean) => { | |
| 39 | const v = params.get(k) ?? ""; | |
| 40 | return ok(v) ? v : ""; | |
| 41 | }; | |
| 42 | const day = (s: string) => DAY.test(s) && !Number.isNaN(Date.parse(s)); | |
| 43 | const from = pick("from", day); | |
| 44 | const to = from === "" ? "" : pick("to", day); | |
| 45 | const asked = params.get("range") ?? ""; | |
| 46 | const range = from !== "" ? "custom" : (VIEW_RANGES as readonly string[]).includes(asked) ? asked : board.range; | |
| 47 | const live = params.get("live"); | |
| 48 | return { | |
| 49 | range, from, to, | |
| 50 | compare: params.get("compare") === "1" && range !== "all", | |
| 51 | live: live === "1" ? true : live === "0" ? false : board.refresh !== "off", | |
| 52 | repo: pick("repo", (s) => NAME.test(s)), | |
| 53 | app: pick("app", (s) => NAME.test(s)), | |
| 54 | member: pick("member", (s) => NAME.test(s)), | |
| 55 | }; | |
| 56 | } | |
| 57 | ||
| 58 | /** The address of a view: only what differs from the board's defaults. */ | |
| 59 | export function viewParams(v: View, board: Board): URLSearchParams { | |
| 60 | const p = new URLSearchParams(); | |
| 61 | if (v.from !== "") { | |
| 62 | p.set("from", v.from); | |
| 63 | if (v.to !== "") p.set("to", v.to); | |
| 64 | } else if (v.range !== board.range) p.set("range", v.range); | |
| 65 | if (v.compare) p.set("compare", "1"); | |
| 66 | if (v.live !== (board.refresh !== "off")) p.set("live", v.live ? "1" : "0"); | |
| 67 | for (const k of ["repo", "app", "member"] as const) if (v[k] !== "") p.set(k, v[k]); | |
| 68 | return p; | |
| 69 | } | |
| 70 | ||
| 71 | /** The window a view looks at: ISO bounds, `from` absent for all time. */ | |
| 72 | export function windowOf(v: View, now = Date.now()): { from?: string; to?: string } { | |
| 73 | if (v.from !== "") { | |
| 74 | const to = v.to === "" ? undefined : new Date(Date.parse(v.to) + 86_400_000).toISOString(); | |
| 75 | return { from: new Date(v.from).toISOString(), ...(to ? { to } : {}) }; | |
| 76 | } | |
| 77 | const ms = RANGE_MS[v.range]; | |
| 78 | return ms === undefined ? {} : { from: new Date(now - ms).toISOString() }; | |
| 79 | } | |
| 80 | ||
| 81 | /** The window of the same length that ended where this one began. */ | |
| 82 | export function previousOf(w: { from?: string; to?: string }, now = Date.now()): { from: string; to: string } | undefined { | |
| 83 | if (w.from === undefined) return undefined; | |
| 84 | const start = Date.parse(w.from); | |
| 85 | const span = (w.to === undefined ? now : Date.parse(w.to)) - start; | |
| 86 | return { from: new Date(start - span).toISOString(), to: w.from }; | |
| 87 | } | |
| 88 | ||
| 89 | /** The ids the variables name, or `none` when one names nothing (a panel then shows no rows). */ | |
| 90 | function pinned(db: Database, v: View): { repo?: string; member?: string } | "none" { | |
| 91 | const out: { repo?: string; member?: string } = {}; | |
| 92 | if (v.repo !== "") { | |
| 93 | const [o, n] = v.repo.split("/"); | |
| 94 | const r = o && n ? getRepo(db, o, n) : undefined; | |
| 95 | if (!r) return "none"; | |
| 96 | out.repo = r.id; | |
| 97 | } | |
| 98 | if (v.app !== "") { | |
| 99 | const row = db.query<{ repo_id: string }, [string]>("SELECT repo_id FROM app WHERE slug = ?").get(v.app); | |
| 100 | if (!row || (out.repo !== undefined && out.repo !== row.repo_id)) return "none"; | |
| 101 | out.repo = row.repo_id; | |
| 102 | } | |
| 103 | if (v.member !== "") { | |
| 104 | const u = getUserByName(db, v.member); | |
| 105 | if (!u) return "none"; | |
| 106 | out.member = u.id; | |
| 107 | } | |
| 108 | return out; | |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * The panel's query under a view: the window when the query names no start | |
| 113 | * of its own, and the variables as filters on the columns the source has | |
| 114 | * (a source without `repo_id` ignores the repo variable). `null` when a | |
| 115 | * variable names nothing that exists. | |
| 116 | */ | |
| 117 | export function viewQuery( | |
| 118 | db: Database, query: Record<string, unknown>, v: View, window: { from?: string; to?: string }, | |
| 119 | ): Record<string, unknown> | null { | |
| 120 | const q: Record<string, unknown> = { ...query }; | |
| 121 | if (q.from === undefined && window.from !== undefined) { | |
| 122 | q.from = window.from; | |
| 123 | if (window.to !== undefined && q.to === undefined) q.to = window.to; | |
| 124 | } | |
| 125 | const ids = pinned(db, v); | |
| 126 | if (ids === "none") return null; | |
| 127 | const cols = SOURCES[String(query.source)]?.columns ?? {}; | |
| 128 | const filter = Array.isArray(q.filter) ? [...q.filter] : []; | |
| 129 | if (ids.repo !== undefined && "repo_id" in cols) filter.push({ column: "repo_id", op: "eq", value: ids.repo }); | |
| 130 | const who = "actor_id" in cols ? "actor_id" : "agent_id" in cols ? "agent_id" : ""; | |
| 131 | if (ids.member !== undefined && who !== "") filter.push({ column: who, op: "eq", value: ids.member }); | |
| 132 | if (filter.length > 0) q.filter = filter; | |
| 133 | return q; | |
| 134 | } | |
| 135 | ||
| 136 | const choices = (db: Database, sql: string): string[] => | |
| 137 | db.query<{ n: string }, [number]>(sql).all(OPTIONS).map((r) => r.n); | |
| 138 | ||
| 139 | const select = (name: string, label: string, current: string, options: string[]) => | |
| 140 | html`<label class="vb-field"><span>${label}</span><select class="input" name="${name}"> | |
| 141 | <option value="">all</option> | |
| 142 | ${(options.includes(current) || current === "" ? options : [current, ...options]).map( | |
| 143 | (o) => html`<option value="${o}" ${o === current ? raw("selected") : null}>${o}</option>`, | |
| 144 | )} | |
| 145 | </select></label>`; | |
| 146 | ||
| 147 | /** The bar: range links, then one GET form for the rest. Nothing here posts. */ | |
| 148 | export function viewBar(db: Database, slug: string, board: Board, v: View): Html { | |
| 149 | const at = (patch: Partial<View>) => { | |
| 150 | const p = viewParams({ ...v, ...patch }, board).toString(); | |
| 151 | return `/boards/${slug}${p === "" ? "" : `?${p}`}`; | |
| 152 | }; | |
| 153 | const repos = choices(db, "SELECT o.username || '/' || r.name AS n FROM repo r JOIN user o ON o.id = r.owner_id ORDER BY n LIMIT ?"); | |
| 154 | const apps = choices(db, "SELECT slug AS n FROM app ORDER BY slug LIMIT ?"); | |
| 155 | const members = choices(db, "SELECT username AS n FROM user WHERE kind = 'user' AND is_agent = 1 ORDER BY username LIMIT ?"); | |
| 156 | return html`<nav class="vbar" aria-label="View"> | |
| 157 | <div class="vb-ranges" role="group" aria-label="Time range"> | |
| 158 | ${VIEW_RANGES.map( | |
| 159 | (r) => html`<a class="vb-range" href="${at({ range: r, from: "", to: "" })}" ${v.range === r ? raw('aria-current="true"') : null}>${r}</a>`, | |
| 160 | )} | |
| 161 | </div> | |
| 162 | <form method="get" action="/boards/${slug}" class="vb-form"> | |
| 163 | <label class="vb-field"><span>From</span><input class="input" type="date" name="from" value="${v.from}" /></label> | |
| 164 | <label class="vb-field"><span>To</span><input class="input" type="date" name="to" value="${v.to}" /></label> | |
| 165 | ${v.range !== "custom" && v.range !== board.range ? html`<input type="hidden" name="range" value="${v.range}" />` : null} | |
| 166 | <label class="vb-check"><input type="checkbox" name="compare" value="1" ${v.compare ? raw("checked") : null} /> compare to previous</label> | |
| 167 | <label class="vb-field"><span>Live</span><select class="input" name="live"> | |
| 168 | <option value="1" ${v.live ? raw("selected") : null}>on</option> | |
| 169 | <option value="0" ${v.live ? null : raw("selected")}>off</option> | |
| 170 | </select></label> | |
| 171 | ${select("repo", "Repo", v.repo, repos)} | |
| 172 | ${select("app", "App", v.app, apps)} | |
| 173 | ${select("member", "Crew member", v.member, members)} | |
| 174 | ${btn("Apply", { variant: "primary", size: "sm" })} | |
| 175 | <a class="muted small" href="/boards/${slug}">reset</a> | |
| 176 | </form> | |
| 177 | </nav>`; | |
| 178 | } |
src/web/pages/boards-view.ts +37 −18
| @@ -21,19 +21,32 @@ import { fragment, page } from "../layout.ts"; | ||
| 21 | 21 | import { btn, card, empty } from "../ui.ts"; |
| 22 | 22 | import { flowPanel, flowPanelCss } from "./flow-panel.ts"; |
| 23 | 23 | import { isPinned, listPins } from "../../boards/pins.ts"; |
| 24 | import { previousOf, readView, viewBar, viewParams, viewQuery, windowOf, type View } from "./boards-bar.ts"; | |
| 24 | 25 | import { cellsOf, chartBox, paramsOf, queryFields, readParams, runFor } from "./boards.ts"; |
| 25 | 26 | |
| 26 | const RANGE_MS = { "1h": 3_600_000, "24h": 86_400_000, "7d": 604_800_000, "30d": 2_592_000_000 } as const; | |
| 27 | 27 | /** The events the panels' queries read (runs, pulls, checks, issues, usage): any of them may change a chart. */ |
| 28 | 28 | const READS = "run.queued,run.started,run.finished,pull.*,check.*,issue.*,usage.*"; |
| 29 | 29 | |
| 30 | 30 | const option = (value: string, selected: boolean) => |
| 31 | 31 | html`<option value="${value}" ${selected ? raw("selected") : null}>${value}</option>`; |
| 32 | 32 | |
| 33 | /** The panel's query as asked, with the board's range as its start when it names none. */ | |
| 34 | function ranged(query: Record<string, unknown>, range: Board["range"]): Record<string, unknown> { | |
| 35 | if (range === "all" || query.from !== undefined) return query; | |
| 36 | return { ...query, from: new Date(Date.now() - RANGE_MS[range]).toISOString() }; | |
| 33 | /** The sum of an answer's values: what "compare to previous" sets side by side. */ | |
| 34 | const total = (rows: readonly { value: number | null }[]) => rows.reduce((n, r) => n + (r.value ?? 0), 0); | |
| 35 | ||
| 36 | /** "▲ 12% vs previous (40)" for a count or a sum over a window; nothing for anything else. */ | |
| 37 | function versus( | |
| 38 | app: App, panel: Panel, view: View, now: { rows: readonly { value: number | null }[] }, window: { from?: string; to?: string }, | |
| 39 | run: typeof runFor, | |
| 40 | ): Html | null { | |
| 41 | const before = previousOf(window); | |
| 42 | if (!view.compare || !before || panel.query.from !== undefined) return null; | |
| 43 | if (panel.query.aggregate !== "count" && panel.query.aggregate !== "sum") return null; | |
| 44 | const q = viewQuery(app.db, { ...panel.query, from: before.from, to: before.to }, view, {}); | |
| 45 | const was = q && run(app.db, panel.chart, q); | |
| 46 | if (!was || !was.ok) return null; | |
| 47 | const [a, b] = [total(now.rows), total(was.rows)]; | |
| 48 | const change = b === 0 ? "" : ` ${a >= b ? "▲" : "▼"} ${String(Math.round((Math.abs(a - b) / b) * 100))}%`; | |
| 49 | return html`<p class="muted small vb-vs">${String(a)} now · ${String(b)} previous${change}</p>`; | |
| 37 | 50 | } |
| 38 | 51 | |
| 39 | 52 | export function drawPanel( |
| @@ -42,12 +55,15 @@ export function drawPanel( | ||
| 42 | 55 | panel: Panel, |
| 43 | 56 | aside: Html | null = null, |
| 44 | 57 | run: typeof runFor = runFor, |
| 58 | view: View = readView(new URLSearchParams(), board), | |
| 45 | 59 | ): Html { |
| 46 | const answer = run(app.db, panel.chart, ranged(panel.query, board.range)); | |
| 60 | const window = windowOf(view); | |
| 61 | const query = viewQuery(app.db, panel.query, view, window); | |
| 62 | const answer = query ? run(app.db, panel.chart, query) : { ok: true as const, rows: [] }; | |
| 47 | 63 | const q = paramsOf(panel.query, panel.chart); |
| 48 | 64 | const body = !answer.ok |
| 49 | 65 | ? html`<p class="muted small">${answer.error}</p>` |
| 50 | : chartBox(panel.chart, cellsOf(answer.rows), panel.title); | |
| 66 | : html`${chartBox(panel.chart, cellsOf(answer.rows), panel.title)}${versus(app, panel, view, answer, window, run)}`; | |
| 51 | 67 | const explore = new URLSearchParams(Object.entries(q).filter(([k, v]) => v !== "" && k !== "rows")); |
| 52 | 68 | return html`<div class="card bp-w${String(panel.w)} bp-h${String(panel.h)}"> |
| 53 | 69 | <div class="card-head"><h2>${panel.title}</h2> |
| @@ -58,17 +74,18 @@ export function drawPanel( | ||
| 58 | 74 | } |
| 59 | 75 | |
| 60 | 76 | /** The grid alone: what the page shows and what the live address answers with. */ |
| 61 | function grid(app: App, slug: string, board: Board): Html { | |
| 77 | function grid(app: App, slug: string, board: Board, view: View): Html { | |
| 78 | const query = viewParams(view, board).toString(); | |
| 62 | 79 | return html`<section |
| 63 | 80 | class="bgrid" |
| 64 | 81 | id="board-grid" |
| 65 | ${board.refresh === "off" | |
| 66 | ? null | |
| 67 | : html`data-live="${READS}" data-live-src="/boards/${slug}/live"`} | |
| 82 | ${view.live | |
| 83 | ? html`data-live="${READS}" data-live-src="/boards/${slug}/live${query === "" ? "" : `?${query}`}"` | |
| 84 | : null} | |
| 68 | 85 | > |
| 69 | 86 | ${board.panels.length === 0 |
| 70 | 87 | ? html`<p class="muted">No panels yet. Add one below.</p>` |
| 71 | : board.panels.map((p) => drawPanel(app, board, p))} | |
| 88 | : board.panels.map((p) => drawPanel(app, board, p, null, runFor, view))} | |
| 72 | 89 | </section>`; |
| 73 | 90 | } |
| 74 | 91 | |
| @@ -165,13 +182,13 @@ function manage(slug: string, board: Board, adding: Html, pins: ReturnType<typeo | ||
| 165 | 182 | ${btn("Rename")} |
| 166 | 183 | </form> |
| 167 | 184 | <form method="post" action="/boards/${slug}/settings" class="bform"> |
| 168 | <label class="qf"><span>Range</span><select class="input" name="range">${RANGES.map((r) => option(r, r === board.range))}</select></label> | |
| 169 | <label class="qf"><span>Live</span><select class="input" name="refresh"><option value="off" ${board.refresh === "off" ? raw("selected") : null}>off</option><option value="10s" ${board.refresh === "off" ? null : raw("selected")}>on: redraw when events arrive</option></select></label> | |
| 170 | ${btn("Save settings")} | |
| 185 | <label class="qf"><span>Default range</span><select class="input" name="range">${RANGES.map((r) => option(r, r === board.range))}</select></label> | |
| 186 | <label class="qf"><span>Default live</span><select class="input" name="refresh"><option value="off" ${board.refresh === "off" ? raw("selected") : null}>off</option><option value="10s" ${board.refresh === "off" ? null : raw("selected")}>on: redraw when events arrive</option></select></label> | |
| 187 | ${btn("Save defaults")} | |
| 171 | 188 | </form> |
| 172 | 189 | <form method="post" action="/boards/${slug}/duplicate" class="bform">${btn("Duplicate")}</form> |
| 173 | 190 | <form method="post" action="/boards/${slug}/delete" class="bform">${btn("Delete this board", { variant: "danger" })}</form>`, |
| 174 | { head: html`<h2>Board</h2>` }, | |
| 191 | { head: html`<h2>Board defaults</h2>` }, | |
| 175 | 192 | )}`; |
| 176 | 193 | } |
| 177 | 194 | |
| @@ -182,13 +199,15 @@ export async function boardShow(ctx: Ctx): Promise<Response> { | ||
| 182 | 199 | const shown = await timePhaseAsync(ctx.request, "board", () => showBoard(ctx.app, ctx.auth.user, slug)); |
| 183 | 200 | if (!shown.ok) return notFoundPage(ctx); |
| 184 | 201 | const board = shown.value; |
| 185 | const panels = timePhase(ctx.request, "grid", () => grid(ctx.app, slug, board)); | |
| 202 | const view = readView(ctx.url.searchParams, board); | |
| 203 | const panels = timePhase(ctx.request, "grid", () => grid(ctx.app, slug, board, view)); | |
| 186 | 204 | const rendering = performance.now(); |
| 187 | 205 | const response = page({ |
| 188 | 206 | ctx, |
| 189 | 207 | title: board.title, |
| 190 | 208 | body: html`<div class="page-head"><h1>${board.title}</h1> |
| 191 | 209 | ${btn("All boards", { href: "/boards", variant: "ghost" })}</div> |
| 210 | ${viewBar(ctx.app.db, slug, board, view)} | |
| 192 | 211 | ${panels} |
| 193 | 212 | ${manage(slug, board, queryFields(readParams(ctx.url.searchParams)), listPins(ctx.app.db, ctx.auth.user?.id ?? ""))}`, |
| 194 | 213 | scriptJs: liveRegionsJs, |
| @@ -203,5 +222,5 @@ export async function boardLive(ctx: Ctx): Promise<Response> { | ||
| 203 | 222 | const slug = ctx.params.slug ?? ""; |
| 204 | 223 | const shown = await showBoard(ctx.app, ctx.auth.user, slug); |
| 205 | 224 | if (!shown.ok) return fragment(html``, { status: 404 }); |
| 206 | return fragment(grid(ctx.app, slug, shown.value), { request: ctx.request }); | |
| 225 | return fragment(grid(ctx.app, slug, shown.value, readView(ctx.url.searchParams, shown.value)), { request: ctx.request }); | |
| 207 | 226 | } |
src/web/style/content.ts +14 −4
| @@ -393,15 +393,25 @@ a.origin-chip:hover { border-color: var(--border-strong); color: var(--fg); } | ||
| 393 | 393 | .bt-group th, .bt-group td { background: var(--muted); font-weight: 600; } |
| 394 | 394 | .sys-pair { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; margin-bottom: 16px; } |
| 395 | 395 | .sys-pair .chart-box { max-width: none; } |
| 396 | .bgrid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; } | |
| 397 | .bp-w2 { grid-column: span 2; } | |
| 398 | .bp-w3 { grid-column: span 3; } | |
| 396 | .bgrid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); grid-auto-flow: dense; gap: 12px; margin-bottom: 16px; } | |
| 397 | .bp-w1 { grid-column: span 4; } | |
| 398 | .bp-w2 { grid-column: span 6; } | |
| 399 | .bp-w3 { grid-column: span 12; } | |
| 400 | .bmanage .row-actions { opacity: 1; } | |
| 401 | .vbar { display: flex; flex-wrap: wrap; align-items: end; gap: 8px 16px; margin-bottom: 12px; font-variant-numeric: tabular-nums; } | |
| 402 | .vb-ranges { display: flex; gap: 2px; } | |
| 403 | .vb-range { padding: 4px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 13px; } | |
| 404 | .vb-range[aria-current] { background: var(--accent); color: var(--accent-fg, #fff); border-color: var(--accent); } | |
| 405 | .vb-form { display: flex; flex-wrap: wrap; align-items: end; gap: 8px 12px; } | |
| 406 | .vb-field { display: grid; gap: 2px; font-size: 12px; } | |
| 407 | .vb-check { font-size: 13px; padding-bottom: 6px; } | |
| 408 | .vb-vs { margin: 4px 0 0; } | |
| 399 | 409 | .bgrid .chart-box { max-width: none; } |
| 400 | 410 | .bp-h2 .chart-box { aspect-ratio: 720 / 400; } |
| 401 | 411 | .bp-h3 .chart-box { aspect-ratio: 720 / 540; } |
| 402 | 412 | .bgrid h2 { font-size: 14px; } |
| 403 | 413 | .bmanage { display: grid; gap: 8px; } |
| 404 | @media (max-width: 720px) { .bgrid { grid-template-columns: minmax(0, 1fr); } .bp-w2, .bp-w3 { grid-column: auto; } } | |
| 414 | @media (max-width: 720px) { .bgrid { grid-template-columns: minmax(0, 1fr); } .bp-w1, .bp-w2, .bp-w3 { grid-column: auto; } } | |
| 405 | 415 | /* The stacked bar chart's series colours, for its bars (fleet-graph.ts). |
| 406 | 416 | Okabe–Ito, told apart with common colour-vision deficiencies; the eighth, |
| 407 | 417 | the folded "others", is grey. */ |
test/boards-bar.test.ts added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | /** The board's view bar (#3695): range, compare, live and variables are the address; they filter every panel and save nothing. */ | |
| 2 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { writeBoard, showBoard } from "../src/actions/boards.ts"; | |
| 7 | import { createApp } from "../src/app.ts"; | |
| 8 | import { createToken } from "../src/core/tokens.ts"; | |
| 9 | import { createUser, type User } from "../src/core/users.ts"; | |
| 10 | import { serve } from "../src/http/server.ts"; | |
| 11 | import { previousOf, readView, viewParams, windowOf } from "../src/web/pages/boards-bar.ts"; | |
| 12 | ||
| 13 | let dir: string; | |
| 14 | let app: ReturnType<typeof createApp>; | |
| 15 | let servers: Awaited<ReturnType<typeof serve>>; | |
| 16 | let alice: User; | |
| 17 | let token: string; | |
| 18 | ||
| 19 | const board = { title: "B", range: "24h", refresh: "off", panels: [ | |
| 20 | { title: "Runs", chart: "number", query: { source: "runs", aggregate: "count" }, w: 2, h: 1 }, | |
| 21 | { title: "Pulls", chart: "number", query: { source: "pulls", aggregate: "count" }, w: 2, h: 1 }, | |
| 22 | ] } as const; | |
| 23 | const get = (path: string) => fetch(servers.url + path, { headers: { authorization: `Bearer ${token}` } }).then((r) => r.text()); | |
| 24 | const iso = (msAgo: number) => new Date(Date.now() - msAgo).toISOString(); | |
| 25 | ||
| 26 | beforeAll(async () => { | |
| 27 | dir = await mkdtemp(join(tmpdir(), "forge-boards-bar-")); | |
| 28 | app = createApp({ dataDir: dir, port: 0 }); | |
| 29 | servers = await serve(app); | |
| 30 | const r = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 31 | if (!r.ok) throw new Error(r.error); | |
| 32 | alice = r.value; | |
| 33 | token = createToken(app.db, alice.id, "t", "admin").token; | |
| 34 | app.db.run("INSERT INTO repo (id, owner_id, name) VALUES ('r1', ?, 'one'), ('r2', ?, 'two')", [alice.id, alice.id]); | |
| 35 | const ins = app.db.prepare("INSERT INTO run (id, repo_id, kind, state, created_at) VALUES (?, ?, 'agent', 'ok', ?)"); | |
| 36 | ins.run("n1", "r1", iso(3_600_000 * 2)); // 2h ago, repo one | |
| 37 | ins.run("n2", "r2", iso(3_600_000 * 3)); // repo two | |
| 38 | ins.run("n3", "r2", iso(3_600_000 * 30)); // 30h ago: in the previous 24h window | |
| 39 | const w = await writeBoard(app, alice, "mine", board); | |
| 40 | if (!w.ok) throw new Error(JSON.stringify(w.error)); | |
| 41 | }); | |
| 42 | ||
| 43 | afterAll(async () => { | |
| 44 | servers.stop(); | |
| 45 | app.close(); | |
| 46 | await rm(dir, { recursive: true, force: true }); | |
| 47 | }); | |
| 48 | ||
| 49 | const b = { ...board, panels: [...board.panels] } as never; | |
| 50 | const view = (q: string) => readView(new URLSearchParams(q), b); | |
| 51 | /** The big numbers of the panels, in order (Runs, then Pulls). */ | |
| 52 | const bigs = (page: string) => [...page.matchAll(/font-size="72">(\d+)</g)].map((m) => Number(m[1])); | |
| 53 | ||
| 54 | describe("the view", () => { | |
| 55 | test("an address is read over the board's defaults; junk falls back", () => { | |
| 56 | expect(view("")).toMatchObject({ range: "24h", compare: false, live: false, repo: "" }); | |
| 57 | expect(view("range=6h&compare=1&live=1&repo=alice/one")).toMatchObject({ range: "6h", compare: true, live: true, repo: "alice/one" }); | |
| 58 | expect(view("range=forever&from=nope&repo=%3Cb%3E")).toMatchObject({ range: "24h", from: "", repo: "" }); | |
| 59 | expect(view("from=2026-10-01&to=2026-10-03").range).toBe("custom"); | |
| 60 | expect(view("range=all&compare=1").compare).toBe(false); | |
| 61 | }); | |
| 62 | ||
| 63 | test("the address carries only what differs from the defaults", () => { | |
| 64 | expect(viewParams(view(""), b).toString()).toBe(""); | |
| 65 | expect(viewParams(view("range=7d&member=scout&live=1"), b).toString()).toBe("range=7d&live=1&member=scout"); | |
| 66 | }); | |
| 67 | ||
| 68 | test("windows: a custom day range is whole days, and compare is the span before it", () => { | |
| 69 | const w = windowOf(view("from=2026-10-01&to=2026-10-03")); | |
| 70 | expect(w).toEqual({ from: "2026-10-01T00:00:00.000Z", to: "2026-10-04T00:00:00.000Z" }); | |
| 71 | expect(previousOf(w)).toEqual({ from: "2026-09-28T00:00:00.000Z", to: "2026-10-01T00:00:00.000Z" }); | |
| 72 | expect(previousOf({})).toBeUndefined(); | |
| 73 | }); | |
| 74 | }); | |
| 75 | ||
| 76 | describe("the board page", () => { | |
| 77 | test("the bar is at the top, above the grid, with range links and variables", async () => { | |
| 78 | const page = await get("/boards/mine"); | |
| 79 | expect(page.indexOf('class="vbar"')).toBeGreaterThan(-1); | |
| 80 | expect(page.indexOf('class="vbar"')).toBeLessThan(page.indexOf('id="board-grid"')); | |
| 81 | expect(page).toContain('href="/boards/mine?range=6h"'); | |
| 82 | expect(page).toContain('name="member"'); | |
| 83 | expect(page).toContain("alice/one"); | |
| 84 | }); | |
| 85 | ||
| 86 | test("changing the range changes the data and the live address, and saves nothing", async () => { | |
| 87 | const before = await showBoard(app, alice, "mine"); | |
| 88 | expect(bigs(await get("/boards/mine"))[0]).toBe(2); | |
| 89 | expect(bigs(await get("/boards/mine?range=7d"))[0]).toBe(3); | |
| 90 | const narrow = await get("/boards/mine?range=1h&live=1"); | |
| 91 | expect(bigs(narrow)[0]).toBe(0); | |
| 92 | expect(narrow).toContain('data-live-src="/boards/mine/live?range=1h&live=1"'); | |
| 93 | expect(narrow).toContain('aria-current="true">1h'); | |
| 94 | expect(await showBoard(app, alice, "mine")).toEqual(before); | |
| 95 | }); | |
| 96 | ||
| 97 | test("a repo variable filters every panel that has a repo", async () => { | |
| 98 | expect(bigs(await get("/boards/mine?range=7d&repo=alice/one"))[0]).toBe(1); | |
| 99 | expect(bigs(await get("/boards/mine?range=7d&repo=alice/two"))[0]).toBe(2); | |
| 100 | expect(bigs(await get("/boards/mine?range=7d&repo=alice/missing"))[0] ?? 0).toBe(0); | |
| 101 | }); | |
| 102 | ||
| 103 | test("compare shows the previous window beside the count", async () => { | |
| 104 | const page = await get("/boards/mine?compare=1"); | |
| 105 | expect(page).toContain("2 now · 1 previous"); | |
| 106 | expect(await get("/boards/mine")).not.toContain(" now · "); | |
| 107 | }); | |
| 108 | ||
| 109 | test("the live address answers with the same view", async () => { | |
| 110 | const live = await get("/boards/mine/live?compare=1"); | |
| 111 | expect(live).toContain("2 now · 1 previous"); | |
| 112 | expect(live).not.toContain('class="vbar"'); | |
| 113 | }); | |
| 114 | ||
| 115 | test("the grid is twelve columns and row actions are not hidden", async () => { | |
| 116 | const css = await Bun.file("src/web/style/content.ts").text(); | |
| 117 | expect(css).toContain("repeat(12, minmax(0, 1fr))"); | |
| 118 | expect(css).toContain(".bmanage .row-actions { opacity: 1; }"); | |
| 119 | }); | |
| 120 | }); |
new above
offline