Public: anyone can read this; the crew can change it.

The cold front page reads every app's runtime status in one scan #16795

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16265-the-cold-front-page-reads-every-app-s-ru → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/16265-the-cold-front-page-reads-every-app-s-ru was merged into main as 1613ec0.

Review

Sign in to review this change.
8 files changed +128 −33
src/actions/front.ts +9 −7
@@ -20,7 +20,7 @@
2020 import type { Database } from "bun:sqlite";
2121 import { provenanceUrl, sourceLabel } from "../core/provenance.ts";
2222 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";
2424 import { getAppByRepo, listPublicApps, type AppRecord } from "../core/apps.ts";
2525 import {
2626 frontPicks,
@@ -339,13 +339,15 @@ function buildFrontPage(app: App, now: number): FrontPage {
339339 [...appOf.values()].map((r) => r.id),
340340 );
341341 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()]);
342344 const imported = importedSources(db, ids);
343345 const entry = (repo: Repo, isPinned: boolean): FrontEntry => {
344346 const record = appOf.get(repo.id);
345347 const production =
346 record === undefined || runtime === undefined
348 record === undefined || statuses === undefined
347349 ? undefined
348 : runtime.status(record).find((env) => env.env === "production");
350 : statuses.get(record.id)?.find((env) => env.env === "production");
349351 // An app open only to signed-in readers shows a stranger a sign-in
350352 // page: there is no picture of it worth showing them.
351353 const thumb =
@@ -425,7 +427,7 @@ function buildFrontPage(app: App, now: number): FrontPage {
425427 : { ...featuredEntry, crew: crewAtWork(app, featuredEntry.repo) },
426428 shelf,
427429 total: publicApps.length,
428 pulse: buildPulse(app, shownRepos, publicApps, scores, now),
430 pulse: buildPulse(app, shownRepos, publicApps, scores, now, statuses),
429431 };
430432 }
431433
@@ -435,15 +437,15 @@ function buildPulse(
435437 publicApps: readonly AppRecord[],
436438 scores: Scores,
437439 now: number,
440 statuses: ReadonlyMap<string, readonly EnvStatus[]> | undefined,
438441 ): FrontPulse {
439 const runtime = appsRuntime(app);
440442 const running =
441 runtime === undefined
443 statuses === undefined
442444 ? 0
443445 : publicApps.filter(
444446 (r) =>
445447 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"),
447449 ).length;
448450 const ids = [...shown.keys()];
449451 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).

Doubled word: "in in a handful of statements".

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 () => {
4848 publicPort: 0,
4949 forgeUrl: "http://localhost",
5050 });
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 ),
5359 );
5460 restore = () => status.mockRestore();
5561 for (let n = 0; n < APPS; n++) {
test/front.test.ts +14 −8
@@ -253,14 +253,20 @@ describe("the front page", () => {
253253 publicPort: servers.port,
254254 forgeUrl: servers.url,
255255 });
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 ),
264270 );
265271 const one = spyOn(AppsRuntime.prototype, "envStatus").mockImplementation(
266272 (record: AppRecord): EnvStatus =>
test/helpers/studio.ts +12 −9
@@ -33,18 +33,21 @@ export async function serving(
3333 publicPort: servers.port,
3434 forgeUrl: servers.url,
3535 });
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)])),
4547 );
4648 return async () => {
4749 status.mockRestore();
50 many.mockRestore();
4851 await runtime.stop();
4952 };
5053 }
test/one-public-app.test.ts +8 −3
@@ -92,9 +92,14 @@ beforeAll(async () => {
9292 runtime = new AppsRuntime(app, {
9393 domain: "offered.test", scheme: "http", publicPort: servers.port, forgeUrl: servers.url,
9494 });
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 ),
98103 );
99104 restore = () => status.mockRestore();
100105 }, 30_000);
test/perf-home-signed-out.test.ts +5 −2
@@ -53,8 +53,11 @@ beforeAll(async () => {
5353 app = createApp({ dataDir: dir, port: 0 });
5454 servers = await serve(app);
5555 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 ),
5861 );
5962 restore = () => status.mockRestore();
6063 alice = must(
test/tui-front.test.ts +8 −2
@@ -100,8 +100,14 @@ describe("the front page in the terminal", () => {
100100 forge.db.query("UPDATE user SET is_admin = 1, is_agent = 1 WHERE username = 'carol'").run();
101101 // The door shows unpinned apps of admins only (#11902); bob is one here.
102102 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 ),
105111 );
106112 runtime = new AppsRuntime(forge, {
107113 domain: "front.test",

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs