One component, PageIntro, for the text under a page's title: shown as it is when it fits in two lines, otherwise folded with a Show more, remembered per page in this browser, opened by tabbing to a link in it, unfolded in print. Used on 41 pages. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
477 lines
23 KiB
TypeScript
477 lines
23 KiB
TypeScript
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, type ServiceLevelResult } 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";
|
||
import PageIntro from "../components/PageIntro";
|
||
|
||
/** "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");
|
||
|
||
/** How each service-level target did over the stretch: its figure against the target, and the downtime against what it allowed. */
|
||
function ServiceLevelsSection({ rows, partial }: { rows: ServiceLevelResult[]; partial: boolean }) {
|
||
if (rows.length === 0) return null;
|
||
const metCount = rows.filter((l) => l.met).length;
|
||
return (
|
||
<div className="card mb-3">
|
||
<div className="card-header">
|
||
<h3 className="card-title">Service levels</h3>
|
||
</div>
|
||
<div className="card-body">
|
||
<p>
|
||
<strong>{metCount}</strong> of {rows.length} {rows.length === 1 ? "target was" : "targets were"} met{partial ? " so far" : ""}. Each is judged on the downtime it allowed over the whole stretch (the error budget).{" "}
|
||
<Link to="/alert-settings?view=levels">Set targets</Link>
|
||
</p>
|
||
<div className="table-responsive">
|
||
<table className="table table-vcenter table-sm">
|
||
<thead>
|
||
<tr>
|
||
<th>Target</th>
|
||
<th className="text-end">Target</th>
|
||
<th className="text-end">Achieved</th>
|
||
<th className="text-end">Down for</th>
|
||
<th className="text-end">Allowed</th>
|
||
<th />
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{rows.map((l) => (
|
||
<tr key={l.id}>
|
||
<td>{l.name}</td>
|
||
<td className="text-end">{l.target}%</td>
|
||
<td className={`text-end ${l.met ? "text-green" : "text-red"}`}>{l.percent}%</td>
|
||
<td className="text-end text-secondary">{l.downMinutes > 0 ? span(l.downMinutes) : "—"}</td>
|
||
<td className="text-end text-secondary">{l.allowedMinutes >= 1 ? span(l.allowedMinutes) : "under a minute"}</td>
|
||
<td>{l.met ? <span className="badge bg-green-lt text-green">met</span> : <span className="badge bg-red-lt text-red">missed</span>}</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
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 }[]; quarters: { id: string; label: string }[]; years: { 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 kind = r?.period ?? (month ? (/Q/.test(month) ? "quarter" : /^\d{4}$/.test(month) ? "year" : "month") : "month");
|
||
const noun = kind === "year" ? "Annual" : kind === "quarter" ? "Quarterly" : "Monthly";
|
||
const stretch = kind === "year" ? "year" : kind === "quarter" ? "quarter" : "month";
|
||
const options = data ? (kind === "year" ? data.years : kind === "quarter" ? data.quarters : data.months) : [];
|
||
/** The newest finished one of a kind, which is what is shown when nothing is asked for. */
|
||
const lastWhole = (list: { id: string }[]) => list[1]?.id ?? list[0]?.id;
|
||
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">{noun} report</h2>
|
||
<div className="ms-auto d-flex flex-wrap gap-2 no-print">
|
||
{data && (
|
||
<div className="btn-group" role="group" aria-label="What the report covers">
|
||
{([["month", "Month", data.months], ["quarter", "Quarter", data.quarters], ["year", "Year", data.years]] as const).map(([k, label, list]) => (
|
||
<button key={k} type="button" className={`btn ${kind === k ? "btn-primary" : "btn-outline-secondary"}`} aria-pressed={kind === k} onClick={() => setParams(k === "month" ? {} : { month: lastWhole(list) ?? "" }, { replace: true })}>
|
||
{label}
|
||
</button>
|
||
))}
|
||
</div>
|
||
)}
|
||
{data && (
|
||
<select className="form-select w-auto" aria-label={noun === "Monthly" ? "Month" : noun === "Quarterly" ? "Quarter" : "Year"} value={r?.month ?? ""} onChange={(e) => setParams(e.target.value === lastWhole(options) && kind === "month" ? {} : { month: e.target.value }, { replace: true })}>
|
||
{options.map((m) => (
|
||
<option key={m.id} value={m.id}>
|
||
{m.label}
|
||
{m.id === options[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>
|
||
<PageIntro className="no-print mb-3">
|
||
How the {stretch} went, in one page: availability, how each service-level target did, 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, and a quarter's and year's report after the month that ends it: <Link to="/settings/notifications">Settings → Notifications</Link>.
|
||
</>
|
||
) : null}
|
||
</PageIntro>
|
||
<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 ${stretch} is not over, so this is the ${stretch} 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>
|
||
|
||
<ServiceLevelsSection rows={r.serviceLevels} partial={r.partial} />
|
||
|
||
<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>
|
||
);
|
||
}
|