Bulk snooze, alert rules judged on report, photos on notes
Bulk snooze: Snooze... and Wake on the ticked alerts, with undo that puts back each alert's earlier snooze exactly. Alert rules are judged for a server right after its agent reports, instead of at the next 15-minute pass; only that server's rule findings change, and the check is serialized with the pass so neither overwrites the other's state. Photos on server and device notes: up to 12 each, shrunk in the browser, accepted by their own bytes (JPEG/PNG/GIF/WebP, 4 MB), kept in the database and removed with the server or device. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
251a4a9d73
commit
a16996d975
27 files changed
+8442
-54
No files matched your search
@@ -49,6 +49,7 @@ import { networkRouter } from "./routes/network.js";
|
||||
import { myWorkRouter } from "./routes/myWork.js";
|
||||
import { apiTokensRouter } from "./routes/apiTokens.js";
|
||||
import { notesRouter, todosRouter } from "./routes/todos.js";
|
||||
import { notePhotosRouter } from "./routes/notePhotos.js";
|
||||
import { npmRouter } from "./routes/npm.js";
|
||||
import { alertRulesRouter } from "./routes/alertRules.js";
|
||||
import { storageRouter } from "./routes/storage.js";
|
||||
@@ -206,6 +207,7 @@ export function createApp() {
|
||||
app.use("/api/service-levels", serviceLevelsRouter);
|
||||
app.use("/api/todos", todosRouter);
|
||||
app.use("/api/notes", notesRouter);
|
||||
app.use("/api/note-photos", notePhotosRouter);
|
||||
app.use("/api/generator", generatorRouter);
|
||||
app.use("/api/alert-tickets", alertTicketsRouter);
|
||||
|
||||
|
||||
+20
-1
@@ -1,5 +1,5 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import { sqliteTable, text, integer, real, uniqueIndex, index, primaryKey } from "drizzle-orm/sqlite-core";
|
||||
import { sqliteTable, text, integer, real, blob, uniqueIndex, index, primaryKey } from "drizzle-orm/sqlite-core";
|
||||
|
||||
// ─── Users & roles ──────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1424,6 +1424,25 @@ export const notes = sqliteTable(
|
||||
(t) => [uniqueIndex("notes_target_idx").on(t.targetType, t.targetId)],
|
||||
);
|
||||
|
||||
// Photos on a note — the rack as it is, a cable's label, the back of the NAS. Kept in the database itself, so a database backup has them and nothing else needs a volume; each is
|
||||
// one server's or one device's, with a foreign key that cascades, so removing the server removes its photos. The bytes are exactly what the browser sent (it makes them small first),
|
||||
// and only a JPEG, PNG, GIF or WebP is accepted, told by what the bytes are and not by what the upload says it is.
|
||||
export const notePhotos = sqliteTable(
|
||||
"note_photos",
|
||||
{
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
serverId: integer("server_id").references(() => servers.id, { onDelete: "cascade" }),
|
||||
deviceId: integer("device_id").references(() => networkDevices.id, { onDelete: "cascade" }),
|
||||
contentType: text("content_type").notNull(),
|
||||
data: blob("data", { mode: "buffer" }).notNull(),
|
||||
size: integer("size").notNull(),
|
||||
caption: text("caption"),
|
||||
createdBy: text("created_by"),
|
||||
createdAt: text("created_at").notNull(), // ISO
|
||||
},
|
||||
(t) => [index("note_photos_server_idx").on(t.serverId), index("note_photos_device_idx").on(t.deviceId)],
|
||||
);
|
||||
|
||||
// Things to do: the small jobs of a homelab ("replace the UPS battery", "move the NAS to the new switch"), with a day they are due and optionally a person and
|
||||
// a thing they are about. Open ones with a due day are in Coming up; the ones handed to someone are in My work.
|
||||
export const todos = sqliteTable("todos", {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { hashToken } from "../services/tokens.js";
|
||||
import { syncServerTasks } from "../services/taskSync.js";
|
||||
import { checkRulesFor } from "../services/healthMonitor.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
import { getSettings } from "../services/settingsStore.js";
|
||||
import { latestAgentSha256, latestAgentVersions } from "../services/agentVersions.js";
|
||||
@@ -206,4 +207,6 @@ agentReportRouter.post("/", asyncHandler(authenticateAgent), express.json({ limi
|
||||
});
|
||||
|
||||
res.status(202).json({ ok: true, taskCount: parsed.data.tasks.length });
|
||||
// The figures are as new as they will be: judge the alert rules for this server now rather than at the next pass. After the answer, so the agent is not kept waiting.
|
||||
void checkRulesFor(server.id).catch((err) => console.error("[health] couldn't judge the alert rules after a report:", err instanceof Error ? err.message : err));
|
||||
}));
|
||||
@@ -2,8 +2,9 @@ import { Router } from "express";
|
||||
import { z } from "zod";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { AckError, MAX_HOURS, MAX_NOTE } from "../services/alertAcks.js";
|
||||
import { SnoozeError } from "../services/alertSnoozes.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { acknowledgeAlerts, BulkInputError, changeAssets, changeServerTag, MAX_BULK, restoreAcks, restoreAssets, restoreServerParents, setServerParents, setServerTags, silenceTargets, targetsOfAlerts, undoSilence, unacknowledgeAlerts } from "../services/bulkActions.js";
|
||||
import { acknowledgeAlerts, BulkInputError, changeAssets, changeServerTag, MAX_BULK, restoreAcks, restoreAssets, restoreServerParents, restoreSnoozes, setServerParents, setServerTags, silenceTargets, snoozeAlerts, targetsOfAlerts, undoSilence, unacknowledgeAlerts, unsnoozeAlerts } from "../services/bulkActions.js";
|
||||
import { InvalidTagError } from "../services/serverTags.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
@@ -102,7 +103,13 @@ const previousAck = z
|
||||
})
|
||||
.nullable();
|
||||
|
||||
const snoozeRecord = z.object({ conditionKey: z.string().max(700), message: z.string().max(500), until: z.string().max(40), snoozedBy: z.string().max(200), snoozedAt: z.string().max(40) });
|
||||
|
||||
const alertsBody = z.discriminatedUnion("action", [
|
||||
/** When they should come back, as a date and time with its zone. */
|
||||
z.object({ action: z.literal("snooze"), alertIds: stringIds, until: z.string().min(1).max(40) }),
|
||||
z.object({ action: z.literal("unsnooze"), alertIds: stringIds }),
|
||||
z.object({ action: z.literal("restore_snoozes"), items: z.array(z.object({ alertId: z.string().min(1).max(600), previous: snoozeRecord.nullable() })).min(1).max(MAX_BULK) }),
|
||||
z.object({ action: z.literal("acknowledge"), alertIds: stringIds, note: z.string().max(MAX_NOTE).default(""), assigneeId: z.number().int().positive().nullable().default(null), hours: z.number().int().min(1).max(MAX_HOURS).nullable().default(null) }),
|
||||
z.object({ action: z.literal("unacknowledge"), alertIds: stringIds }),
|
||||
z.object({ action: z.literal("restore_acks"), items: z.array(z.object({ alertId: z.string().min(1).max(600), previous: previousAck })).min(1).max(MAX_BULK) }),
|
||||
@@ -132,6 +139,23 @@ alertsBulkRouter.post("/", asyncHandler(async (req, res) => {
|
||||
await recordAudit({ actor, category: "alert", action: "bulk_undo", detail: { changed } });
|
||||
return res.json({ changed, skipped: [], undo: null });
|
||||
}
|
||||
case "snooze": {
|
||||
const until = new Date(body.until);
|
||||
if (Number.isNaN(until.getTime())) return bad(res, "Say when they should come back.");
|
||||
const r = await snoozeAlerts(body.alertIds, until, nameOf(req));
|
||||
await recordAudit({ actor, category: "alert", action: "bulk_snooze", detail: { changed: r.changed, until: until.toISOString(), alerts: detailIds(body.alertIds) } });
|
||||
return res.json(r);
|
||||
}
|
||||
case "unsnooze": {
|
||||
const r = await unsnoozeAlerts(body.alertIds);
|
||||
await recordAudit({ actor, category: "alert", action: "bulk_unsnooze", detail: { changed: r.changed, alerts: detailIds(body.alertIds) } });
|
||||
return res.json(r);
|
||||
}
|
||||
case "restore_snoozes": {
|
||||
const changed = await restoreSnoozes(body.items);
|
||||
await recordAudit({ actor, category: "alert", action: "bulk_undo", detail: { changed, what: "snooze" } });
|
||||
return res.json({ changed, skipped: [], undo: null });
|
||||
}
|
||||
case "silence": {
|
||||
const { targets, skipped } = await targetsOfAlerts(body.alertIds);
|
||||
const r = await silenceTargets(targets, body.minutes, body.reason || "Maintenance, from several alerts", nameOf(req), false);
|
||||
@@ -145,7 +169,7 @@ alertsBulkRouter.post("/", asyncHandler(async (req, res) => {
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof AckError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
if (err instanceof AckError || err instanceof SnoozeError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import express, { Router, type NextFunction, type Request, type Response } from "express";
|
||||
import { z } from "zod";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { addPhoto, getPhoto, listPhotos, MAX_PHOTO_BYTES, PhotoError, removePhoto, setCaption } from "../services/notePhotos.js";
|
||||
import { TARGET_TYPES, targetName } from "../services/todos.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
/** Photos on the notes of servers and devices: everyone signed in sees them; operators add, caption and remove. */
|
||||
export const notePhotosRouter = Router();
|
||||
notePhotosRouter.use(requireAuth);
|
||||
|
||||
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
|
||||
const label = (u: { name: string | null; email: string | null; oidcSub: string }) => u.name ?? u.email ?? u.oidcSub;
|
||||
|
||||
notePhotosRouter.get("/", asyncHandler(async (req, res) => {
|
||||
const type = z.enum(TARGET_TYPES).safeParse(req.query.targetType);
|
||||
const id = idOf(String(req.query.targetId ?? ""));
|
||||
if (!type.success || !Number.isInteger(id)) return res.status(400).json({ error: "invalid_target" });
|
||||
res.json({ photos: await listPhotos(type.data, id) });
|
||||
}));
|
||||
|
||||
// The picture itself. What it is is what its bytes say (it was checked when it was added), it cannot run anything — the sandbox policy and nosniff see to that even if it were opened on its own —
|
||||
// and it never changes, so the browser may keep it.
|
||||
notePhotosRouter.get("/:id/image", asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(404).json({ error: "not_found" });
|
||||
const photo = await getPhoto(id);
|
||||
if (!photo) return res.status(404).json({ error: "not_found" });
|
||||
res.setHeader("Content-Type", photo.info.contentType);
|
||||
res.setHeader("Content-Length", String(photo.data.length));
|
||||
res.setHeader("X-Content-Type-Options", "nosniff");
|
||||
res.setHeader("Content-Security-Policy", "default-src 'none'; sandbox");
|
||||
res.setHeader("Cache-Control", "private, max-age=86400, immutable");
|
||||
res.send(photo.data);
|
||||
}));
|
||||
|
||||
// The bytes of the picture are the body, whatever it says its type is (the first bytes decide); the target and the caption are in the address.
|
||||
notePhotosRouter.post(
|
||||
"/",
|
||||
requireRole("operator"),
|
||||
express.raw({ type: () => true, limit: MAX_PHOTO_BYTES + 1024 }),
|
||||
asyncHandler(async (req, res) => {
|
||||
const type = z.enum(TARGET_TYPES).safeParse(req.query.targetType);
|
||||
const id = idOf(String(req.query.targetId ?? ""));
|
||||
if (!type.success || !Number.isInteger(id)) return res.status(400).json({ error: "invalid_target" });
|
||||
const caption = typeof req.query.caption === "string" ? req.query.caption : null;
|
||||
const body = Buffer.isBuffer(req.body) ? req.body : Buffer.alloc(0);
|
||||
try {
|
||||
const actor = req.currentUser!;
|
||||
const photo = await addPhoto(type.data, id, body, caption, label(actor));
|
||||
await recordAudit({ actor, category: "note", action: "photo_add", targetType: type.data, targetId: id, detail: { name: await targetName(type.data, id), photoId: photo.id, size: photo.size, caption: photo.caption } });
|
||||
res.status(201).json({ photo });
|
||||
} catch (err) {
|
||||
if (err instanceof PhotoError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
throw err;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
notePhotosRouter.put("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
const parsed = z.object({ caption: z.string().max(500).nullable() }).safeParse(req.body);
|
||||
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body" });
|
||||
const photo = await setCaption(id, parsed.data.caption);
|
||||
if (!photo) return res.status(404).json({ error: "not_found" });
|
||||
res.json({ photo });
|
||||
}));
|
||||
|
||||
notePhotosRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const removed = await removePhoto(id);
|
||||
if (!removed) return res.status(404).json({ error: "not_found" });
|
||||
await recordAudit({ actor: req.currentUser!, category: "note", action: "photo_delete", targetType: removed.targetType, targetId: removed.targetId, detail: { name: await targetName(removed.targetType, removed.targetId), photoId: id } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
// An upload over the limit is told plainly, not with the framework's page.
|
||||
notePhotosRouter.use((err: Error & { type?: string }, _req: Request, res: Response, next: NextFunction) => {
|
||||
if (err.type === "entity.too.large") return res.status(413).json({ error: "too_big", message: `A photo can be at most ${MAX_PHOTO_BYTES / 1024 / 1024} MB.` });
|
||||
next(err);
|
||||
});
|
||||
@@ -7,7 +7,7 @@
|
||||
* past its limit on every sample for that long. A disk's temperature is only known as of its last report, so it is judged right now. A server that is not reporting is not
|
||||
* judged — its own offline alert says that.
|
||||
*/
|
||||
import { eq, gte } from "drizzle-orm";
|
||||
import { and, eq, gte } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { alertRules, serverMetrics, servers, type AlertRuleMetric } from "../db/schema.js";
|
||||
import { parseHostHealth } from "./hostHealth.js";
|
||||
@@ -128,11 +128,12 @@ export async function listRules(): Promise<RuleRow[]> {
|
||||
return (await db.select().from(alertRules)).sort((a, b) => a.name.localeCompare(b.name) || a.id - b.id);
|
||||
}
|
||||
|
||||
/** What the given rules find right now, from the stored history. Reads nothing when no rule is on. */
|
||||
export async function computeRuleFindings(rules: RuleRow[], now: number = Date.now()): Promise<RuleFinding[]> {
|
||||
/** What the given rules find right now, from the stored history — on one server only when `onlyServerId` is given, which reads only that server's history. Reads nothing when no rule is on. */
|
||||
export async function computeRuleFindings(rules: RuleRow[], now: number = Date.now(), onlyServerId?: number): Promise<RuleFinding[]> {
|
||||
const on = rules.filter((r) => r.enabled);
|
||||
if (on.length === 0) return [];
|
||||
const rows = await db.select({ id: servers.id, name: servers.name, tags: servers.tags, hostHealth: servers.hostHealth }).from(servers);
|
||||
const everything = db.select({ id: servers.id, name: servers.name, tags: servers.tags, hostHealth: servers.hostHealth }).from(servers);
|
||||
const rows = await (onlyServerId === undefined ? everything : everything.where(eq(servers.id, onlyServerId)));
|
||||
const serverList: RuleServer[] = rows.map((r) => ({
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
@@ -143,7 +144,8 @@ export async function computeRuleFindings(rules: RuleRow[], now: number = Date.n
|
||||
if (on.some((r) => r.metric !== "disk_temp")) {
|
||||
const longest = Math.max(...on.filter((r) => r.metric !== "disk_temp").map((r) => r.forMinutes));
|
||||
const since = new Date(now - (longest * 60_000 + 3 * 3600_000)).toISOString();
|
||||
for (const r of await db.select().from(serverMetrics).where(gte(serverMetrics.recordedAt, since))) {
|
||||
const recent = gte(serverMetrics.recordedAt, since);
|
||||
for (const r of await db.select().from(serverMetrics).where(onlyServerId === undefined ? recent : and(recent, eq(serverMetrics.serverId, onlyServerId)))) {
|
||||
if (r.metric !== "cpu" && r.metric !== "mem" && !r.metric.startsWith("disk:")) continue;
|
||||
const k = sampleKey(r.serverId, r.metric);
|
||||
const list = samples.get(k) ?? [];
|
||||
|
||||
@@ -60,6 +60,31 @@ export async function setSnooze(input: { alertId: string; conditionKey: string;
|
||||
return { until: values.until, snoozedBy: values.snoozedBy, snoozedAt: values.snoozedAt };
|
||||
}
|
||||
|
||||
/** A snooze as it is stored, whole — what an undo puts back. */
|
||||
export interface SnoozeRecord {
|
||||
conditionKey: string;
|
||||
message: string;
|
||||
until: string;
|
||||
snoozedBy: string;
|
||||
snoozedAt: string;
|
||||
}
|
||||
|
||||
/** The snooze on an alert as it stands, expired or not; null when it has none. */
|
||||
export async function readSnooze(alertId: string): Promise<SnoozeRecord | null> {
|
||||
const [row] = await db.select().from(alertSnoozes).where(eq(alertSnoozes.alertId, alertId)).limit(1);
|
||||
return row ? { conditionKey: row.conditionKey, message: row.message, until: row.until, snoozedBy: row.snoozedBy, snoozedAt: row.snoozedAt } : null;
|
||||
}
|
||||
|
||||
/** Puts a snooze back as it was (an undo). One whose time has already passed is not worth restoring: it would only announce the alert again at once, so it is dropped. */
|
||||
export async function restoreSnooze(alertId: string, record: SnoozeRecord, now: Date = new Date()): Promise<void> {
|
||||
if (Date.parse(record.until) <= now.getTime()) {
|
||||
await clearSnooze(alertId);
|
||||
return;
|
||||
}
|
||||
const values = { ...record, lastSeenAt: now.toISOString() };
|
||||
await db.insert(alertSnoozes).values({ alertId, ...values }).onConflictDoUpdate({ target: alertSnoozes.alertId, set: values });
|
||||
}
|
||||
|
||||
export async function clearSnooze(alertId: string): Promise<{ message: string } | null> {
|
||||
const [row] = await db.select().from(alertSnoozes).where(eq(alertSnoozes.alertId, alertId)).limit(1);
|
||||
if (!row) return null;
|
||||
|
||||
@@ -8,6 +8,8 @@ import { db } from "../db/client.js";
|
||||
import { assets, maintenanceWindows, servers } from "../db/schema.js";
|
||||
import { invalidateAlertsCache, getAlerts } from "./alerts.js";
|
||||
import { clearAck, restoreAck, setAck, type AckInfo, type SetAckInput } from "./alertAcks.js";
|
||||
import { clearSnooze, readSnooze, restoreSnooze, setSnooze, type SnoozeRecord } from "./alertSnoozes.js";
|
||||
import { keyOfAlertId } from "./flapGuard.js";
|
||||
import { impactOf } from "./dependencies.js";
|
||||
import { isRealDate } from "./inventory.js";
|
||||
import { listActiveWindows, startOrExtendWindow } from "./maintenance.js";
|
||||
@@ -232,6 +234,57 @@ export async function restoreAcks(items: AcksUndo["items"]): Promise<number> {
|
||||
return items.length;
|
||||
}
|
||||
|
||||
export interface SnoozesUndo {
|
||||
action: "restore_snoozes";
|
||||
items: { alertId: string; previous: SnoozeRecord | null }[];
|
||||
}
|
||||
|
||||
/** Snoozes each alert until the same time (an earlier snooze is replaced). Alerts that are no longer there are skipped. A time that is not allowed refuses the whole thing, before anything is changed. */
|
||||
export async function snoozeAlerts(alertIds: string[], until: Date, actor: string, now: Date = new Date()): Promise<BulkResult<SnoozesUndo>> {
|
||||
const present = new Map((await getAlerts(false)).alerts.map((a) => [a.id, a]));
|
||||
const skipped: Skipped[] = [];
|
||||
const items: SnoozesUndo["items"] = [];
|
||||
for (const alertId of unique(alertIds)) {
|
||||
const alert = present.get(alertId);
|
||||
if (!alert) {
|
||||
skipped.push({ id: alertId, name: alertId, reason: "it isn't there any more" });
|
||||
continue;
|
||||
}
|
||||
const previous = await readSnooze(alertId);
|
||||
await setSnooze({ alertId, conditionKey: keyOfAlertId(alert.id, alert.category), message: alert.message, until, actor }, now);
|
||||
items.push({ alertId, previous });
|
||||
}
|
||||
if (items.length > 0) invalidateAlertsCache();
|
||||
return { changed: items.length, skipped, undo: items.length > 0 ? { action: "restore_snoozes", items } : null };
|
||||
}
|
||||
|
||||
/** Wakes each alert that is snoozed. */
|
||||
export async function unsnoozeAlerts(alertIds: string[]): Promise<BulkResult<SnoozesUndo>> {
|
||||
const skipped: Skipped[] = [];
|
||||
const items: SnoozesUndo["items"] = [];
|
||||
for (const alertId of unique(alertIds)) {
|
||||
const previous = await readSnooze(alertId);
|
||||
if (!previous) {
|
||||
skipped.push({ id: alertId, name: alertId, reason: "it wasn't snoozed" });
|
||||
continue;
|
||||
}
|
||||
await clearSnooze(alertId);
|
||||
items.push({ alertId, previous });
|
||||
}
|
||||
if (items.length > 0) invalidateAlertsCache();
|
||||
return { changed: items.length, skipped, undo: items.length > 0 ? { action: "restore_snoozes", items } : null };
|
||||
}
|
||||
|
||||
/** Puts each alert's snooze back as it was (or takes it off, if it had none). */
|
||||
export async function restoreSnoozes(items: SnoozesUndo["items"], now: Date = new Date()): Promise<number> {
|
||||
for (const i of items) {
|
||||
if (i.previous === null) await clearSnooze(i.alertId);
|
||||
else await restoreSnooze(i.alertId, i.previous, now);
|
||||
}
|
||||
if (items.length > 0) invalidateAlertsCache();
|
||||
return items.length;
|
||||
}
|
||||
|
||||
/** The maintenance targets of the alerts given (those with none can't be silenced). */
|
||||
export async function targetsOfAlerts(alertIds: string[]): Promise<{ targets: Target[]; skipped: Skipped[] }> {
|
||||
const present = new Map((await getAlerts(false)).alerts.map((a) => [a.id, a]));
|
||||
|
||||
@@ -178,8 +178,16 @@ export function describeChange(category: string, action: string, detail: Record<
|
||||
return { title: `${typeof detail.changed === "number" ? detail.changed : "Several"} alert${detail.changed === 1 ? "" : "s"} acknowledged at once`, tone: "neutral", detail: typeof detail.note === "string" && detail.note ? detail.note : null };
|
||||
case "alert/bulk_unack":
|
||||
return { title: `Acknowledgement taken off ${typeof detail.changed === "number" ? detail.changed : "several"} alert${detail.changed === 1 ? "" : "s"}`, tone: "neutral", detail: null };
|
||||
case "note/photo_add":
|
||||
return { title: `A photo was added to a note${name ? ` on ${name}` : ""}`, tone: "neutral", detail: str(detail.caption) };
|
||||
case "note/photo_delete":
|
||||
return { title: `A photo was removed from a note${name ? ` on ${name}` : ""}`, tone: "neutral", detail: null };
|
||||
case "alert/bulk_undo":
|
||||
return { title: "Acknowledging several alerts at once was undone", tone: "neutral", detail: null };
|
||||
return { title: detail.what === "snooze" ? "Snoozing several alerts at once was undone" : "Acknowledging several alerts at once was undone", tone: "neutral", detail: null };
|
||||
case "alert/bulk_snooze":
|
||||
return { title: `${typeof detail.changed === "number" ? detail.changed : "Several"} alert${detail.changed === 1 ? "" : "s"} snoozed at once`, tone: "neutral", detail: str(detail.until) ? `until ${str(detail.until)!.slice(0, 16).replace("T", " ")} UTC` : null };
|
||||
case "alert/bulk_unsnooze":
|
||||
return { title: `${typeof detail.changed === "number" ? detail.changed : "Several"} snoozed alert${detail.changed === 1 ? "" : "s"} woken at once`, tone: "neutral", detail: null };
|
||||
case "inventory/bulk_change":
|
||||
return { title: detail.change === "undo" ? "A change to several pieces of hardware was undone" : `${typeof detail.changed === "number" ? detail.changed : "Several"} piece${detail.changed === 1 ? "" : "s"} of hardware changed at once`, tone: "neutral", detail: genericSummary(detail, ["ids", "changed", "change"]) };
|
||||
case "webhook/retry":
|
||||
|
||||
@@ -689,7 +689,49 @@ export function alsoList(names: string[]): string {
|
||||
return rest > 0 ? `${shown.join(", ")} and ${rest} more` : head;
|
||||
}
|
||||
|
||||
export async function runHealthCheck(now: number = Date.now()): Promise<{ added: number; resolved: number }> {
|
||||
// One look at a time: the pass and the check that follows an agent's report both read the state of what is known, change it and write it back.
|
||||
let running: Promise<unknown> = Promise.resolve();
|
||||
function serialized<T>(work: () => Promise<T>): Promise<T> {
|
||||
const result = running.then(work, work);
|
||||
running = result.catch(() => undefined);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function runHealthCheck(now: number = Date.now()): Promise<{ added: number; resolved: number }> {
|
||||
return serialized(() => runHealthCheckNow(now));
|
||||
}
|
||||
|
||||
/** A report from the same server is not looked at again within this long. */
|
||||
export const RULE_CHECK_MIN_GAP_MS = 60_000;
|
||||
const lastRuleCheck = new Map<number, number>();
|
||||
|
||||
/**
|
||||
* The server has just reported, so its figures are as new as they will be: judge the administrator's alert rules for it now, instead of at the next pass (up to fifteen minutes
|
||||
* later). Only this server's rule findings are looked at and only they change; everything else — the built-in checks, the other servers, the integrations — waits for the pass, so
|
||||
* what a pass would say is unchanged. Announced and recovered like the pass does. Not done during the start-up grace, nor for a server looked at in the last minute (`minGapMs`).
|
||||
*/
|
||||
export function checkRulesFor(serverId: number, now: number = Date.now(), minGapMs: number = RULE_CHECK_MIN_GAP_MS): Promise<{ added: number; resolved: number } | null> {
|
||||
if (now - PROCESS_START < STARTUP_GRACE_MS) return Promise.resolve(null);
|
||||
const last = lastRuleCheck.get(serverId);
|
||||
if (last !== undefined && now - last < minGapMs) return Promise.resolve(null);
|
||||
lastRuleCheck.set(serverId, now);
|
||||
return serialized(async () => {
|
||||
const findings = await computeRuleFindings(await listRules(), now, serverId);
|
||||
const mine = new RegExp(`^rule:\\d+:server:${serverId}(:|$)`);
|
||||
const previous = await loadState();
|
||||
const before: ActiveState = Object.fromEntries(Object.entries(previous).filter(([k]) => mine.test(k)));
|
||||
if (findings.length === 0 && Object.keys(before).length === 0) return { added: 0, resolved: 0 };
|
||||
const current: HealthCondition[] = findings.map((f) => ({ key: findingKey(f), source: "server:rules", message: f.message, severity: f.severity }));
|
||||
const { added, resolved, next, newlyDown, ended } = diffConditions(before, current, new Set(), await activeSubjects(new Date(now)));
|
||||
await setInternalFlag(STATE_FLAG, JSON.stringify({ ...Object.fromEntries(Object.entries(previous).filter(([k]) => !mine.test(k))), ...next }));
|
||||
await recordServerTransitions(newlyDown, ended);
|
||||
if (added.length > 0) await notifyHealthIssues(added);
|
||||
if (resolved.length > 0) await notifyHealthRecovered(resolved);
|
||||
return { added: added.length, resolved: resolved.length };
|
||||
});
|
||||
}
|
||||
|
||||
async function runHealthCheckNow(now: number = Date.now()): Promise<{ added: number; resolved: number }> {
|
||||
const { healthChecks } = await getSettings();
|
||||
const { snapshot, held } = await collectSnapshot();
|
||||
// What the pools read this time go into their history (one reading an hour at most), so they can be forecast.
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Photos on the notes of servers and network devices. The browser makes each one small before it is sent (a phone's photo is several megabytes; a screen's worth is a few hundred
|
||||
* kilobytes), and this is the second line: a photo over 4 MB is refused, so is anything that is not really a JPEG, PNG, GIF or WebP — told by the first bytes, never by the name or
|
||||
* the type the upload claims (an SVG, which can carry script, is not accepted) — and a thing has at most 12.
|
||||
*/
|
||||
import { count, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { notePhotos } from "../db/schema.js";
|
||||
import { targetName, type TargetType } from "./todos.js";
|
||||
|
||||
export const MAX_PHOTO_BYTES = 4 * 1024 * 1024;
|
||||
export const MAX_PHOTOS_PER_TARGET = 12;
|
||||
export const MAX_CAPTION = 120;
|
||||
|
||||
export class PhotoError extends Error {
|
||||
constructor(
|
||||
readonly status: number,
|
||||
readonly code: string,
|
||||
message: string,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export interface PhotoInfo {
|
||||
id: number;
|
||||
caption: string | null;
|
||||
contentType: string;
|
||||
size: number;
|
||||
createdBy: string | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
/** Pure: what the bytes are, from how they begin; null when they are none of the four. */
|
||||
export function sniffImage(b: Buffer): "image/jpeg" | "image/png" | "image/gif" | "image/webp" | null {
|
||||
if (b.length >= 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) return "image/jpeg";
|
||||
if (b.length >= 8 && b.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return "image/png";
|
||||
if (b.length >= 6 && (b.subarray(0, 6).toString("latin1") === "GIF87a" || b.subarray(0, 6).toString("latin1") === "GIF89a")) return "image/gif";
|
||||
if (b.length >= 12 && b.subarray(0, 4).toString("latin1") === "RIFF" && b.subarray(8, 12).toString("latin1") === "WEBP") return "image/webp";
|
||||
return null;
|
||||
}
|
||||
|
||||
const column = (type: TargetType) => (type === "server" ? notePhotos.serverId : notePhotos.deviceId);
|
||||
const cleanCaption = (c: string | null | undefined) => {
|
||||
const t = (c ?? "").replace(/\s+/g, " ").trim();
|
||||
return t === "" ? null : t.slice(0, MAX_CAPTION);
|
||||
};
|
||||
const info = (r: Pick<typeof notePhotos.$inferSelect, "id" | "caption" | "contentType" | "size" | "createdBy" | "createdAt">): PhotoInfo => ({ id: r.id, caption: r.caption, contentType: r.contentType, size: r.size, createdBy: r.createdBy, createdAt: r.createdAt });
|
||||
|
||||
/** The photos of a server or a device, oldest first (the order they were taken in). */
|
||||
export async function listPhotos(type: TargetType, id: number): Promise<PhotoInfo[]> {
|
||||
const rows = await db
|
||||
.select({ id: notePhotos.id, caption: notePhotos.caption, contentType: notePhotos.contentType, size: notePhotos.size, createdBy: notePhotos.createdBy, createdAt: notePhotos.createdAt })
|
||||
.from(notePhotos)
|
||||
.where(eq(column(type), id))
|
||||
.orderBy(notePhotos.id);
|
||||
return rows.map(info);
|
||||
}
|
||||
|
||||
export async function addPhoto(type: TargetType, id: number, bytes: Buffer, caption: string | null, actor: string, now: Date = new Date()): Promise<PhotoInfo> {
|
||||
const targetLabel = await targetName(type, id);
|
||||
if (!targetLabel) throw new PhotoError(404, "unknown_target", "That server or network device is not there.");
|
||||
if (bytes.length === 0) throw new PhotoError(400, "empty", "There is nothing in that file.");
|
||||
if (bytes.length > MAX_PHOTO_BYTES) throw new PhotoError(413, "too_big", `A photo can be at most ${MAX_PHOTO_BYTES / 1024 / 1024} MB.`);
|
||||
const contentType = sniffImage(bytes);
|
||||
if (!contentType) throw new PhotoError(400, "not_an_image", "That is not a JPEG, PNG, GIF or WebP picture.");
|
||||
const [{ n }] = await db.select({ n: count() }).from(notePhotos).where(eq(column(type), id));
|
||||
if (n >= MAX_PHOTOS_PER_TARGET) throw new PhotoError(400, "too_many", `There can be at most ${MAX_PHOTOS_PER_TARGET} photos on one note. Remove one first.`);
|
||||
const [row] = await db
|
||||
.insert(notePhotos)
|
||||
.values({ serverId: type === "server" ? id : null, deviceId: type === "device" ? id : null, contentType, data: bytes, size: bytes.length, caption: cleanCaption(caption), createdBy: actor, createdAt: now.toISOString() })
|
||||
.returning();
|
||||
return info(row);
|
||||
}
|
||||
|
||||
/** One photo with its bytes, and whose it is; null when there is none. */
|
||||
export async function getPhoto(id: number): Promise<{ info: PhotoInfo; data: Buffer; targetType: TargetType; targetId: number } | null> {
|
||||
const [row] = await db.select().from(notePhotos).where(eq(notePhotos.id, id)).limit(1);
|
||||
if (!row) return null;
|
||||
return { info: info(row), data: Buffer.from(row.data), targetType: row.serverId !== null ? "server" : "device", targetId: (row.serverId ?? row.deviceId)! };
|
||||
}
|
||||
|
||||
export async function setCaption(id: number, caption: string | null): Promise<PhotoInfo | null> {
|
||||
const [row] = await db.update(notePhotos).set({ caption: cleanCaption(caption) }).where(eq(notePhotos.id, id)).returning();
|
||||
return row ? info(row) : null;
|
||||
}
|
||||
|
||||
export async function removePhoto(id: number): Promise<{ targetType: TargetType; targetId: number } | null> {
|
||||
const [row] = await db.delete(notePhotos).where(eq(notePhotos.id, id)).returning({ serverId: notePhotos.serverId, deviceId: notePhotos.deviceId });
|
||||
return row ? { targetType: row.serverId !== null ? "server" : "device", targetId: (row.serverId ?? row.deviceId)! } : null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user