// PEP-179: the «Погоня за задачей» picture above the team list — who of the team is at work // right now (on the track, with their story), who is free (on the bench) and how many stories // the team has brought to «Готово» in the projects the viewer sees. Same live-runner rule as // `listAgents` (a session not done/failed), so the picture and the cards below always agree. import { agents, projects, sessions, tasks, type Db } from "@peppa/db"; import { formatTaskKey } from "@peppa/shared"; import { and, asc, desc, eq, inArray, isNull, notInArray, sql } from "drizzle-orm"; import { isSuperadmin } from "./monitor.ts"; import { getSetting, setSetting } from "./settings.ts"; import { userProjectIds, type User } from "./users.ts"; /** At most this many agents run on the track; the rest is «+N». */ export const CHASE_TRACK_MAX = 8; /** The agent's colour — the same tone index as the web `AgentBadge` (sum of code points). */ const AGENT_COLORS = ["#7dd3fc", "#6ee7b7", "#fcd34d", "#93c5fd", "#a5b4fc", "#c4b5fd", "#5eead4", "#fdba74"]; export const agentColor = (name: string) => AGENT_COLORS[[...name].reduce((a, c) => a + c.codePointAt(0)!, 0) % AGENT_COLORS.length]; export interface ChaseAgent { id: number; name: string; color: string; /** The story of its live runner — only when the viewer can open that project. */ task: { key: string; title: string } | null; } export interface TeamChaseView { /** On the track, oldest agent first (≤ CHASE_TRACK_MAX). */ runners: ChaseAgent[]; /** Working agents that did not fit on the track. */ extra: number; /** Free agents of the team (removed ones are not on the bench). */ bench: ChaseAgent[]; /** Stories in «Готово» in the projects the viewer sees (a superadmin — all of them). */ accepted: number; /** When the data was read (ISO) — «Нет связи — данные на ЧЧ:ММ». */ at: string; } export async function teamChaseView(db: Db, user: Pick, now = new Date()): Promise { const admin = await isSuperadmin(db, user); const visible = admin ? null : new Set(await userProjectIds(db, user.id)); const live = notInArray(sessions.status, ["done", "failed"]); const team = await db.select({ id: agents.id, name: agents.name, disabledAt: agents.disabledAt }).from(agents).orderBy(asc(agents.id)); const runs = await db .select({ agentId: sessions.agentId, projectId: tasks.projectId, number: tasks.number, title: tasks.title, key: projects.key }) .from(sessions) .leftJoin(tasks, eq(tasks.id, sessions.taskId)) .leftJoin(projects, eq(projects.id, tasks.projectId)) .where(and(live, sql`${sessions.agentId} is not null`)) .orderBy(desc(sessions.id)); // The newest live runner of each agent tells which story it chases. const current = new Map(); for (const r of runs) if (r.agentId != null && !current.has(r.agentId)) current.set(r.agentId, r); const working: ChaseAgent[] = []; const bench: ChaseAgent[] = []; for (const a of team) { const run = current.get(a.id); const base = { id: a.id, name: a.name, color: agentColor(a.name) }; if (run) { const seen = run.key != null && run.number != null && run.projectId != null && (visible == null || visible.has(run.projectId)); working.push({ ...base, task: seen ? { key: formatTaskKey(run.key!, run.number!), title: run.title ?? "" } : null }); } else if (!a.disabledAt) bench.push({ ...base, task: null }); } const ids = visible == null ? null : [...visible]; let accepted = 0; if (ids == null || ids.length) { const [row] = await db .select({ n: sql`count(*)` }) .from(tasks) .innerJoin(projects, eq(projects.id, tasks.projectId)) .where(and(eq(tasks.status, "done"), isNull(projects.deletedAt), ids == null ? undefined : inArray(tasks.projectId, ids))); accepted = Number(row?.n ?? 0); } return { runners: working.slice(0, CHASE_TRACK_MAX), extra: Math.max(0, working.length - CHASE_TRACK_MAX), bench, accepted, at: now.toISOString() }; } const hiddenKey = (userId: number) => `ui.team-chase:${userId}`; /** «Скрыть» on the picture — remembered for the user. */ export async function teamChaseHidden(db: Db, userId: number): Promise { return (await getSetting(db, hiddenKey(userId))) === "hidden"; } export async function setTeamChaseHidden(db: Db, userId: number, hidden: boolean): Promise { await setSetting(db, hiddenKey(userId), hidden ? "hidden" : "shown"); }