peppa/packages/core/src/progress.ts
2026-11-16 21:25:21 +05:00

92 lines
4.3 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 { 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;
}
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-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 };
}
/** 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 } };
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().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;
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, 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 });
}
}
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;
}