Public: anyone can read this; the crew can change it.
Home's KPI row adds builders active of slots, today's spend of the ceiling, and the age of the last good backup #2912
scout/forge:scout/2740-home-s-kpi-row-adds-builders-active-of-s → main 1 commit fork
7 files
changed +193
−15
README.md +7 −4
| @@ -254,12 +254,15 @@ settings until asked. `/?all=1` is the whole home, and its "Just my apps" link | ||
| 254 | 254 | comes back. The terminal's first screen opens with the same lines, from the |
| 255 | 255 | same query (`src/core/client-apps.ts`). |
| 256 | 256 | |
| 257 | Home shows three figures first, from `src/flow/kpis.ts`: shipped per hour (with | |
| 258 | a 24 h sparkline), the live version and since when, and the median check time. | |
| 257 | Home shows figures first, from `src/flow/kpis.ts`: shipped per hour (with | |
| 258 | a 24 h sparkline), the live version and since when, and the median check time; | |
| 259 | then, from `src/flow/kpis_ops.ts`, builders active of slots, today's spend of | |
| 260 | the daily ceiling, and the age of the last good off-box backup (red past 24 h | |
| 261 | or when none has landed), linking to `/settings/crew` and `/settings/backups`. | |
| 259 | 262 | They count every repository, so only a site admin sees them, on both doors; |
| 260 | 263 | nobody else does, and neither does the lean client home. On the web each is a |
| 261 | card linking to `/board`, `/releases` or `/boards`. The terminal's first screen | |
| 262 | shows the same three as lines above the forge strip (`kpiLines` in | |
| 264 | card linking to its page. The terminal's first screen | |
| 265 | shows the first three as lines above the forge strip (`kpiLines` in | |
| 263 | 266 | `src/flow/kpi_words.ts`). They are read when the page or screen loads and |
| 264 | 267 | do not refresh on their own. |
| 265 | 268 |
src/flow/kpis_ops.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** | |
| 2 | * Home's three operating figures (#2740), as plain data: crew builders at | |
| 3 | * work of the slots the repositories on their own allow, what the crew spent | |
| 4 | * today of the daily ceiling, and how old the last good off-box backup is. | |
| 5 | * The clock is the caller's. | |
| 6 | * | |
| 7 | * Builders and slots come from `autonomyStatus`, so the count is the one the | |
| 8 | * loop enforces. The ceiling is the forge's when one is set, else the sum of | |
| 9 | * the ceilings of the repositories on their own. The backup is the newest | |
| 10 | * snapshot that landed (`snapshot/state.json`), which is what Backups shows. | |
| 11 | */ | |
| 12 | ||
| 13 | import type { App } from "../app.ts"; | |
| 14 | import { autonomyStatus, spentTodayEverywhere } from "../crew/autonomy.ts"; | |
| 15 | import { listAutonomousRepos } from "../core/repos.ts"; | |
| 16 | import { lastSnapshotOutcome } from "../snapshot/outcome.ts"; | |
| 17 | ||
| 18 | /** Past this the backup tile is red. */ | |
| 19 | export const BACKUP_STALE_MS = 24 * 3_600_000; | |
| 20 | ||
| 21 | export interface OpsKpis { | |
| 22 | readonly active: number; | |
| 23 | readonly slots: number; | |
| 24 | readonly spentUsd: number; | |
| 25 | /** The daily ceiling in dollars; null when no repository is on its own and the forge sets none. */ | |
| 26 | readonly ceilingUsd: number | null; | |
| 27 | /** Milliseconds since the last good backup landed; null when none has. */ | |
| 28 | readonly backupAgeMs: number | null; | |
| 29 | } | |
| 30 | ||
| 31 | export function readOpsKpis(app: App, now: Date): OpsKpis { | |
| 32 | let active = 0; | |
| 33 | let slots = 0; | |
| 34 | let ceiling = 0; | |
| 35 | for (const repo of listAutonomousRepos(app.db)) { | |
| 36 | const s = autonomyStatus(app, repo, now); | |
| 37 | active += s.active; | |
| 38 | slots += s.slots; | |
| 39 | ceiling += s.ceilingUsd; | |
| 40 | } | |
| 41 | const forge = app.config.crewCeilingUsd; | |
| 42 | const landed = Date.parse(lastSnapshotOutcome(app.config.dataDir)?.landedAt ?? ""); | |
| 43 | return { | |
| 44 | active, | |
| 45 | slots, | |
| 46 | spentUsd: spentTodayEverywhere(app.db, now), | |
| 47 | ceilingUsd: forge !== undefined ? forge : slots > 0 ? ceiling : null, | |
| 48 | backupAgeMs: Number.isNaN(landed) ? null : Math.max(0, now.getTime() - landed), | |
| 49 | }; | |
| 50 | } | |
| 51 | ||
| 52 | /** An age the way a person reads it: `12m`, `5h`, `3d`. */ | |
| 53 | export function ageWords(ms: number): string { | |
| 54 | const m = Math.floor(ms / 60_000); | |
| 55 | if (m < 60) return `${m}m`; | |
| 56 | const h = Math.floor(m / 60); | |
| 57 | return h < 48 ? `${h}h` : `${Math.floor(h / 24)}d`; | |
| 58 | } |
src/web/studio/feed_style.ts +1
| @@ -27,6 +27,7 @@ export const feedCss = /* css */ ` | ||
| 27 | 27 | /* The figures row: three cards at a fixed height, so first paint is final. */ |
| 28 | 28 | .kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 12px; } |
| 29 | 29 | .kpi { display: grid; grid-template-rows: 20px 32px 20px 24px; align-content: start; gap: 2px; padding: 12px 14px; color: inherit; text-decoration: none; } |
| 30 | .kpi-bad .kpi-value { color: var(--danger); } | |
| 30 | 31 | .kpi:hover { border-color: var(--border-strong); } |
| 31 | 32 | .kpi-value { font-size: 16px; line-height: 32px; font-weight: 600; } |
| 32 | 33 | .kpi-note { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } |
src/web/studio/home.ts +1 −1
| @@ -246,7 +246,7 @@ export async function studioHome( | ||
| 246 | 246 | const ask = firstSteps ?? everydayCard(ctx, user); |
| 247 | 247 | const figures = user.isAdmin && steps === undefined; |
| 248 | 248 | const body = html`<h1 class="sr-only">Home</h1> |
| 249 | ${ask} ${address} ${figures ? kpiRow(ctx.app.db) : liveLine(ctx)} | |
| 249 | ${ask} ${address} ${figures ? kpiRow(ctx.app) : liveLine(ctx)} | |
| 250 | 250 | ${when( |
| 251 | 251 | steps, |
| 252 | 252 | () => html`<p class="small muted" id="lean-home"> |
src/web/studio/kpi_row.ts +30 −7
| @@ -1,13 +1,14 @@ | ||
| 1 | 1 | /** |
| 2 | * Home's row of three figures (#2871), from `flow/kpis.ts`: shipped per hour | |
| 2 | * Home's row of figures (#2871, #2740), from `flow/kpis.ts`: shipped per hour | |
| 3 | 3 | * with a day's sparkline, the live version and since when, and the median |
| 4 | 4 | * check time. Server-rendered, one fixed-height card each, so nothing moves |
| 5 | 5 | * when it paints. Only the studio's words: shipped, change, check time. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | 8 | import { checkTime } from "../../flow/kpi_words.ts"; |
| 10 | 9 | import { readKpis, type Kpis } from "../../flow/kpis.ts"; |
| 10 | import { ageWords, BACKUP_STALE_MS, readOpsKpis, type OpsKpis } from "../../flow/kpis_ops.ts"; | |
| 11 | import type { App } from "../../app.ts"; | |
| 11 | 12 | import { html, raw, type Html } from "../html.ts"; |
| 12 | 13 | import { BOOTED } from "../../release/gate.ts"; |
| 13 | 14 | import { running } from "../../kernel/version.ts"; |
| @@ -29,8 +30,8 @@ export function sparkline(hours: readonly number[]): Html { | ||
| 29 | 30 | ); |
| 30 | 31 | } |
| 31 | 32 | |
| 32 | function tile(label: string, href: string, value: Html | string, full: string, note: string | Html, title: string, extra: Html | null = null): Html { | |
| 33 | return html`<a class="card kpi" href="${href}" aria-label="${label}: ${full}, ${title}"> | |
| 33 | function tile(label: string, href: string, value: Html | string, full: string, note: string | Html, title: string, extra: Html | null = null, bad = false): Html { | |
| 34 | return html`<a class="${bad ? "card kpi kpi-bad" : "card kpi"}" href="${href}" aria-label="${label}: ${full}, ${title}"> | |
| 34 | 35 | <span class="small muted">${label}</span> |
| 35 | 36 | <span class="kpi-value num" title="${full}">${value}</span> |
| 36 | 37 | <span class="kpi-note small muted" title="${title}">${note}</span> |
| @@ -44,7 +45,28 @@ export function liveNow(): { sha: string; at: string } | null { | ||
| 44 | 45 | return commit === null ? null : { sha: commit, at: BOOTED }; |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | export function kpiRowFor(k: Kpis, live: { sha: string; at: string } | null): Html { | |
| 48 | /** The crew, spend and backup tiles; present only when the caller read them. */ | |
| 49 | function opsTiles(o: OpsKpis): Html { | |
| 50 | const usd = (n: number) => `$${n.toFixed(2)}`; | |
| 51 | const stale = o.backupAgeMs !== null && o.backupAgeMs > BACKUP_STALE_MS; | |
| 52 | const none = "No backup has landed yet"; | |
| 53 | const builders = `${o.active} of ${o.slots} ${o.slots === 1 ? "slot" : "slots"} in use`; | |
| 54 | const spend = o.ceilingUsd === null ? "No ceiling set" : `of ${usd(o.ceilingUsd)} a day`; | |
| 55 | return html`${tile("Builders active", "/settings/crew", `${o.active} of ${o.slots}`, builders, "at work now", builders)} | |
| 56 | ${tile("Spent today", "/settings/crew", usd(o.spentUsd), usd(o.spentUsd), spend, spend)} | |
| 57 | ${tile( | |
| 58 | "Last good backup", | |
| 59 | "/settings/backups", | |
| 60 | o.backupAgeMs === null ? "—" : ageWords(o.backupAgeMs), | |
| 61 | o.backupAgeMs === null ? none : `${ageWords(o.backupAgeMs)} old`, | |
| 62 | o.backupAgeMs === null ? none : stale ? "older than a day" : "within the last day", | |
| 63 | o.backupAgeMs === null ? none : stale ? "Older than a day" : "Within the last day", | |
| 64 | null, | |
| 65 | stale || o.backupAgeMs === null, | |
| 66 | )}`; | |
| 67 | } | |
| 68 | ||
| 69 | export function kpiRowFor(k: Kpis, live: { sha: string; at: string } | null, ops: OpsKpis | null = null): Html { | |
| 48 | 70 | const rate = k.landed / k.landedHours.length; |
| 49 | 71 | return html`<section class="kpis" aria-label="The last 24 hours, as of this page load"> |
| 50 | 72 | ${tile( |
| @@ -72,10 +94,11 @@ export function kpiRowFor(k: Kpis, live: { sha: string; at: string } | null): Ht | ||
| 72 | 94 | k.medianCheckMs === null ? "No check finished in 24 h" : "over the last 24 h", |
| 73 | 95 | k.medianCheckMs === null ? "No check finished in 24 h" : "over the last 24 h", |
| 74 | 96 | )} |
| 97 | ${ops === null ? null : opsTiles(ops)} | |
| 75 | 98 | </section>`; |
| 76 | 99 | } |
| 77 | 100 | |
| 78 | 101 | /** The figures count every repository, so the caller shows this to an admin only. */ |
| 79 | export function kpiRow(db: Database, now = new Date()): Html { | |
| 80 | return kpiRowFor(readKpis(db, now), liveNow()); | |
| 102 | export function kpiRow(app: App, now = new Date()): Html { | |
| 103 | return kpiRowFor(readKpis(app.db, now), liveNow(), readOpsKpis(app, now)); | |
| 81 | 104 | } |
test/home-kpi-page.test.ts +3 −3
| @@ -25,7 +25,7 @@ afterAll(async () => { | ||
| 25 | 25 | await rm(dataDir, { recursive: true, force: true }); |
| 26 | 26 | }); |
| 27 | 27 | |
| 28 | test("home renders the three tiles; only an admin sees them", async () => { | |
| 28 | test("home renders the six tiles; only an admin sees them", async () => { | |
| 29 | 29 | const mk = async (username: string, first = false): Promise<User> => { |
| 30 | 30 | const r = await createUser(app.db, { |
| 31 | 31 | origin: "seed", |
| @@ -50,8 +50,8 @@ test("home renders the three tiles; only an admin sees them", async () => { | ||
| 50 | 50 | |
| 51 | 51 | const a = await home(admin); |
| 52 | 52 | expect(a).toContain('class="kpis"'); |
| 53 | expect((a.match(/class="card kpi"/g) ?? []).length).toBe(3); | |
| 54 | for (const href of ["/board", "/releases", "/boards"]) expect(a).toContain(`class="card kpi" href="${href}"`); | |
| 53 | expect((a.match(/class="card kpi(?: kpi-bad)?"/g) ?? []).length).toBe(6); | |
| 54 | for (const href of ["/board", "/releases", "/boards", "/settings/crew", "/settings/backups"]) expect(a).toMatch(new RegExp(`class="card kpi(?: kpi-bad)?" href="${href}"`)); | |
| 55 | 55 | |
| 56 | 56 | // The figures count every repository, so a non-admin sees none. |
| 57 | 57 | expect(await home(bob)).not.toContain('class="kpis"'); |
test/home-kpis-ops.test.ts added +93
| @@ -0,0 +1,93 @@ | ||
| 1 | /** Home's operating figures (#2740): builders of slots, spend of the ceiling, backup age; each from seeded rows. */ | |
| 2 | import { afterAll, beforeAll, 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 { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 9 | import { claimRun, createRun, finishRun } from "../src/core/runs.ts"; | |
| 10 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 11 | import { BACKUP_STALE_MS, readOpsKpis } from "../src/flow/kpis_ops.ts"; | |
| 12 | import { recordSnapshotOutcome } from "../src/snapshot/outcome.ts"; | |
| 13 | import { kpiRowFor } from "../src/web/studio/kpi_row.ts"; | |
| 14 | ||
| 15 | let dataDir: string; | |
| 16 | let app: App; | |
| 17 | const now = new Date(); | |
| 18 | const ago = (ms: number) => new Date(now.getTime() - ms).toISOString(); | |
| 19 | const k = { landedHours: new Array<number>(24).fill(0), landed: 0, live: null, medianCheckMs: null }; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | dataDir = await mkdtemp(join(tmpdir(), "forge-kpis-ops-")); | |
| 23 | app = createApp({ dataDir, port: 0 }); | |
| 24 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 25 | if (!a.ok) throw new Error(a.error); | |
| 26 | const made = await createRepo(app, a.value, { name: "engine", visibility: "private" }); | |
| 27 | if (!made.ok) throw new Error(String(made.error)); | |
| 28 | const agent = await createAgent(app, a.value, { username: "mason" }); | |
| 29 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 30 | expect(addToCrew(app, a.value, made.value, "mason").ok).toBe(true); | |
| 31 | app.db.query("UPDATE repo SET autonomy = 'on', autonomy_by = ?1, autonomy_slots = 3, autonomy_ceiling_usd = 20 WHERE id = ?2").run(a.value.id, made.value.id); | |
| 32 | const mason = getUserByName(app.db, "mason")!; | |
| 33 | const mk = (cost: number | null) => { | |
| 34 | const r = createRun(app.db, { repoId: made.value.id, kind: "agent", actorId: mason.id, reason: "assigned" }); | |
| 35 | claimRun(app.db, r.id); | |
| 36 | if (cost !== null) finishRun(app.db, r.id, { state: "ok", summary: "done", costUsd: cost }); | |
| 37 | }; | |
| 38 | mk(null); // in flight | |
| 39 | mk(2.5); | |
| 40 | mk(1); | |
| 41 | }); | |
| 42 | ||
| 43 | afterAll(async () => { | |
| 44 | app.close(); | |
| 45 | await rm(dataDir, { recursive: true, force: true }); | |
| 46 | }); | |
| 47 | ||
| 48 | test("builders, spend and ceiling are read from seeded rows", () => { | |
| 49 | const o = readOpsKpis(app, now); | |
| 50 | expect(o.active).toBe(1); | |
| 51 | expect(o.slots).toBe(3); | |
| 52 | expect(o.spentUsd).toBeCloseTo(3.5, 5); | |
| 53 | expect(o.ceilingUsd).toBe(20); | |
| 54 | const out = kpiRowFor(k, null, o).toString(); | |
| 55 | expect(out).toContain("1 of 3"); | |
| 56 | expect(out).toContain("$3.50"); | |
| 57 | expect(out).toContain("of $20.00 a day"); | |
| 58 | expect(out).toContain('href="/settings/crew"'); | |
| 59 | }); | |
| 60 | ||
| 61 | test("the forge's own ceiling wins over the sum of the repositories'", () => { | |
| 62 | const capped = { ...app, config: { ...app.config, crewCeilingUsd: 50 } }; | |
| 63 | expect(readOpsKpis(capped, now).ceilingUsd).toBe(50); | |
| 64 | }); | |
| 65 | ||
| 66 | test("no backup yet is red and says so", () => { | |
| 67 | const o = readOpsKpis(app, now); | |
| 68 | expect(o.backupAgeMs).toBeNull(); | |
| 69 | const out = kpiRowFor(k, null, o).toString(); | |
| 70 | expect(out).toContain("No backup has landed yet"); | |
| 71 | expect(out).toContain("kpi-bad"); | |
| 72 | }); | |
| 73 | ||
| 74 | test("the backup is green within 24 h and red past it", () => { | |
| 75 | recordSnapshotOutcome(dataDir, { landed: "snap" }, ago(5 * 3_600_000)); | |
| 76 | const fresh = readOpsKpis(app, now); | |
| 77 | expect(fresh.backupAgeMs).toBe(5 * 3_600_000); | |
| 78 | const green = kpiRowFor(k, null, fresh).toString(); | |
| 79 | expect(green).toContain(">5h<"); | |
| 80 | expect(green).not.toContain("kpi-bad"); | |
| 81 | expect(green).toContain('href="/settings/backups"'); | |
| 82 | ||
| 83 | recordSnapshotOutcome(dataDir, { landed: "snap" }, ago(BACKUP_STALE_MS + 60_000)); | |
| 84 | const red = kpiRowFor(k, null, readOpsKpis(app, now)).toString(); | |
| 85 | expect(red).toContain("kpi-bad"); | |
| 86 | expect(red).toContain("Older than a day"); | |
| 87 | }); | |
| 88 | ||
| 89 | test("a failed attempt after a good one does not hide the good one's age", () => { | |
| 90 | recordSnapshotOutcome(dataDir, { landed: "snap" }, ago(2 * 3_600_000)); | |
| 91 | recordSnapshotOutcome(dataDir, { error: "store unreachable" }, ago(60_000)); | |
| 92 | expect(readOpsKpis(app, now).backupAgeMs).toBe(2 * 3_600_000); | |
| 93 | }); |
new above
offline