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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/6273-the-front-page-s-build-shows-its-stretch → main 0 commits fork 1 thread · 1 unresolved
3 files
changed +64
−38
src/actions/front.ts +52 −37
| @@ -52,7 +52,6 @@ import { whyNoPicture } from "../looks/why.ts"; | ||
| 52 | 52 | import { canReadMany, getRepo, getReposByIds, type Repo } from "../core/repos.ts"; |
| 53 | 53 | import { listActiveRuns } from "../core/runs.ts"; |
| 54 | 54 | import { getUsersByIds, isActive, type User } from "../core/users.ts"; |
| 55 | import { timePhase } from "../core/route_timing.ts"; | |
| 56 | 55 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 57 | 56 | import { limitValue } from "../settings/limits.ts"; |
| 58 | 57 | |
| @@ -285,36 +284,46 @@ export function doorRepos(app: App): Repo[] { | ||
| 285 | 284 | .filter((r) => !seen.has(r.id) && seen.add(r.id)); |
| 286 | 285 | } |
| 287 | 286 | |
| 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)); | |
| 294 | 297 | const kept = built.get(app); |
| 295 | 298 | const wall = performance.now(); |
| 296 | 299 | if (kept !== undefined && kept.key === key && wall - kept.at < PAGE_MS) |
| 297 | 300 | return kept.page; |
| 298 | const page = buildFrontPage(app, now); | |
| 301 | const page = buildFrontPage(app, now, lap); | |
| 299 | 302 | built.set(app, { key, at: wall, page }); |
| 300 | 303 | return page; |
| 301 | 304 | } |
| 302 | 305 | |
| 303 | function buildFrontPage(app: App, now: number): FrontPage { | |
| 306 | function buildFrontPage(app: App, now: number, lap: Lap): FrontPage { | |
| 304 | 307 | 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 | })); | |
| 312 | 317 | // A stranger's reading, asked of every repository, pinned or not. |
| 313 | 318 | // 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 | ); | |
| 318 | 327 | const readable = (repo: Repo | undefined): repo is Repo => |
| 319 | 328 | repo !== undefined && |
| 320 | 329 | strangerReads.has(repo.id) && |
| @@ -336,15 +345,17 @@ function buildFrontPage(app: App, now: number): FrontPage { | ||
| 336 | 345 | ...publicApps.map((r) => r.repoId), |
| 337 | 346 | ]), |
| 338 | 347 | ]; |
| 339 | const scores = scoresFor(app, ids, now); | |
| 348 | const scores = lap("front-scores", () => scoresFor(app, ids, now)); | |
| 340 | 349 | 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 | ), | |
| 344 | 355 | ); |
| 345 | 356 | const runtime = appsRuntime(app); |
| 346 | 357 | // 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()])); | |
| 348 | 359 | const imported = importedSources(db, ids); |
| 349 | 360 | const entry = (repo: Repo, isPinned: boolean): FrontEntry => { |
| 350 | 361 | const record = appOf.get(repo.id); |
| @@ -416,18 +427,20 @@ function buildFrontPage(app: App, now: number): FrontPage { | ||
| 416 | 427 | // one, waking a pinned app to do it) — never a letter in a box. |
| 417 | 428 | const tileable = (e: FrontEntry): boolean => |
| 418 | 429 | e.state !== undefined && SHOWN.has(e.state) && e.picture !== undefined; |
| 419 | const pinnedEntries = pinned.map((repo) => entry(repo, true)); | |
| 420 | 430 | 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 | })); | |
| 431 | 444 | thumbs.done(); |
| 432 | 445 | // Featured is a card, not a tile: the first pin may be a repository with |
| 433 | 446 | // no app, or one whose app strangers cannot open (the forge itself) — |
| @@ -452,7 +465,9 @@ function buildFrontPage(app: App, now: number): FrontPage { | ||
| 452 | 465 | : { ...featuredEntry, crew: crewAtWork(app, featuredEntry.repo) }, |
| 453 | 466 | shelf, |
| 454 | 467 | 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 | ), | |
| 456 | 471 | }; |
| 457 | 472 | } |
| 458 | 473 |
src/web/studio/home.ts +1 −1
| @@ -733,7 +733,7 @@ export function studioDoor(ctx: Ctx): Response { | ||
| 733 | 733 | // The door draws what is in memory; a pack pushed since is picked up for the next visit. |
| 734 | 734 | void refreshCatalogQuietly(ctx.app, 5_000); |
| 735 | 735 | 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))); | |
| 737 | 737 | const featured = front.featured; |
| 738 | 738 | const all = [...(featured === undefined ? [] : [featured]), ...front.shelf]; |
| 739 | 739 | 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 () => { | ||
| 72 | 72 | expect(got).toContain("tiles"); |
| 73 | 73 | expect(new Set(got).size).toBe(got.length); |
| 74 | 74 | }); |
| 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 | }); |
new above
offline
This line runs far past 100 columns. Name the lap callback in a const above, or wrap the arguments.