import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { api, type CurrentUser, type ServiceLevel, type ServiceLevelInput, type ServiceLevelSubject } from "../api/client"; import EmptyState from "../components/EmptyState"; import { ErrorNotice } from "../components/Notice"; import { BlockSkeleton } from "../components/Skeleton"; import { confirmDialog } from "../utils/dialogs"; import { readableError } from "../utils/errors"; import { toast } from "../utils/toast"; const SUBJECT_WORDS: Record = { endpoint: "Endpoint check", server: "Server", integration: "Integration" }; const SUBJECT_HINTS: Record = { endpoint: "Measured from the check's answers, day by day: the share that failed.", server: "Measured from when it stopped reporting and reported again, to the minute.", integration: "Measured from when calls to that kind of service started failing and worked again.", }; /** "43 min", "4 h", "2 d 3 h"; under a minute is said so. */ function span(minutes: number): string { if (minutes < 1) return "under a minute"; const m = 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 COLOR = { ok: "#2fb344", at_risk: "#f59f00", breached: "#e05252", no_data: "#9aa0a6" } as const; const BADGE = { ok: { label: "Within budget", cls: "bg-green-lt text-green" }, at_risk: { label: "Budget nearly used", cls: "bg-yellow-lt text-yellow" }, breached: { label: "Target missed", cls: "bg-red-lt text-red" }, no_data: { label: "Nothing to measure yet", cls: "bg-secondary-lt text-secondary" }, } as const; interface Draft { id: number | null; name: string; subjectType: ServiceLevelSubject; subjectId: string; target: string; alert: boolean; } const EMPTY: Draft = { id: null, name: "", subjectType: "endpoint", subjectId: "", target: "99.9", alert: true }; function Card({ level: l, isAdmin, onEdit, onRemove }: { level: ServiceLevel; isAdmin: boolean; onEdit: () => void; onRemove: () => void }) { const b = l.budget; const used = b.usedPercent ?? 0; return (

{l.name} {BADGE[b.state].label}

{SUBJECT_WORDS[l.subjectType]}: {l.subjectName ?? removed} · target {l.target}% a month
{isAdmin && (
)}
{b.state === "no_data" ? (
{l.subjectName === null ? "What this measured has been removed." : `Nothing has been recorded for ${l.period.label} yet. ${SUBJECT_HINTS[l.subjectType]}`}
) : ( <>
So far in {l.period.label}
{l.measure.percent}%
Down
{l.measure.downMinutes > 0 ? span(l.measure.downMinutes) : "—"}
Allowed this month
{span(b.allowedMinutes)}
{b.remainingMinutes >= 0 ? "Left in the budget" : "Over the budget by"}
{b.remainingMinutes >= 0 ? span(b.remainingMinutes) : span(-b.remainingMinutes)}
{used >= 100 ? "The whole error budget is used" : `${Math.round(used)}% of the error budget is used`} {b.projectedPercent !== null && b.state !== "breached" ? ` · at this pace, about ${b.projectedPercent}% by the end of the month` : ""} {l.alert ? "" : " · no alert for this one"}
)} {l.history.some((h) => h.percent !== null) && (
The months before
{l.history.map((h) => ( {h.label.split(" ")[0].slice(0, 3)} {h.percent === null ? "—" : `${h.percent}%`} ))}
)}
); } /** What each service is held to — "99.9% for Gitea" — and what is left of this month's error budget; the months before show whether it was kept. */ export default function ServiceLevels({ user }: { user: CurrentUser }) { const isAdmin = user.role === "admin"; const [data, setData] = useState> | null>(null); const [error, setError] = useState(null); const [draft, setDraft] = useState(null); const [busy, setBusy] = useState(false); function load() { api.serviceLevels .list() .then((res) => { setData(res); setError(null); }) .catch((err) => setError(readableError(err))); } useEffect(() => { load(); const t = setInterval(load, 120_000); return () => clearInterval(t); }, []); const set = (key: K, value: Draft[K]) => setDraft((d) => (d ? { ...d, [key]: value } : d)); const subjects = data && draft ? data.subjects[draft.subjectType] : []; async function save(e: React.FormEvent) { e.preventDefault(); if (!draft) return; setBusy(true); setError(null); const input: ServiceLevelInput = { name: draft.name.trim(), subjectType: draft.subjectType, subjectId: Number(draft.subjectId), target: Number(draft.target), alert: draft.alert }; try { if (draft.id === null) await api.serviceLevels.create(input); else await api.serviceLevels.update(draft.id, input); toast.success(draft.id === null ? `Set a target for ${input.name}.` : "Saved."); setDraft(null); load(); } catch (err) { setError(readableError(err)); } finally { setBusy(false); } } async function remove(l: ServiceLevel) { if (!(await confirmDialog({ title: "Remove a target", message: `Remove the target for "${l.name}"? What was measured is kept; only the target goes.`, confirmLabel: "Remove", danger: true }))) return; try { await api.serviceLevels.remove(l.id); load(); } catch (err) { setError(readableError(err)); } } return ( <>

Service levels

{isAdmin && !draft && ( )}

What a service is held to — for example 99.9% for Gitea — and what is left of this month’s error budget: the downtime the target allows. A month with 99.9% allows about 45 minutes. The months before show whether it was kept. When most of the budget is used it is a warning, and when it is overspent a critical alert, until the month turns over. Planned maintenance is not taken out: down is down. The same figures are in the{" "} monthly, quarterly and annual reports.

{draft && data && (

{draft.id === null ? "Set a target" : `Edit ${draft.name || "the target"}`}

set("name", e.target.value)} />
{SUBJECT_HINTS[draft.subjectType]}
set("target", e.target.value)} /> %
99% allows about 7 h a month, 99.9% about 45 min, 99.99% about 4 min. Between 90 and 99.999.
)} {!data && !error && } {data?.levels.length === 0 && !draft && (
setDraft({ ...EMPTY }) } : undefined} />
)} {data?.levels.map((l) => ( setDraft({ id: l.id, name: l.name, subjectType: l.subjectType, subjectId: String(l.subjectId), target: String(l.target), alert: l.alert })} onRemove={() => void remove(l)} /> ))} ); }