Patch plans, a monthly report and new-port alerts

Patch plans (Updates -> Patch plans): updates on a schedule, one server at a time, each by an
ordinary update run, stopping at the first failure by default, silencing what depends on a
server while it is updated, with one message at the end.

Monthly report (Operations -> Monthly report, and optionally a message on a day of the month):
availability, incidents, updates, restore tests, backups and renewals for a calendar month,
printable. Backed by daily endpoint figures and a record of renewals.

New ports: the first report with ports is the baseline; a TCP port that begins listening later and
is not noted on the server's Ports card is a warning, cleared by Mark as expected.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-19 23:16:14 +02:00
1 parent ddd0aa622a
commit be6c5552bd
46 files changed
+7836 -26

No files matched your search

+413
View File
@@ -0,0 +1,413 @@
import { useEffect, useState } from "react";
import { Link, useSearchParams } from "react-router-dom";
import { IconPrinter } from "@tabler/icons-react";
import { api, type AvailabilityRow, type CurrentUser, type MonthlyReport as Report } from "../api/client";
import { ErrorNotice } from "../components/Notice";
import { BlockSkeleton } from "../components/Skeleton";
import { CATEGORY_NAMES, incidentPath, SEVERITY_BADGE } from "../components/IncidentHistory";
import { formatDate, formatDateTime } from "../utils/date";
import { readableError } from "../utils/errors";
import { toast } from "../utils/toast";
/** "2 h 40 min", "35 min", "3 d 4 h". */
function span(minutes: number): string {
const m = Math.max(1, Math.round(minutes));
if (m < 60) return `${m} min`;
const h = Math.floor(m / 60);
if (h < 24) return m % 60 && h < 10 ? `${h} h ${m % 60} min` : `${h} h`;
const d = Math.floor(h / 24);
return h % 24 ? `${d} d ${h % 24} h` : `${d} d`;
}
const RESULT_WORDS: Record<string, { label: string; badge: string }> = {
ok: { label: "Passed", badge: "bg-green-lt text-green" },
partial: { label: "Partly worked", badge: "bg-yellow-lt text-yellow" },
failed: { label: "Failed", badge: "bg-red-lt text-red" },
not_needed: { label: "Not needed", badge: "bg-secondary-lt text-secondary" },
};
const average = (rows: { percent: number | null }[]) => {
const known = rows.filter((r) => r.percent !== null);
return known.length === 0 ? null : Math.round((known.reduce((n, r) => n + (r.percent as number), 0) / known.length) * 10) / 10;
};
const percentBadge = (p: number | null) => (p === null ? "text-secondary" : p >= 99.9 ? "text-green" : p >= 99 ? "" : p >= 95 ? "text-yellow" : "text-red");
function Availability({ title, rows, hint }: { title: string; rows: AvailabilityRow[]; hint?: string }) {
if (rows.length === 0) return null;
return (
<div className="mb-3">
<h4 className="mb-1">{title}</h4>
{hint && <div className="text-secondary small mb-2">{hint}</div>}
<div className="table-responsive">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Name</th>
<th className="text-end">Up</th>
<th className="text-end">Down for</th>
</tr>
</thead>
<tbody>
{rows.map((r) => (
<tr key={r.id}>
<td>{r.name}</td>
<td className={`text-end ${percentBadge(r.percent)}`}>{r.percent === null ? "—" : `${r.percent}%`}</td>
<td className="text-end text-secondary">{r.downMinutes > 0 ? span(r.downMinutes) : "—"}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
/**
* How a month went, in one page that can be read, printed or sent: how available the servers and endpoints were, the incidents and how long they took, the
* updates applied, restore tests and the state of the backups, and what was renewed. The same report can be sent on a day of the month (Settings →
* Notifications → Monthly report).
*/
export default function MonthlyReport({ user }: { user: CurrentUser }) {
const [params, setParams] = useSearchParams();
const month = params.get("month") ?? undefined;
const [data, setData] = useState<{ report: Report; months: { id: string; label: string }[] } | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
let alive = true;
setData(null);
api.reports
.monthly(month)
.then((res) => {
if (!alive) return;
setData(res);
setError(null);
})
.catch((err) => alive && setError(readableError(err)));
return () => {
alive = false;
};
}, [month]);
async function copyText() {
try {
const t = await api.reports.monthlyText(data?.report.month);
await navigator.clipboard.writeText(`${t.title}\n\n${t.message}`);
toast.success("Copied the report as text.");
} catch (err) {
toast.error(readableError(err));
}
}
const r = data?.report;
const serverAvg = r ? average(r.servers) : null;
const endpointAvg = r ? average(r.endpoints) : null;
const cards = r
? [
{ label: "Servers up, on average", value: serverAvg === null ? "—" : `${serverAvg}%`, color: percentBadge(serverAvg) },
{ label: "Endpoints answering", value: endpointAvg === null ? "—" : `${endpointAvg}%`, color: percentBadge(endpointAvg) },
{ label: "Incidents began", value: String(r.incidents.began), color: r.incidents.began > 0 ? "text-yellow" : "text-green" },
{ label: "Update runs", value: String(r.updates.runs), color: r.updates.failed > 0 ? "text-yellow" : "" },
{ label: "Renewed", value: String(r.renewals.length), color: "" },
]
: [];
return (
<div className="report-page">
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
<h2 className="page-title mb-0">Monthly report</h2>
<div className="ms-auto d-flex flex-wrap gap-2 no-print">
{data && (
<select className="form-select w-auto" aria-label="Month" value={r?.month ?? ""} onChange={(e) => setParams(e.target.value === data.months[1]?.id ? {} : { month: e.target.value }, { replace: true })}>
{data.months.map((m) => (
<option key={m.id} value={m.id}>
{m.label}
{m.id === data.months[0].id ? " (so far)" : ""}
</option>
))}
</select>
)}
<button className="btn btn-outline-secondary" onClick={() => void copyText()} disabled={!r}>
Copy as text
</button>
<button className="btn btn-outline-secondary d-inline-flex align-items-center gap-1" onClick={() => window.print()} disabled={!r}>
<IconPrinter size={16} aria-hidden="true" /> Print
</button>
</div>
</div>
<p className="text-secondary no-print">
How the month went, in one page: availability, incidents and how long they took, updates applied, restore tests and backups, and what was renewed.
{user.role === "admin" ? (
<>
{" "}
It can be sent on a day of the month: <Link to="/settings/notifications">Settings → Notifications</Link>.
</>
) : null}
</p>
<ErrorNotice error={error} />
{!data && !error && <BlockSkeleton lines={6} />}
{r && (
<>
<h3 className="mb-1">{r.label}</h3>
<div className="text-secondary mb-3">
{formatDate(new Date(r.from))} – {formatDate(new Date(r.to))}
{r.partial ? " · the month is not over, so this is the month so far" : ""} · time zone {r.timezone}
</div>
<div className="row row-cards mb-3">
{cards.map((c) => (
<div className="col-6 col-md" key={c.label}>
<div className="card card-sm">
<div className="card-body">
<div className={`h2 mb-0 ${c.color}`}>{c.value}</div>
<div className="text-secondary">{c.label}</div>
</div>
</div>
</div>
))}
</div>
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">Availability</h3>
</div>
<div className="card-body">
{r.servers.length === 0 && r.endpoints.length === 0 && r.integrations.length === 0 ? (
<div className="text-secondary">Nothing was measured for this month. Servers are measured from when their agent first reported, endpoints from when they were first checked.</div>
) : (
<>
<Availability title="Servers" rows={r.servers} hint="From when each stopped reporting and reported again; the worst first." />
{r.endpoints.length > 0 && (
<div className="mb-3">
<h4 className="mb-1">Endpoints</h4>
<div className="text-secondary small mb-2">From every check's answers, day by day (the days are UTC days).</div>
<div className="table-responsive">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Check</th>
<th className="text-end">Answered</th>
<th className="text-end">Average answer</th>
<th className="text-end">Times asked</th>
</tr>
</thead>
<tbody>
{r.endpoints.map((e) => (
<tr key={e.id}>
<td>{e.name}</td>
<td className={`text-end ${percentBadge(e.percent)}`}>{e.percent === null ? "—" : `${e.percent}%`}</td>
<td className="text-end text-secondary">{e.avgMs === null ? "—" : `${e.avgMs} ms`}</td>
<td className="text-end text-secondary">{e.checks}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
<Availability title="Integrations" rows={r.integrations} hint="How often the calls to each kind of integration worked." />
</>
)}
</div>
</div>
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">Incidents</h3>
</div>
<div className="card-body">
{r.incidents.began === 0 && r.incidents.resolved === 0 && r.incidents.stillOpen === 0 ? (
<div className="text-secondary">No incident began or ended this month.</div>
) : (
<>
<p>
<strong>{r.incidents.began}</strong> began, <strong>{r.incidents.resolved}</strong> ended{r.incidents.stillOpen > 0 ? <>, <strong>{r.incidents.stillOpen}</strong> {r.partial ? "open" : "still open at the end of the month"}</> : null}.
{r.incidents.averageMinutes !== null && <> One that began and ended in the month took {span(r.incidents.averageMinutes)} on average, {span(r.incidents.totalMinutes)} in all.</>}
</p>
{r.incidents.byCategory.length > 0 && (
<div className="mb-3 d-flex flex-wrap gap-2">
{r.incidents.byCategory.map((c) => (
<span key={c.category} className="badge bg-secondary-lt text-secondary">
{CATEGORY_NAMES[c.category as keyof typeof CATEGORY_NAMES] ?? c.category} · {c.count}
</span>
))}
</div>
)}
<div className="table-responsive">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Began</th>
<th>Where</th>
<th>What</th>
<th>Worst</th>
<th className="text-end">Lasted</th>
</tr>
</thead>
<tbody>
{r.incidents.list.map((i) => (
<tr key={i.id}>
<td className="text-nowrap">{formatDateTime(new Date(i.openedAt))}</td>
<td>{i.where}</td>
<td>
<Link to={incidentPath(i.id)}>{i.message}</Link>
</td>
<td>
<span className={`badge ${SEVERITY_BADGE[i.severity] ?? ""}`}>{i.severity}</span>
</td>
<td className="text-end text-nowrap">
{span(i.minutes)}
{!i.resolvedAt && <span className="text-secondary small"> and open</span>}
</td>
</tr>
))}
</tbody>
</table>
</div>
{r.incidents.began > r.incidents.list.length && <div className="text-secondary small">The {r.incidents.list.length} longest of {r.incidents.began}. All are under Alerts → History.</div>}
</>
)}
</div>
</div>
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">Updates</h3>
</div>
<div className="card-body">
{r.updates.runs === 0 && r.updates.planRuns.runs === 0 ? (
<div className="text-secondary">No update run was made from here this month.</div>
) : (
<>
<p>
<strong>{r.updates.runs}</strong> update run{r.updates.runs === 1 ? "" : "s"}: {r.updates.done} done{r.updates.failed > 0 ? `, ${r.updates.failed} failed` : ""}
{r.updates.installed > 0 ? ` — ${r.updates.installed} update${r.updates.installed === 1 ? "" : "s"} installed` : ""}.
{r.updates.planRuns.runs > 0 && (
<>
{" "}
Patch plans ran {r.updates.planRuns.runs} time{r.updates.planRuns.runs === 1 ? "" : "s"}
{r.updates.planRuns.failed > 0 ? `, ${r.updates.planRuns.failed} not cleanly` : ", all cleanly"}.
</>
)}
</p>
{r.updates.byServer.length > 0 && (
<div className="table-responsive">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Server</th>
<th className="text-end">Runs</th>
<th className="text-end">Failed</th>
<th className="text-end">Installed</th>
</tr>
</thead>
<tbody>
{r.updates.byServer.map((s) => (
<tr key={s.name}>
<td>{s.name}</td>
<td className="text-end">{s.runs}</td>
<td className={`text-end ${s.failed > 0 ? "text-red" : "text-secondary"}`}>{s.failed || "—"}</td>
<td className="text-end">{s.installed || "—"}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</>
)}
</div>
</div>
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">Backups and restore tests</h3>
</div>
<div className="card-body">
{r.restoreTests.length === 0 ? (
<p className="text-secondary">No restore test was recorded this month.</p>
) : (
<div className="table-responsive mb-2">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Tested</th>
<th>What</th>
<th>Result</th>
<th>Note</th>
</tr>
</thead>
<tbody>
{r.restoreTests.map((t, i) => (
<tr key={`${t.label}-${i}`}>
<td className="text-nowrap">{t.testedAt}</td>
<td>{t.label}</td>
<td>
<span className={`badge ${RESULT_WORDS[t.result]?.badge ?? ""}`}>{RESULT_WORDS[t.result]?.label ?? t.result}</span>
</td>
<td className="text-secondary">{t.note ?? ""}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
{r.backups ? (
<p className="mb-0">
Backups as they are now (<span title={formatDateTime(new Date(r.backups.asOf))}>read {formatDate(new Date(r.backups.asOf))}</span>): <strong>{r.backups.ok}</strong> of {r.backups.total} guest{r.backups.total === 1 ? "" : "s"} have a recent one
{r.backups.failed + r.backups.stale + r.backups.none + r.backups.unknown > 0 && (
<>
; {r.backups.failed} failed, {r.backups.stale} stale, {r.backups.none} without any{r.backups.unknown > 0 ? `, ${r.backups.unknown} unknown` : ""}
</>
)}
. See <Link to="/backups">Backup coverage</Link>.
</p>
) : (
<p className="text-secondary mb-0">The backups have not been read since this app started, so their state is not shown here. Open Backup coverage to read them.</p>
)}
</div>
</div>
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">Renewed</h3>
</div>
<div className="card-body">
{r.renewals.length === 0 ? (
<div className="text-secondary">No certificate or domain was seen to be renewed this month.</div>
) : (
<div className="table-responsive">
<table className="table table-vcenter table-sm">
<thead>
<tr>
<th>Seen</th>
<th>What</th>
<th>Was good until</th>
<th>Is good until</th>
</tr>
</thead>
<tbody>
{r.renewals.map((x, i) => (
<tr key={`${x.subject}-${i}`}>
<td className="text-nowrap">{formatDate(new Date(x.at))}</td>
<td>
{x.kind === "domain" ? "Domain" : "Certificate"} {x.subject}
</td>
<td className="text-secondary">{x.fromDate}</td>
<td>{x.toDate}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
<div className="text-secondary small mt-2">Renewals are noticed when the app re-reads an expiry date, so only those since it began watching are here.</div>
</div>
</div>
</>
)}
</div>
);
}