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

"Every repository you work on" is every one: the shipping and checks charts no longer drop the oldest, busiest repository #16336

Merged opened this

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

Merged

talk/scope-all → main 0 commits 2 threads · 2 unresolved

  • Merged. talk/scope-all was merged into main as 18b85cd.

Review

Sign in to review this change.
3 files changed +36 −22
src/core/repos.ts +13
@@ -1187,6 +1187,19 @@ export function listReposWritableTo(
11871187 return options.withoutCounts === true ? rows : withCounts(db, rows);
11881188 }
11891189
1190 /**
1191 * The ids of every repository this viewer may write (`REPO_WRITABLE`, as `listReposWritableTo`),
1192 * with no page and no limit: what "every repository you work on" spans for a count, where cutting
1193 * at the newest N would drop the oldest and busiest. One indexed statement; ids only.
1194 */
1195 export function everyWritableRepoId(db: Database, viewer: User | undefined): string[] {
1196 if (!viewer) return [];
1197 return db
1198 .query<{ id: string }, [string]>(`SELECT repo.id AS id FROM repo WHERE ${REPO_WRITABLE}`)
1199 .all(viewer.id)
1200 .map((r) => r.id);
1201 }
1202
11901203 /** Repos owned by one user or org, visible to the viewer. */
11911204 export function listReposByOwner(
11921205 db: Database,
src/widgets/registry.ts +12 −22
@@ -18,7 +18,7 @@ import { mergedPerDay } from "../core/pulls.ts";
1818 import { historyBrief } from "../system/telemetry.ts";
1919 import { withUnit } from "../core/talk_voice.ts";
2020 import type { Database } from "bun:sqlite";
21 import { canRead, getRepo, listReposWritableTo, type Repo } from "../core/repos.ts";
21 import { canRead, getRepo, everyWritableRepoId } from "../core/repos.ts";
2222
2323 /** The forge's renderers a widget may name. */
2424 export const RENDERERS = ["chart"] as const;
@@ -222,31 +222,23 @@ export const SCOPE_ARGS = {
222222 },
223223 } as const satisfies Record<string, Schema>;
224224
225 /** The most repositories "every repository you work on" spans. */
226 export const SCOPE_REPOS = 100;
227
228225 /**
229 * The repositories a widget reads: the one named, when the person may read it, else `not_found`,
230 * whether it is missing or hidden; or the ones they work on (their own and their teams'), at most
231 * `SCOPE_REPOS`.
226 * The repositories a widget reads, as ids: the one named, when the person may read it, else
227 * `not_found`, whether it is missing or hidden; or every one they work on (their own, their teams',
228 * the ones they crew), however many: a newest-first page would leave out the oldest repository,
229 * often the busiest (#16330 counted 19 merges for an owner whose forge merged hundreds).
232230 */
233231 export function scopeOf(
234232 app: App,
235233 user: User | undefined,
236234 args: Readonly<Record<string, unknown>>,
237 ): Result<{ repos: Repo[]; where: string }, "not_found"> {
235 ): Result<{ repoIds: string[]; where: string }, "not_found"> {
238236 const named = typeof args["repo"] === "string" ? args["repo"] : undefined;
239 if (named === undefined) {
240 const repos = listReposWritableTo(app.db, user, {
241 withoutCounts: true,
242 limit: SCOPE_REPOS,
243 });
244 return ok({ repos, where: "every repository you work on" });
245 }
237 if (named === undefined) return ok({ repoIds: everyWritableRepoId(app.db, user), where: "every repository you work on" });
246238 const [owner = "", name = ""] = named.split("/");

Long single-line if ... return ok({...}): break it across lines like the rest of the file.

247239 const repo = getRepo(app.db, owner, name);
248240 return repo !== undefined && canRead(app.db, repo, user)
249 ? ok({ repos: [repo], where: `${repo.ownerName}/${repo.name}` })
241 ? ok({ repoIds: [repo.id], where: `${repo.ownerName}/${repo.name}` })
250242 : err("not_found");
251243 }
252244
@@ -287,20 +279,18 @@ export function perDay<S extends string>(days: readonly string[], series: readon
287279 return out;
288280 }
289281
290 /** The repositories as the one JSON list every read binds. */
291 const idsOf = (repos: readonly Repo[]): string => JSON.stringify(repos.map((r) => r.id));
292282

