/** * "My work": what one person has taken on, in one place — the alerts handed to them, the acknowledgements about to run out, the maintenance windows they * started that are about to end, and the update runs they started that are still going (or just finished). Everything here already exists elsewhere in the app; * this only gathers the parts that have this person's name on them, and says which of them need a look soon. * * Who did what is recorded as a label — the person's name, or their email, or their sign-in id — so those three are what are matched. */ import { and, eq, gte, inArray } from "drizzle-orm"; import { db } from "../db/client.js"; import { servers, updateRuns } from "../db/schema.js"; import { getAlerts } from "./alerts.js"; import { loadAcks } from "./alertAcks.js"; import { describeTarget, listActiveWindows } from "./maintenance.js"; import { openTodosFor } from "./todos.js"; /** An acknowledgement that runs out within this long is flagged. */ export const ACK_ENDS_SOON_MS = 24 * 3600_000; /** A maintenance window that ends within this long is flagged. */ export const WINDOW_ENDS_SOON_MS = 2 * 3600_000; /** A job handed to somebody is flagged from this many days before it is due (and once it is past). */ export const TODO_DUE_SOON_DAYS = 2; /** A finished update run stays on the list this long, so the result is seen. */ export const RUN_KEPT_MS = 24 * 3600_000; export interface MyWorkUser { id: number; name: string | null; email: string | null; oidcSub: string; } export interface MyWork { /** Alerts that are on the Alerts page now and that this person has acknowledged or been handed, worst-first-ending first. */ alerts: { alertId: string; message: string; severity: string; source: string; link: string | null; note: string; /** "handed" = somebody gave it to them (or they gave it to themselves); "acked" = they acknowledged it for nobody in particular. */ role: "handed" | "acked"; ackedBy: string; ackedAt: string; until: string | null; endsSoon: boolean; }[]; windows: { id: number; targetType: string; targetId: number; targetName: string; targetKind: string; reason: string | null; startedAt: string; endsAt: string; endsSoon: boolean }[]; updateRuns: { id: number; serverId: number; serverName: string; templateName: string; status: string; message: string | null; startedAt: string; finishedAt: string | null; running: boolean; updatesBefore: number | null; updatesAfter: number | null; rebootRequired: boolean }[]; /** Open jobs from the to-do list handed to this person, the soonest due first. */ todos: { id: number; title: string; detail: string | null; dueDate: string | null; overdue: boolean; dueSoon: boolean; target: { type: string; id: number; name: string } | null }[]; /** How many of the above want a look: acknowledgements and windows that end soon, alerts handed to them, update runs still going. */ needsAttention: number; } /** The labels an action by this person may have been recorded under. */ export function labelsOf(user: MyWorkUser): string[] { return [...new Set([user.name, user.email, user.oidcSub].filter((v): v is string => typeof v === "string" && v !== ""))]; } export async function buildMyWork(user: MyWorkUser, now: Date = new Date()): Promise { const labels = new Set(labelsOf(user)); const nowMs = now.getTime(); const report = await getAlerts(false); const acks = await loadAcks(now); const alerts: MyWork["alerts"] = []; for (const a of report.alerts) { const ack = acks.get(a.id); if (!ack || !ack.active) continue; const handed = ack.assignee?.id === user.id; const acked = labels.has(ack.ackedBy); if (!handed && !acked) continue; alerts.push({ alertId: a.id, message: a.message, severity: a.severity, source: a.source, link: a.link, note: ack.note, role: handed ? "handed" : "acked", ackedBy: ack.ackedBy, ackedAt: ack.ackedAt, until: ack.until, endsSoon: ack.until !== null && Date.parse(ack.until) - nowMs <= ACK_ENDS_SOON_MS, }); } // What runs out first, first; then what has no end, worst first. const SEV: Record = { critical: 0, warning: 1, info: 2 }; alerts.sort((a, b) => (a.until === null ? Infinity : Date.parse(a.until)) - (b.until === null ? Infinity : Date.parse(b.until)) || (SEV[a.severity] ?? 3) - (SEV[b.severity] ?? 3) || a.message.localeCompare(b.message)); const windows: MyWork["windows"] = []; for (const w of await listActiveWindows(now)) { if (!w.createdBy || !labels.has(w.createdBy)) continue; const target = await describeTarget(w.targetType, w.targetId); if (!target) continue; windows.push({ id: w.id, targetType: w.targetType, targetId: w.targetId, targetName: target.name, targetKind: target.kind, reason: w.reason, startedAt: w.startedAt, endsAt: w.endsAt, endsSoon: Date.parse(w.endsAt) - nowMs <= WINDOW_ENDS_SOON_MS }); } windows.sort((a, b) => a.endsAt.localeCompare(b.endsAt)); const labelList = [...labels]; const runRows = labelList.length === 0 ? [] : await db .select({ run: updateRuns, serverName: servers.name }) .from(updateRuns) .innerJoin(servers, eq(servers.id, updateRuns.serverId)) .where(and(inArray(updateRuns.startedBy, labelList), gte(updateRuns.startedAt, new Date(nowMs - 7 * 86_400_000).toISOString()))); const runs: MyWork["updateRuns"] = []; for (const { run, serverName } of runRows) { const running = run.finishedAt === null; // Still going, or finished recently enough that its result is news. if (!running && nowMs - Date.parse(run.finishedAt!) > RUN_KEPT_MS) continue; runs.push({ id: run.id, serverId: run.serverId, serverName, templateName: run.templateName, status: run.status, message: run.message, startedAt: run.startedAt, finishedAt: run.finishedAt, running, updatesBefore: run.updatesBefore, updatesAfter: run.updatesAfter, rebootRequired: run.rebootRequired }); } runs.sort((a, b) => Number(b.running) - Number(a.running) || b.startedAt.localeCompare(a.startedAt)); const today = now.toISOString().slice(0, 10); const soon = new Date(nowMs + TODO_DUE_SOON_DAYS * 86_400_000).toISOString().slice(0, 10); const jobs: MyWork["todos"] = (await openTodosFor(user.id)).map((t) => ({ id: t.id, title: t.title, detail: t.detail, dueDate: t.dueDate, overdue: t.dueDate !== null && t.dueDate < today, dueSoon: t.dueDate !== null && t.dueDate >= today && t.dueDate <= soon, target: t.target, })); const needsAttention = alerts.filter((a) => a.role === "handed" || a.endsSoon).length + windows.filter((w) => w.endsSoon).length + runs.filter((r) => r.running).length + jobs.filter((j) => j.overdue || j.dueSoon).length; return { alerts, windows, updateRuns: runs, todos: jobs, needsAttention }; }