peppa/packages/core/src/progress.ts
peppa-bot 0097cb07f7 PEP-182: не больше 9 агентов на подписку — ёмкость, очередь задач, «Делается · В очереди»
Ёмкость подписки (core/capacity.ts, по умолчанию 9, суперадмин меняет на набор 1–20);
pickSubscription, ротация и ручной перевод агентов уважают ёмкость; без места задача
ждёт в очереди и запускается сама по порядку доски (capacityQueueTick); команда растёт
только в пределах ёмкости; экраны «Подписки», Главная, доска, страница задачи; ru/kk/en;
тест subscription-capacity.test.ts; OpenSpec change archived.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 02:55:40 +05:00

101 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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, workLine, 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().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]));
out.work = await workLine(db, 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: await workLine(db, [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;
}