Главная 80 → 30 мс, 40 запросов разом p95 2,98 → 1,12 с; «Подписки» 36 → 22 мс. Замер на копии живой базы (apps/web/scripts/bench-pages.ts). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
102 lines
5.1 KiB
TypeScript
102 lines
5.1 KiB
TypeScript
// PEP-77: progress of every project on the Home page — stories per board column and what is
|
||
// in work right now (agent, tasks.md progress).
|
||
import { m as tr } from "@peppa/shared/i18n";
|
||
import { productUrls } from "./product.ts";
|
||
import { openProductLinkTask } from "./product-link.ts";
|
||
import { projects, tasks, users, type Db } from "@peppa/db";
|
||
import { formatTaskKey } from "@peppa/shared";
|
||
import { and, asc, eq, inArray, isNull } from "drizzle-orm";
|
||
import { taskProgress } from "./openspec.ts";
|
||
import { taskAgentNames } from "./team.ts";
|
||
import { capacityQueue, workLineIndex, type WorkLine } from "./capacity.ts";
|
||
import { pausedProjectIds } from "./project-pause.ts";
|
||
import { listOtherProjects, userProjectIds } from "./users.ts";
|
||
|
||
export interface StoryInWork {
|
||
key: string;
|
||
title: string;
|
||
status: string;
|
||
agent: string | null;
|
||
/** tasks.md of the story's change: done/total and the current item. */
|
||
done: number | null;
|
||
total: number | null;
|
||
current: string | null;
|
||
/** PEP-182: place in the queue for a free agent (no agent yet). */
|
||
queued: number | null;
|
||
}
|
||
|
||
export interface ProjectProgress {
|
||
key: string;
|
||
name: string;
|
||
/** Set for a project of somebody else (a superadmin's view). */
|
||
ownerLogin: string | null;
|
||
backlog: number;
|
||
inProgress: number;
|
||
review: number;
|
||
done: number;
|
||
total: number;
|
||
working: StoryInWork[];
|
||
/** Stories waiting for the PM's «Принять». */
|
||
reviewKeys: string[];
|
||
/** PEP-111: where the product runs (prod, else the version to check), if it is deployed. */
|
||
product: { url: string; live: boolean } | null;
|
||
/** PEP-145: no link yet — the key of the system story that makes the product open by a link. */
|
||
productFix: string | null;
|
||
/** PEP-182: «Делается · В очереди» of the project. */
|
||
work: WorkLine;
|
||
/** PEP-174: the project is on pause. */
|
||
paused: boolean;
|
||
}
|
||
|
||
export interface ProgressOverview {
|
||
mine: ProjectProgress[];
|
||
/** Other people's projects — for a superadmin only. */
|
||
others: ProjectProgress[];
|
||
totals: { backlog: number; inProgress: number; review: number; done: number; total: number };
|
||
/** PEP-182: «Делается · В очереди» over the user's own projects. */
|
||
work: WorkLine;
|
||
}
|
||
|
||
/** Board column of a story status (the same split as the board). */
|
||
const column = (status: string) => (status === "backlog" ? "backlog" : status === "review" ? "review" : status === "done" ? "done" : "inProgress");
|
||
|
||
export async function progressOverview(db: Db, userId: number): Promise<ProgressOverview> {
|
||
const mineIds = await userProjectIds(db, userId);
|
||
const otherRows = await listOtherProjects(db, userId);
|
||
const ids = [...mineIds, ...otherRows.map((p) => p.id)];
|
||
const products = await productUrls(db, ids);
|
||
const empty: ProgressOverview = { mine: [], others: [], totals: { backlog: 0, inProgress: 0, review: 0, done: 0, total: 0 }, work: { doing: 0, queued: 0 } };
|
||
if (!ids.length) return empty;
|
||
const ps = await db.select({ p: projects, ownerLogin: users.login }).from(projects).leftJoin(users, eq(users.id, projects.ownerId)).where(inArray(projects.id, ids)).orderBy(asc(projects.key));
|
||
const ts = await db.select({ id: tasks.id, projectId: tasks.projectId, number: tasks.number, title: tasks.title, status: tasks.status, openspecChange: tasks.openspecChange }).from(tasks).where(and(inArray(tasks.projectId, ids), isNull(tasks.archivedAt))).orderBy(asc(tasks.position), asc(tasks.number));
|
||
const mine = new Set(mineIds);
|
||
const paused = await pausedProjectIds(db);
|
||
const out = empty;
|
||
const queue = new Map((await capacityQueue(db, { projectIds: ids })).map((q) => [q.taskId, q.position]));
|
||
const lineOf = await workLineIndex(db, ids);
|
||
out.work = lineOf(mineIds);
|
||
for (const { p, ownerLogin } of ps) {
|
||
const row: ProjectProgress = { key: p.key, name: p.name, ownerLogin: mine.has(p.id) ? null : (ownerLogin ?? tr("без владельца")), backlog: 0, inProgress: 0, review: 0, done: 0, total: 0, working: [], reviewKeys: [], product: products.get(p.id) ?? null, productFix: null, work: lineOf([p.id]), paused: paused.has(p.id) };
|
||
const fix = row.product ? null : await openProductLinkTask(db, p.id);
|
||
if (fix) row.productFix = formatTaskKey(p.key, fix.number);
|
||
const own = ts.filter((t) => t.projectId === p.id);
|
||
const agentsOf = own.some((t) => column(t.status) === "inProgress") ? await taskAgentNames(db, p.id) : new Map<number, string>();
|
||
for (const t of own) {
|
||
const c = column(t.status);
|
||
row[c]++;
|
||
row.total++;
|
||
const key = formatTaskKey(p.key, t.number);
|
||
if (c === "review") row.reviewKeys.push(key);
|
||
if (c === "inProgress") {
|
||
const pr = taskProgress(p, t.openspecChange);
|
||
row.working.push({ key, title: t.title, status: t.status, agent: agentsOf.get(t.id) ?? null, done: pr?.done ?? null, total: pr?.total ?? null, current: pr?.current ?? null, queued: queue.get(t.id) ?? null });
|
||
}
|
||
}
|
||
if (mine.has(p.id)) {
|
||
out.mine.push(row);
|
||
for (const k of ["backlog", "inProgress", "review", "done", "total"] as const) out.totals[k] += row[k];
|
||
} else out.others.push(row);
|
||
}
|
||
return out;
|
||
}
|