Public: anyone can read this; the crew can change it.
The cold front page reads every app's runtime status in one scan #16795
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16265-the-cold-front-page-reads-every-app-s-ru → main 0 commits fork 1 thread · 1 unresolved
8 files
changed +128
−33
src/actions/front.ts +9 −7
| @@ -20,7 +20,7 @@ | ||
| 20 | 20 | import type { Database } from "bun:sqlite"; |
| 21 | 21 | import { provenanceUrl, sourceLabel } from "../core/provenance.ts"; |
| 22 | 22 | import type { App } from "../app.ts"; |
| 23 | import { appsRuntime, type EnvState } from "../apps/runtime.ts"; | |
| 23 | import { appsRuntime, type EnvState, type EnvStatus } from "../apps/runtime.ts"; | |
| 24 | 24 | import { getAppByRepo, listPublicApps, type AppRecord } from "../core/apps.ts"; |
| 25 | 25 | import { |
| 26 | 26 | frontPicks, |
| @@ -339,13 +339,15 @@ function buildFrontPage(app: App, now: number): FrontPage { | ||
| 339 | 339 | [...appOf.values()].map((r) => r.id), |
| 340 | 340 | ); |
| 341 | 341 | const runtime = appsRuntime(app); |
| 342 | // Every app's environments in one pass; the page and its pulse share it. | |
| 343 | const statuses = runtime?.statusMany([...appOf.values()]); | |
| 342 | 344 | const imported = importedSources(db, ids); |
| 343 | 345 | const entry = (repo: Repo, isPinned: boolean): FrontEntry => { |
| 344 | 346 | const record = appOf.get(repo.id); |
| 345 | 347 | const production = |
| 346 | record === undefined || runtime === undefined | |
| 348 | record === undefined || statuses === undefined | |
| 347 | 349 | ? undefined |
| 348 | : runtime.status(record).find((env) => env.env === "production"); | |
| 350 | : statuses.get(record.id)?.find((env) => env.env === "production"); | |
| 349 | 351 | // An app open only to signed-in readers shows a stranger a sign-in |
| 350 | 352 | // page: there is no picture of it worth showing them. |
| 351 | 353 | const thumb = |
| @@ -425,7 +427,7 @@ function buildFrontPage(app: App, now: number): FrontPage { | ||
| 425 | 427 | : { ...featuredEntry, crew: crewAtWork(app, featuredEntry.repo) }, |
| 426 | 428 | shelf, |
| 427 | 429 | total: publicApps.length, |
| 428 | pulse: buildPulse(app, shownRepos, publicApps, scores, now), | |
| 430 | pulse: buildPulse(app, shownRepos, publicApps, scores, now, statuses), | |
| 429 | 431 | }; |
| 430 | 432 | } |
| 431 | 433 | |
| @@ -435,15 +437,15 @@ function buildPulse( | ||
| 435 | 437 | publicApps: readonly AppRecord[], |
| 436 | 438 | scores: Scores, |
| 437 | 439 | now: number, |
| 440 | statuses: ReadonlyMap<string, readonly EnvStatus[]> | undefined, | |
| 438 | 441 | ): FrontPulse { |
| 439 | const runtime = appsRuntime(app); | |
| 440 | 442 | const running = |
| 441 | runtime === undefined | |
| 443 | statuses === undefined | |
| 442 | 444 | ? 0 |
| 443 | 445 | : publicApps.filter( |
| 444 | 446 | (r) => |
| 445 | 447 | r.access === "public" && |
| 446 | runtime.status(r).some((env) => env.env === "production" && env.state === "running"), | |
| 448 | statuses.get(r.id)?.some((env) => env.env === "production" && env.state === "running"), | |
| 447 | 449 | ).length; |
| 448 | 450 | const ids = [...shown.keys()]; |
| 449 | 451 | const merged = ids.reduce((sum, id) => sum + (scores.get(id)?.s.ships ?? 0), 0); |
test/front-cold-build.test.ts added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | /** | |
| 2 | * A cold build of the front page with 200 public apps reads every app's runtime in one scan, in | |
| 3 | * in a handful of statements, whatever the count (#16265). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, spyOn, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { forgetFrontScores, frontPage } from "../src/actions/front.ts"; | |
| 11 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 12 | import { expectWork } from "./helpers/work.ts"; | |
| 13 | import { once } from "./helpers/statements.ts"; | |
| 14 | import { quietLogs } from "./helpers/globals.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | ||
| 18 | const APPS = 200; | |
| 19 | let dataDir = ""; | |
| 20 | let app: App; | |
| 21 | let runtime: AppsRuntime; | |
| 22 | ||
| 23 | beforeAll(async () => { | |
| 24 | dataDir = await mkdtemp(join(tmpdir(), "forge-front-cold-")); | |
| 25 | app = createApp({ dataDir, port: 0 }); | |
| 26 | runtime = new AppsRuntime(app, { | |
| 27 | domain: "front.test", | |
| 28 | scheme: "http", | |
| 29 | publicPort: 0, | |
| 30 | forgeUrl: "http://localhost", | |
| 31 | }); | |
| 32 | const db = app.db; | |
| 33 | db.query("INSERT INTO user (id, username, email, password_hash) VALUES ('u1', 'owner', '[email protected]', 'x')").run(); | |
| 34 | db.transaction(() => { | |
| 35 | for (let n = 0; n < APPS; n++) { | |
| 36 | db.query("INSERT INTO repo (id, owner_id, name, visibility) VALUES (?1, 'u1', ?2, 'public')").run(`r${n}`, `app${n}`); | |
| 37 | db.query("INSERT INTO app (id, repo_id, slug, access) VALUES (?1, ?2, ?3, 'public')").run(`a${n}`, `r${n}`, `app${n}`); | |
| 38 | } | |
| 39 | })(); | |
| 40 | }); | |
| 41 | ||
| 42 | afterAll(async () => { | |
| 43 | await runtime.stop(); | |
| 44 | app.close(); | |
| 45 | await rm(dataDir, { recursive: true, force: true }); | |
| 46 | }); | |
| 47 | ||
| 48 | test("the cold build reads runtime status once for all apps, never per app, in statements that do not grow with the apps", async () => { | |
| 49 | const many = spyOn(AppsRuntime.prototype, "statusMany"); | |
| 50 | const one = spyOn(AppsRuntime.prototype, "status"); | |
| 51 | frontPage(app); // warm the module, then drop the kept page | |
| 52 | forgetFrontScores(app); | |
| 53 | many.mockClear(); | |
| 54 | let page!: ReturnType<typeof frontPage>; | |
| 55 | const n = await once(app.db, () => { | |
| 56 | page = frontPage(app); | |
| 57 | }); | |
| 58 | expect(page.total).toBe(APPS); | |
| 59 | expect(one).not.toHaveBeenCalled(); | |
| 60 | expect(many).toHaveBeenCalledTimes(1); | |
| 61 | many.mockRestore(); | |
| 62 | one.mockRestore(); | |
| 63 | expectWork(n, { max: 25 }); | |
| 64 | }); |
test/front-speed.test.ts +8 −2
| @@ -48,8 +48,14 @@ beforeAll(async () => { | ||
| 48 | 48 | publicPort: 0, |
| 49 | 49 | forgeUrl: "http://localhost", |
| 50 | 50 | }); |
| 51 | const status = spyOn(AppsRuntime.prototype, "status").mockImplementation( | |
| 52 | (): EnvStatus[] => [{ env: "production", state: "running", commitOid: HEAD } as EnvStatus], | |
| 51 | const status = spyOn(AppsRuntime.prototype, "statusMany").mockImplementation( | |
| 52 | (records) => | |
| 53 | new Map( | |
| 54 | records.map((r) => [ | |
| 55 | r.id, | |
| 56 | [{ env: "production", state: "running", commitOid: HEAD } as EnvStatus], | |
| 57 | ]), | |
| 58 | ), | |
| 53 | 59 | ); |
| 54 | 60 | restore = () => status.mockRestore(); |
| 55 | 61 | for (let n = 0; n < APPS; n++) { |
test/front.test.ts +14 −8
| @@ -253,14 +253,20 @@ describe("the front page", () => { | ||
| 253 | 253 | publicPort: servers.port, |
| 254 | 254 | forgeUrl: servers.url, |
| 255 | 255 | }); |
| 256 | const status = spyOn(AppsRuntime.prototype, "status").mockImplementation( | |
| 257 | (record: AppRecord): EnvStatus[] => [ | |
| 258 | { | |
| 259 | env: "production", | |
| 260 | state: states.get(record.id) ?? "stopped", | |
| 261 | commitOid: HEAD, | |
| 262 | } as EnvStatus, | |
| 263 | ], | |
| 256 | const status = spyOn(AppsRuntime.prototype, "statusMany").mockImplementation( | |
| 257 | (records: readonly AppRecord[]) => | |
| 258 | new Map( | |
| 259 | records.map((record) => [ | |
| 260 | record.id, | |
| 261 | [ | |
| 262 | { | |
| 263 | env: "production", | |
| 264 | state: states.get(record.id) ?? "stopped", | |
| 265 | commitOid: HEAD, | |
| 266 | } as EnvStatus, | |
| 267 | ], | |
| 268 | ]), | |
| 269 | ), | |
| 264 | 270 | ); |
| 265 | 271 | const one = spyOn(AppsRuntime.prototype, "envStatus").mockImplementation( |
| 266 | 272 | (record: AppRecord): EnvStatus => |
test/helpers/studio.ts +12 −9
| @@ -33,18 +33,21 @@ export async function serving( | ||
| 33 | 33 | publicPort: servers.port, |
| 34 | 34 | forgeUrl: servers.url, |
| 35 | 35 | }); |
| 36 | const status = spyOn(AppsRuntime.prototype, "status").mockImplementation( | |
| 37 | (record: AppRecord): EnvStatus[] => [ | |
| 38 | { | |
| 39 | env: "production", | |
| 40 | state: stateOf(record), | |
| 41 | // Serving the commit its newest picture is of. | |
| 42 | commitOid: looksForEnv(app.db, record.id, "production")[0]?.commitOid ?? "", | |
| 43 | } as EnvStatus, | |
| 44 | ], | |
| 36 | const envOf = (record: AppRecord): EnvStatus[] => [ | |
| 37 | { | |
| 38 | env: "production", | |
| 39 | state: stateOf(record), | |
| 40 | // Serving the commit its newest picture is of. | |
| 41 | commitOid: looksForEnv(app.db, record.id, "production")[0]?.commitOid ?? "", | |
| 42 | } as EnvStatus, | |
| 43 | ]; | |
| 44 | const status = spyOn(AppsRuntime.prototype, "status").mockImplementation(envOf); | |
| 45 | const many = spyOn(AppsRuntime.prototype, "statusMany").mockImplementation( | |
| 46 | (records: readonly AppRecord[]) => new Map(records.map((r) => [r.id, envOf(r)])), | |
| 45 | 47 | ); |
| 46 | 48 | return async () => { |
| 47 | 49 | status.mockRestore(); |
| 50 | many.mockRestore(); | |
| 48 | 51 | await runtime.stop(); |
| 49 | 52 | }; |
| 50 | 53 | } |
test/one-public-app.test.ts +8 −3
| @@ -92,9 +92,14 @@ beforeAll(async () => { | ||
| 92 | 92 | runtime = new AppsRuntime(app, { |
| 93 | 93 | domain: "offered.test", scheme: "http", publicPort: servers.port, forgeUrl: servers.url, |
| 94 | 94 | }); |
| 95 | const status = spyOn(AppsRuntime.prototype, "status").mockImplementation( | |
| 96 | (record: AppRecord): EnvStatus[] => | |
| 97 | [{ env: "production", state: states.get(record.id) ?? "stopped", commitOid: "a".repeat(40) } as EnvStatus], | |
| 95 | const status = spyOn(AppsRuntime.prototype, "statusMany").mockImplementation( | |
| 96 | (records: readonly AppRecord[]) => | |
| 97 | new Map( | |
| 98 | records.map((r) => [ | |
| 99 | r.id, | |
| 100 | [{ env: "production", state: states.get(r.id) ?? "stopped", commitOid: "a".repeat(40) } as EnvStatus], | |
| 101 | ]), | |
| 102 | ), | |
| 98 | 103 | ); |
| 99 | 104 | restore = () => status.mockRestore(); |
| 100 | 105 | }, 30_000); |
test/perf-home-signed-out.test.ts +5 −2
| @@ -53,8 +53,11 @@ beforeAll(async () => { | ||
| 53 | 53 | app = createApp({ dataDir: dir, port: 0 }); |
| 54 | 54 | servers = await serve(app); |
| 55 | 55 | runtime = new AppsRuntime(app, { domain: "home.test", scheme: "http", publicPort: 0, forgeUrl: "http://localhost" }); |
| 56 | const status = spyOn(AppsRuntime.prototype, "status").mockImplementation( | |
| 57 | (): EnvStatus[] => [{ env: "production", state: "running", commitOid: HEAD } as EnvStatus], | |
| 56 | const status = spyOn(AppsRuntime.prototype, "statusMany").mockImplementation( | |
| 57 | (records) => | |
| 58 | new Map( | |
| 59 | records.map((r) => [r.id, [{ env: "production", state: "running", commitOid: HEAD } as EnvStatus]]), | |
| 60 | ), | |
| 58 | 61 | ); |
| 59 | 62 | restore = () => status.mockRestore(); |
| 60 | 63 | alice = must( |
test/tui-front.test.ts +8 −2
| @@ -100,8 +100,14 @@ describe("the front page in the terminal", () => { | ||
| 100 | 100 | forge.db.query("UPDATE user SET is_admin = 1, is_agent = 1 WHERE username = 'carol'").run(); |
| 101 | 101 | // The door shows unpinned apps of admins only (#11902); bob is one here. |
| 102 | 102 | forge.db.query("UPDATE user SET is_admin = 1 WHERE username = 'bob'").run(); |
| 103 | status = spyOn(AppsRuntime.prototype, "status").mockImplementation( | |
| 104 | (_r: AppRecord): EnvStatus[] => [{ env: "production", state: "running", commitOid: HEAD } as EnvStatus], | |
| 103 | status = spyOn(AppsRuntime.prototype, "statusMany").mockImplementation( | |
| 104 | (records: readonly AppRecord[]) => | |
| 105 | new Map( | |
| 106 | records.map((r) => [ | |
| 107 | r.id, | |
| 108 | [{ env: "production", state: "running", commitOid: HEAD } as EnvStatus], | |
| 109 | ]), | |
| 110 | ), | |
| 105 | 111 | ); |
| 106 | 112 | runtime = new AppsRuntime(forge, { |
| 107 | 113 | domain: "front.test", |
new above
offline
Doubled word: "in in a handful of statements".