Service-level targets with error budgets, and quarterly and annual reports

Monitoring -> Service levels: a target (99.9 %) on an endpoint check, a server or an
integration, measured over calendar months. Shows the figure, downtime used, the
error budget left, and how the last six months went. 80 % of the budget used is a
warning and an overspent budget a critical alert. Endpoints are measured day by day,
servers and integrations to the minute from the audit log.

The report page gains Quarter and Year (same figures, with a Service levels section),
and two settings send the quarterly and annual report after the month that ends them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-21 22:20:07 +02:00
1 parent 31044b2b64
commit 3b31b16796
31 files changed
+7706 -61

No files matched your search

+14 -13
View File
@@ -1,6 +1,6 @@
import { Router } from "express";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { buildMonthlyReport, isMonthId, lastFullMonth, monthlyReportText, recentMonths } from "../services/monthlyReport.js";
import { buildMonthlyReport, isPeriodId, lastFullMonth, monthlyReportText, recentPeriods } from "../services/monthlyReport.js";
import { recordAudit } from "../services/audit.js";
import { notifyNow } from "../services/notify.js";
import { getSettings } from "../services/settingsStore.js";
@@ -10,29 +10,30 @@ export const reportsRouter = Router();
reportsRouter.use(requireAuth);
/** The month asked for, or the last whole one; null when what was asked is not a month. */
async function monthFrom(query: unknown): Promise<{ id: string; months: { id: string; label: string }[] } | null> {
/** The month, quarter or year asked for, or the last whole month; null when what was asked is not one that can be had. */
async function monthFrom(query: unknown): Promise<({ id: string } & ReturnType<typeof recentPeriods>) | null> {
const tz = (await getSettings()).notifications.timezone || "UTC";
const months = recentMonths(new Date(), tz);
if (query === undefined || query === "") return { id: lastFullMonth(new Date(), tz), months };
if (!isMonthId(query) || !months.some((m) => m.id === query)) return null;
return { id: query, months };
const options = recentPeriods(new Date(), tz);
if (query === undefined || query === "") return { id: lastFullMonth(new Date(), tz), ...options };
const all = [...options.months, ...options.quarters, ...options.years];
if (!isPeriodId(query) || !all.some((m) => m.id === query)) return null;
return { id: query, ...options };
}
// How a month went, for everyone signed in: it is only what the pages it comes from already show them. ?month=2026-09, the last whole month by default;
// ?format=text is the plain-text version a notification carries.
// How a month, a quarter or a year went, for everyone signed in: it is only what the pages it comes from already show them. ?month=2026-09 (or ?month=2026-Q3,
// ?month=2026), the last whole month by default; ?format=text is the plain-text version a notification carries.
reportsRouter.get("/monthly", asyncHandler(async (req, res) => {
const picked = await monthFrom(req.query.month);
if (!picked) return res.status(400).json({ error: "invalid_month", message: "Ask for one of the last twelve months, like 2026-09." });
const picked = await monthFrom(req.query.month ?? req.query.period);
if (!picked) return res.status(400).json({ error: "invalid_month", message: "Ask for one of the last twelve months (2026-09), the last eight quarters (2026-Q3) or the last three years (2026)." });
const report = await buildMonthlyReport(picked.id);
if (req.query.format === "text") return res.json(monthlyReportText(report));
res.json({ report, months: picked.months });
res.json({ report, months: picked.months, quarters: picked.quarters, years: picked.years });
}));
// Send it now to the notification channels, as if it were the day. Administrators.
reportsRouter.post("/monthly/send", requireRole("admin"), asyncHandler(async (req, res) => {
const picked = await monthFrom((req.body as { month?: unknown } | undefined)?.month);
if (!picked) return res.status(400).json({ error: "invalid_month", message: "Ask for one of the last twelve months, like 2026-09." });
if (!picked) return res.status(400).json({ error: "invalid_month", message: "Ask for one of the last twelve months (2026-09), the last eight quarters (2026-Q3) or the last three years (2026)." });
const { title, message } = monthlyReportText(await buildMonthlyReport(picked.id));
await notifyNow(title, message);
await recordAudit({ actor: req.currentUser!, category: "report", action: "send_monthly", targetType: "report", detail: { month: picked.id } });