Speedtest Tracker, Costs, "Having trouble" on status pages, grouped Integrations and Dashboard

- Speedtest Tracker integration: charts, alerts for a slow line (median of the
  last three tests under a share of what is paid for) and for failing tests.
- Costs page (Records): things paid for by hand plus Hetzner's estimate, totals
  per currency, bills rolled forward into Coming up as Renewals.
- Status pages: a "Having trouble" state for things that answer but have an open
  alert; Speedtest reads as the internet.
- Integrations page and kind picker grouped like the sidebar (one shared types
  file); Notifications "What to send" shown one group at a time; Dashboard
  integration sections follow the menu groups, with tiles for integrations that
  have no widget.
- Docs, tests, and ROLES.md menu table brought up to date.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-26 23:23:46 +01:00
1 parent 78822346b1
commit f86661f6c9
49 files changed
+18156 -238

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
import { Router } from "express";
import { eq } from "drizzle-orm";
import { z } from "zod";
import { db } from "../db/client.js";
import { costItems } from "../db/schema.js";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { automaticCosts, costTotals, DEFAULT_CATEGORIES, isYmd, listCostItems, monthlyOf, nextRenewal, PERIODS, type CostRow } from "../services/costs.js";
import { localDate } from "../services/agenda.js";
import { getSettings } from "../services/settingsStore.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const costsRouter = Router();
costsRouter.use(requireAuth);
const ymd = z
.string()
.regex(/^\d{4}-\d{2}-\d{2}$/)
.refine((v) => isYmd(v), "not a date");
const body = z.object({
name: z.string().trim().min(1).max(100),
amount: z.number().positive().max(10_000_000),
currency: z.string().trim().regex(/^[A-Za-z]{3}$/).transform((c) => c.toUpperCase()),
period: z.enum(PERIODS),
category: z.string().trim().min(1).max(40).default("Other"),
renewsOn: ymd.nullable().optional(),
note: z.string().trim().max(300).nullable().optional(),
});
const shape = (r: CostRow, today: string) => ({
id: r.id,
name: r.name,
amount: r.amount,
currency: r.currency,
period: r.period,
category: r.category,
renewsOn: r.renewsOn,
/** The next day it is billed, worked out from the day given. */
nextRenewal: nextRenewal(r.renewsOn, r.period, today),
monthly: Math.round(monthlyOf(r.amount, r.period) * 100) / 100,
note: r.note,
});
/** What it costs: the items typed in, the estimates that can be read, and the totals per currency. Everyone signed in can see it. */
costsRouter.get("/", asyncHandler(async (_req, res) => {
const [items, automatic, settings] = await Promise.all([listCostItems(), automaticCosts(), getSettings()]);
const today = localDate(new Date(), settings.notifications.timezone || "UTC");
res.json({
items: items.map((r) => shape(r, today)),
automatic,
totals: costTotals(items, automatic),
categories: [...new Set([...DEFAULT_CATEGORIES, ...items.map((i) => i.category)])],
});
}));
costsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res) => {
const parsed = body.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Give it a name, an amount above zero, a three-letter currency (EUR, SEK …) and say whether it is per month or per year." });
const now = new Date().toISOString();
const [created] = await db.insert(costItems).values({ ...parsed.data, renewsOn: parsed.data.renewsOn ?? null, note: parsed.data.note ?? null, createdAt: now, updatedAt: now }).returning();
await recordAudit({ actor: req.currentUser!, category: "cost", action: "create", targetType: "cost", targetId: created.id, detail: { name: created.name, amount: created.amount, currency: created.currency, period: created.period } });
res.status(201).json({ id: created.id });
}));
costsRouter.put("/:id", requireRole("admin"), asyncHandler(async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const parsed = body.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Give it a name, an amount above zero, a three-letter currency (EUR, SEK …) and say whether it is per month or per year." });
const [updated] = await db.update(costItems).set({ ...parsed.data, renewsOn: parsed.data.renewsOn ?? null, note: parsed.data.note ?? null, updatedAt: new Date().toISOString() }).where(eq(costItems.id, id)).returning();
if (!updated) return res.status(404).json({ error: "not_found" });
await recordAudit({ actor: req.currentUser!, category: "cost", action: "update", targetType: "cost", targetId: id, detail: { name: updated.name, amount: updated.amount, currency: updated.currency, period: updated.period } });
res.json({ id });
}));
costsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const [removed] = await db.delete(costItems).where(eq(costItems.id, id)).returning();
if (!removed) return res.status(404).json({ error: "not_found" });
await recordAudit({ actor: req.currentUser!, category: "cost", action: "delete", targetType: "cost", targetId: id, detail: { name: removed.name } });
res.status(204).end();
}));
+4
View File
@@ -37,6 +37,7 @@ import { forgetPaperless, syncPaperless } from "../services/paperlessSync.js";
import { forgetN8n, syncN8n } from "../services/n8nSync.js";
import { forgetNetBox, syncNetBox } from "../services/netboxSync.js";
import { forgetHetzner, syncHetzner } from "../services/hetznerSync.js";
import { forgetSpeedtest, syncSpeedtest } from "../services/speedtestSync.js";
import { forgetNextcloud, syncNextcloud } from "../services/nextcloudSync.js";
import { dockerVulnSyncStatuses, findingsOf, forgetDockerVulns, isDockerVulnFresh, syncDockerVulns } from "../services/dockerVulnSync.js";
import { invalidateAlertsCache } from "../services/alerts.js";
@@ -149,6 +150,7 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
if (type === "n8n") void syncN8n(created.id).then(() => invalidateAlertsCache());
if (type === "netbox") void syncNetBox(created.id).then(() => invalidateAlertsCache());
if (type === "hetzner") void syncHetzner(created.id).then(() => invalidateAlertsCache());
if (type === "speedtest") void syncSpeedtest(created.id).then(() => invalidateAlertsCache());
if (type === "nextcloud") void syncNextcloud(created.id).then(() => invalidateAlertsCache());
if (type === "dockhand") void syncDockerVulns(created.id).then(() => invalidateAlertsCache());
@@ -262,6 +264,7 @@ integrationsRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req,
if (updated.type === "n8n" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncN8n(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "netbox" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncNetBox(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "hetzner" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncHetzner(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "speedtest" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncSpeedtest(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "nextcloud" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncNextcloud(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "dockhand" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncDockerVulns(updated.id).then(() => invalidateAlertsCache());
@@ -351,6 +354,7 @@ integrationsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req,
if (existing.type === "n8n") await forgetN8n(id);
if (existing.type === "netbox") await forgetNetBox(id);
if (existing.type === "hetzner") await forgetHetzner(id);
if (existing.type === "speedtest") await forgetSpeedtest(id);
if (existing.type === "nextcloud") await forgetNextcloud(id);
if (existing.type === "dockhand") await forgetDockerVulns(id);
await db.delete(integrations).where(eq(integrations.id, id));
+71
View File
@@ -0,0 +1,71 @@
import { Router } from "express";
import { and, asc, eq, gte } from "drizzle-orm";
import { db } from "../db/client.js";
import { integrations, speedtestResults } from "../db/schema.js";
import { requireAuth } from "../auth/middleware.js";
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
import { expectedOf, isSpeedtestFresh, median, recentResults, speedtestProblems, speedtestSyncStatuses, WEIGH } from "../services/speedtestSync.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const speedtestRouter = Router();
speedtestRouter.use(requireAuth);
/** Every Speedtest Tracker, how it is, from what the last reads stored: the newest test, the middle of the last few, what the line is sold at and what is wrong. Everyone signed in can see it. */
speedtestRouter.get("/", asyncHandler(async (_req, res) => {
const [rows, statuses] = await Promise.all([
db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "speedtest")).orderBy(integrations.name),
speedtestSyncStatuses(),
]);
const now = Date.now();
const speedtests = [];
for (const r of rows) {
const status = statuses[r.id] ?? null;
const fresh = isSpeedtestFresh(status ?? undefined, now);
const results = await recentResults(r.id, 60);
let expected = expectedOf(null);
try {
expected = expectedOf((await loadIntegrationConfig(r.id))?.config);
} catch {
// a config that cannot be read costs only the numbers it holds
}
const completed = results.filter((x) => x.status === "completed");
const latest = completed[0] ?? null;
const lastFew = completed.slice(0, WEIGH);
const middle = (pick: (x: (typeof lastFew)[number]) => number | null) => median(lastFew.map(pick).filter((v): v is number => v !== null));
speedtests.push({
id: r.id,
name: r.name,
enabled: r.enabled,
status,
fresh,
expected,
state:
results.length === 0
? null
: {
latest: latest && { at: latest.at, download: latest.downloadBps, upload: latest.uploadBps, ping: latest.pingMs, healthy: latest.healthy },
/** The middle of the newest few completed tests — what the alerts judge. */
middle: { count: lastFew.length, download: middle((x) => x.downloadBps), upload: middle((x) => x.uploadBps), ping: middle((x) => x.pingMs) },
lastTest: results[0] && { at: results[0].at, status: results[0].status },
stored: results.length,
},
problems: results.length > 0 && fresh ? speedtestProblems(r.id, r.name, results, expected) : [],
});
}
res.json({ speedtests });
}));
/** The tests of one Speedtest Tracker over the last `days` (1–90), oldest first, for the charts: Mbit/s and milliseconds. */
speedtestRouter.get("/:id/history", asyncHandler(async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const [row] = await db.select({ id: integrations.id }).from(integrations).where(and(eq(integrations.id, id), eq(integrations.type, "speedtest"))).limit(1);
if (!row) return res.status(404).json({ error: "not_found" });
const days = Math.min(90, Math.max(1, Math.floor(Number(req.query.days)) || 7));
const since = new Date(Date.now() - days * 86_400_000).toISOString();
const rows = await db.select().from(speedtestResults).where(and(eq(speedtestResults.integrationId, id), eq(speedtestResults.status, "completed"), gte(speedtestResults.at, since))).orderBy(asc(speedtestResults.at));
res.json({
days,
points: rows.map((r) => ({ t: r.at, download: r.downloadBps === null ? null : r.downloadBps / 1e6, upload: r.uploadBps === null ? null : r.uploadBps / 1e6, ping: r.pingMs })),
});
}));