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

The front page's build shows its stretches in Server-Timing on a cache miss #16956

Merged opened this

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

Merged

scout/forge:scout/6273-the-front-page-s-build-shows-its-stretch → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/6273-the-front-page-s-build-shows-its-stretch was merged into main as 190ec24.

Review

Sign in to review this change.
3 files changed +64 −38
src/actions/front.ts +52 −37
@@ -52,7 +52,6 @@ import { whyNoPicture } from "../looks/why.ts";
5252 import { canReadMany, getRepo, getReposByIds, type Repo } from "../core/repos.ts";
5353 import { listActiveRuns } from "../core/runs.ts";
5454 import { getUsersByIds, isActive, type User } from "../core/users.ts";
55 import { timePhase } from "../core/route_timing.ts";
5655 import { err, ok, type Result } from "../kernel/result.ts";
5756 import { limitValue } from "../settings/limits.ts";
5857
@@ -285,36 +284,46 @@ export function doorRepos(app: App): Repo[] {
285284 .filter((r) => !seen.has(r.id) && seen.add(r.id));
286285 }
287286
288 /** The front page as a stranger is shown it; `request` is told what the key cost. */
289 export function frontPage(app: App, now = Date.now(), request?: Request): FrontPage {
290 const key =
291 request === undefined
292 ? frontKey(app)
293 : timePhase(request, "key", () => frontKey(app));
287 /** Names a stretch of work and runs it; the caller decides where the time goes. */
288 export type Lap = <T>(name: string, run: () => T) => T;
289 const noLap: Lap = (_name, run) => run();
290
291 /**
292 * The front page as a stranger is shown it; `lap` is told what the key cost
293 * and, on a cache miss only, each stretch of the build (`front-*`).
294 */
295 export function frontPage(app: App, now = Date.now(), lap: Lap = noLap): FrontPage {
296 const key = lap("key", () => frontKey(app));
294297 const kept = built.get(app);
295298 const wall = performance.now();
296299 if (kept !== undefined && kept.key === key && wall - kept.at < PAGE_MS)
297300 return kept.page;
298 const page = buildFrontPage(app, now);
301 const page = buildFrontPage(app, now, lap);
299302 built.set(app, { key, at: wall, page });
300303 return page;
301304 }
302305
303 function buildFrontPage(app: App, now: number): FrontPage {
306 function buildFrontPage(app: App, now: number, lap: Lap): FrontPage {
304307 const db = app.db;
305 const picks = frontPicks(db);
306 const records = listPublicApps(db, READ);
307 const pinnedRepos = getReposByIds(db, picks.pinned);
308 const repos = getReposByIds(
309 db,
310 records.map((r) => r.repoId),
311 );
308 const picks = lap("front-picks", () => frontPicks(db));
309 const records = lap("front-records", () => listPublicApps(db, READ));
310 const { pinnedRepos, repos } = lap("front-repos", () => ({
311 pinnedRepos: getReposByIds(db, picks.pinned),
312 repos: getReposByIds(
313 db,
314 records.map((r) => r.repoId),
315 ),
316 }));
312317 // A stranger's reading, asked of every repository, pinned or not.
313318 // A suspended or removed account's repositories are offered to nobody.
314 const owners = getUsersByIds(db, [
315 ...new Set([...pinnedRepos.values(), ...repos.values()].map((r) => r.ownerId)),
316 ]);
317 const strangerReads = canReadMany(db, [...pinnedRepos.values(), ...repos.values()], undefined);
319 const owners = lap("front-owners", () =>
320 getUsersByIds(db, [
321 ...new Set([...pinnedRepos.values(), ...repos.values()].map((r) => r.ownerId)),
322 ]),
323 );
324 const strangerReads = lap("front-reads", () =>
325 canReadMany(db, [...pinnedRepos.values(), ...repos.values()], undefined),
326 );
318327 const readable = (repo: Repo | undefined): repo is Repo =>
319328 repo !== undefined &&
320329 strangerReads.has(repo.id) &&
@@ -336,15 +345,17 @@ function buildFrontPage(app: App, now: number): FrontPage {
336345 ...publicApps.map((r) => r.repoId),
337346 ]),
338347 ];
339 const scores = scoresFor(app, ids, now);
348 const scores = lap("front-scores", () => scoresFor(app, ids, now));
340349 const thumbs = strangersThumbs(app);
341 const looks = looksForApps(
342 db,
343 [...appOf.values()].map((r) => r.id),
350 const looks = lap("front-looks", () =>
351 looksForApps(
352 db,
353 [...appOf.values()].map((r) => r.id),
354 ),
344355 );
345356 const runtime = appsRuntime(app);
346357 // Every app's environments in one pass; the page and its pulse share it.
347 const statuses = runtime?.statusMany([...appOf.values()]);
358 const statuses = lap("front-runs", () => runtime?.statusMany([...appOf.values()]));
348359 const imported = importedSources(db, ids);
349360 const entry = (repo: Repo, isPinned: boolean): FrontEntry => {
350361 const record = appOf.get(repo.id);
@@ -416,18 +427,20 @@ function buildFrontPage(app: App, now: number): FrontPage {
416427 // one, waking a pinned app to do it) — never a letter in a box.
417428 const tileable = (e: FrontEntry): boolean =>
418429 e.state !== undefined && SHOWN.has(e.state) && e.picture !== undefined;
419 const pinnedEntries = pinned.map((repo) => entry(repo, true));
420430 const pinnedIds = new Set(pinned.map((r) => r.id));
421 const ranked = rankFront(
422 publicApps
423 .filter((r) => !pinnedIds.has(r.repoId) && !picks.hidden.has(r.repoId))
424 // The door shows the forge's own makers; a stranger's app is found on
425 // /explore or pinned by an admin. Filtered here, before any ranking is kept.
426 .filter((r) => owners.get(repos.get(r.repoId)!.ownerId)?.isAdmin === true)
427 .map((r) => entry(repos.get(r.repoId)!, false))
428 .filter(tileable)
429 .map((e) => ({ ...e, id: e.repo.id, owner: e.repo.ownerName })),
430 );
431 const { pinnedEntries, ranked } = lap("front-rows", () => ({
432 pinnedEntries: pinned.map((repo) => entry(repo, true)),
433 ranked: rankFront(
434 publicApps
435 .filter((r) => !pinnedIds.has(r.repoId) && !picks.hidden.has(r.repoId))
436 // The door shows the forge's own makers; a stranger's app is found on
437 // /explore or pinned by an admin. Filtered here, before any ranking is kept.
438 .filter((r) => owners.get(repos.get(r.repoId)!.ownerId)?.isAdmin === true)
439 .map((e) => entry(repos.get(e.repoId)!, false))
440 .filter(tileable)
441 .map((e) => ({ ...e, id: e.repo.id, owner: e.repo.ownerName })),
442 ),
443 }));
431444 thumbs.done();
432445 // Featured is a card, not a tile: the first pin may be a repository with
433446 // no app, or one whose app strangers cannot open (the forge itself) —
@@ -452,7 +465,9 @@ function buildFrontPage(app: App, now: number): FrontPage {
452465 : { ...featuredEntry, crew: crewAtWork(app, featuredEntry.repo) },
453466 shelf,
454467 total: publicApps.length,
455 pulse: buildPulse(app, shownRepos, publicApps, scores, now, statuses),
468 pulse: lap("front-pulse", () =>
469 buildPulse(app, shownRepos, publicApps, scores, now, statuses),
470 ),
456471 };
457472 }
458473
src/web/studio/home.ts +1 −1
@@ -733,7 +733,7 @@ export function studioDoor(ctx: Ctx): Response {
733733 // The door draws what is in memory; a pack pushed since is picked up for the next visit.
734734 void refreshCatalogQuietly(ctx.app, 5_000);
735735 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
736 const front = timePhase(ctx.request, "shelf", () => frontPage(ctx.app, Date.now(), ctx.request));
736 const front = timePhase(ctx.request, "shelf", () => frontPage(ctx.app, Date.now(), (name, run) => timePhase(ctx.request, name, run)));

This line runs far past 100 columns. Name the lap callback in a const above, or wrap the arguments.

737737 const featured = front.featured;
738738 const all = [...(featured === undefined ? [] : [featured]), ...front.shelf];
739739 const made = all.slice(0, Math.max(DOOR_TILES, all.filter((e) => e.pinned).length));
test/shelf-laps.test.ts +11
@@ -72,3 +72,14 @@ test("GET / names the front shelf's read and its tiles once each", async () => {
7272 expect(got).toContain("tiles");
7373 expect(new Set(got).size).toBe(got.length);
7474 });
75
76 // A miss names the stretches of `buildFrontPage`; a hit within the minute
77 // builds nothing and names none (#6273).
78 test("GET / names the front build's stretches on a miss, none on a hit", async () => {
79 app.db.run("UPDATE front_epoch SET n = n + 1");
80 const miss = await names("/");
81 for (const lap of ["picks", "records", "repos", "owners", "rows"])
82 expect(miss.filter((n) => n === `front-${lap}`)).toHaveLength(1);
83 const hit = await names("/");
84 expect(hit.filter((n) => n.startsWith("front-"))).toEqual([]);
85 });

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

Keyboard shortcuts

Docs