Two blank lines are left here after removing idsOf; keep one.

293283 /**
294284 * Each check finished on these repositories since an instant: when, and how it ended. One covering
295285 * walk of `run_finished_cov`.
296286 */
297 export const checksSince = (db: Database, repos: readonly Repo[], since: string): { at: string; state: string }[] =>
287 export const checksSince = (db: Database, repoIds: readonly string[], since: string): { at: string; state: string }[] =>
298288 db
299289 .query<{ at: string; state: string }, [string, string]>(
300290 `SELECT finished_at AS at, state FROM run INDEXED BY run_finished_cov
301291 WHERE kind = 'check' AND finished_at IS NOT NULL AND finished_at >= ?1 AND repo_id IN (SELECT value FROM json_each(?2))`,
302292 )
303 .all(since, idsOf(repos));
293 .all(since, JSON.stringify(repoIds));
304294
305295 /** A number rounded for a sentence: whole above ten, one place below. */
306296 export const said = (v: number): string => (Math.abs(v) >= 10 ? Math.round(v) : Math.round(v * 10) / 10).toLocaleString("en-US");
@@ -316,7 +306,7 @@ const checks: Widget = {
316306 const scope = scopeOf(app, user, args);
317307 if (!scope.ok) return scope;
318308 const { days, since } = windowOf(args);
319 const by = perDay(days, ["ok", "failed"] as const, checksSince(app.db, scope.value.repos, since).map((r) => ({ at: r.at, series: r.state === "ok" ? "ok" : r.state === "failed" ? "failed" : undefined })));
309 const by = perDay(days, ["ok", "failed"] as const, checksSince(app.db, scope.value.repoIds, since).map((r) => ({ at: r.at, series: r.state === "ok" ? "ok" : r.state === "failed" ? "failed" : undefined })));
320310 const passed = by.ok.reduce((a, b) => a + b, 0);
321311 const failed = by.failed.reduce((a, b) => a + b, 0);
322312 const all = passed + failed;
@@ -399,7 +389,7 @@ const shipping: Widget = {
399389 const scope = scopeOf(app, user, args);
400390 if (!scope.ok) return scope;
401391 const { days } = windowOf(args);
402 const { merged } = mergedPerDay(app.db, scope.value.repos.map((r) => r.id), days.length);
392 const { merged } = mergedPerDay(app.db, scope.value.repoIds, days.length);
403393 const all = merged.reduce((a, b) => a + b, 0);
404394 const best = merged.indexOf(Math.max(...merged));
405395 return ok({
test/widgets.test.ts +11
@@ -191,6 +191,17 @@ describe("shipping", () => {
191191 expect(chart("shipping", alice, { days: 7 }).data.series[0]!.values.reduce((a, b) => a + b, 0)).toBe(4);
192192 expect(chart("shipping", bob, { days: 7 }).says).toBe("No pulls merged in the last 7 days on every repository you work on.");
193193 });
194
195 test("every repository the person works on is every one, not the newest hundred: the oldest, busiest repository still counts", () => {
196 // 120 newer repositories of alice's beside `open`, her oldest with the merges and checks: a
197 // newest-first page of 100 left `open` out and counted none of its work (#16330 in production).
198 const cols = app.db.query<{ name: string }, []>("SELECT name FROM pragma_table_info('repo')").all().map((c) => c.name);
199 const copy = app.db.query(`INSERT INTO repo (${cols.join(", ")}) SELECT ${cols.map((c) => (c === "id" ? "?1" : c === "name" ? "?2" : c)).join(", ")} FROM repo WHERE id = ?3`);
200 for (let n = 0; n < 120; n++) copy.run(id(), `newer-${n}`, open.id);
201 const all = (name: string) => chart(name, alice, { days: 7 }).data.series.map((s) => s.values.reduce((a, b) => a + b, 0));
202 expect(all("shipping")).toEqual([4]);
203 expect(all("checks")).toEqual([5, 3]);
204 });
194205 });
195206
196207 describe("the words that lead to a graph", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs