Alert settings keeps Alert rules, Service levels and Alert tickets; administrators get a link to Settings -> Notifications. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
209 lines
9.4 KiB
TypeScript
209 lines
9.4 KiB
TypeScript
import { useState } from "react";
|
|
import { api, type HealthCheckSettings, type ServerRecord, type ServerThresholds as Thresholds } from "../api/client";
|
|
import { readableError } from "../utils/errors";
|
|
import { toast } from "../utils/toast";
|
|
import { ErrorNotice } from "./Notice";
|
|
|
|
type Field = "serverOfflineMinutes" | "diskUsagePercent" | "diskForecastDays" | "backupStaleHours";
|
|
|
|
const FIELDS: { key: Field; label: string; unit: string; min: number; max: number; hint: string; onlyForGuests?: boolean }[] = [
|
|
{ key: "serverOfflineMinutes", label: "Offline after", unit: "min", min: 15, max: 10080, hint: "How long without a report before this server counts as offline — longer for a laptop that is off at night." },
|
|
{ key: "diskUsagePercent", label: "Disk usage alert at", unit: "%", min: 50, max: 99, hint: "For every disk on this server that has no limit of its own below." },
|
|
{ key: "diskForecastDays", label: "Warn when a disk will fill within", unit: "days", min: 0, max: 90, hint: "0 turns the forecast off for this server." },
|
|
{ key: "backupStaleHours", label: "Its backup is too old after", unit: "hours", min: 0, max: 2160, hint: "For the Proxmox guest this server is. 0 means it is never called stale.", onlyForGuests: true },
|
|
];
|
|
|
|
const has = (t: Thresholds | null) => !!t && (Object.keys(t).some((k) => k !== "mounts" && k !== "ignoreServices") || Object.keys(t.mounts ?? {}).length > 0);
|
|
|
|
/**
|
|
* This server's own alert limits. Anything left empty follows Settings → Notifications, so changing the global limit still reaches every
|
|
* server that hasn't said otherwise. A disk can have a limit of its own, or be left alone altogether. Everyone can see what is set; only
|
|
* an administrator changes it.
|
|
*/
|
|
export default function ServerThresholds({ server, defaults, disks, canEdit, onChanged }: { server: ServerRecord; defaults: HealthCheckSettings; disks: string[]; canEdit: boolean; onChanged: () => void }) {
|
|
const own = server.thresholds;
|
|
const isGuest = server.proxmoxVmid !== null;
|
|
|
|
const [draft, setDraft] = useState<Record<Field, string>>(() => ({
|
|
serverOfflineMinutes: String(own?.serverOfflineMinutes ?? ""),
|
|
diskUsagePercent: String(own?.diskUsagePercent ?? ""),
|
|
diskForecastDays: String(own?.diskForecastDays ?? ""),
|
|
backupStaleHours: String(own?.backupStaleHours ?? ""),
|
|
}));
|
|
const [mounts, setMounts] = useState<Record<string, { percent: string; ignore: boolean }>>(() =>
|
|
Object.fromEntries(Object.entries(own?.mounts ?? {}).map(([m, r]) => [m, { percent: String(r.percent ?? ""), ignore: !!r.ignore }])),
|
|
);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
// The disks it reports now, plus any that have a rule but aren't reported any more (so the rule can still be taken away).
|
|
const mountNames = [...new Set([...disks, ...Object.keys(mounts)])];
|
|
|
|
function build(): Thresholds | null {
|
|
const out: Thresholds = {};
|
|
for (const f of FIELDS) {
|
|
const v = draft[f.key].trim();
|
|
if (v !== "") out[f.key] = Number(v);
|
|
}
|
|
const rules: NonNullable<Thresholds["mounts"]> = {};
|
|
for (const [m, r] of Object.entries(mounts)) {
|
|
const rule: { percent?: number; ignore?: boolean } = {};
|
|
if (r.percent.trim() !== "") rule.percent = Number(r.percent);
|
|
if (r.ignore) rule.ignore = true;
|
|
if (rule.percent !== undefined || rule.ignore) rules[m] = rule;
|
|
}
|
|
if (Object.keys(rules).length > 0) out.mounts = rules;
|
|
// The services to ignore are edited on the health card; saving limits here must not lose them.
|
|
if (own?.ignoreServices?.length) out.ignoreServices = own.ignoreServices;
|
|
return Object.keys(out).length > 0 ? out : null;
|
|
}
|
|
|
|
async function save(value: Thresholds | null) {
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
await api.servers.update(server.id, { thresholds: value });
|
|
toast.success(value ? "Saved. This server now follows its own limits where it has them." : "This server follows Settings again.");
|
|
onChanged();
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
if (!canEdit) {
|
|
if (!has(own)) return null;
|
|
return (
|
|
<div className="card mb-3">
|
|
<div className="card-header">
|
|
<h3 className="card-title">Alert limits</h3>
|
|
</div>
|
|
<div className="card-body">
|
|
<div className="text-secondary small mb-2">This server has limits of its own; everything else follows Settings.</div>
|
|
<ul className="mb-0">
|
|
{FIELDS.filter((f) => own?.[f.key] !== undefined).map((f) => (
|
|
<li key={f.key}>
|
|
{f.label}: <strong>{own![f.key]}</strong> {f.unit}
|
|
</li>
|
|
))}
|
|
{Object.entries(own?.mounts ?? {}).map(([m, r]) => (
|
|
<li key={m}>
|
|
<code>{m}</code>: {r.ignore ? "left alone" : `alerts at ${r.percent}%`}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="card mb-3">
|
|
<div className="card-header">
|
|
<h3 className="card-title">Alert limits</h3>
|
|
{has(own) && (
|
|
<div className="card-actions">
|
|
<span className="badge bg-blue-lt text-blue">its own limits</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
<form
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
void save(build());
|
|
}}
|
|
>
|
|
<div className="card-body">
|
|
<p className="text-secondary small">Leave a box empty to follow Settings → Notifications (shown inside the box). Only what you fill in is different for this server.</p>
|
|
<div className="row g-3">
|
|
{FIELDS.filter((f) => !f.onlyForGuests || isGuest || own?.[f.key] !== undefined).map((f) => (
|
|
<div className="col-md-6" key={f.key}>
|
|
<label className="form-label" htmlFor={`th-${f.key}`}>
|
|
{f.label} <span className="text-secondary">({f.unit})</span>
|
|
</label>
|
|
<input
|
|
id={`th-${f.key}`}
|
|
type="number"
|
|
className="form-control"
|
|
min={f.min}
|
|
max={f.max}
|
|
step={1}
|
|
placeholder={`Settings: ${defaults[f.key]}`}
|
|
value={draft[f.key]}
|
|
onChange={(e) => setDraft((d) => ({ ...d, [f.key]: e.target.value }))}
|
|
/>
|
|
<div className="form-hint text-secondary small mt-1">{f.hint}</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
{mountNames.length > 0 && (
|
|
<div className="mt-3">
|
|
<div className="form-label">Disks</div>
|
|
<div className="table-responsive">
|
|
<table className="table table-sm table-vcenter mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>Disk</th>
|
|
<th style={{ width: 180 }}>Alert at (%)</th>
|
|
<th>Leave it alone</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{mountNames.map((m) => {
|
|
const r = mounts[m] ?? { percent: "", ignore: false };
|
|
const set = (patch: Partial<typeof r>) => setMounts((all) => ({ ...all, [m]: { ...r, ...patch } }));
|
|
return (
|
|
<tr key={m}>
|
|
<td>
|
|
<code>{m}</code>
|
|
{!disks.includes(m) && <span className="text-secondary small ms-2">(not reported now)</span>}
|
|
</td>
|
|
<td>
|
|
<input
|
|
type="number"
|
|
className="form-control form-control-sm"
|
|
aria-label={`Alert at for ${m}`}
|
|
min={50}
|
|
max={99}
|
|
step={1}
|
|
placeholder={`${draft.diskUsagePercent.trim() || defaults.diskUsagePercent}`}
|
|
disabled={r.ignore}
|
|
value={r.percent}
|
|
onChange={(e) => set({ percent: e.target.value })}
|
|
/>
|
|
</td>
|
|
<td>
|
|
<label className="form-check mb-0">
|
|
<input type="checkbox" className="form-check-input" checked={r.ignore} onChange={(e) => set({ ignore: e.target.checked })} />
|
|
<span className="form-check-label text-secondary">no alerts for this disk</span>
|
|
</label>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
)}
|
|
<div className="mt-3">
|
|
<ErrorNotice error={error} />
|
|
</div>
|
|
</div>
|
|
<div className="card-footer d-flex gap-2">
|
|
<button type="submit" className="btn btn-primary" disabled={busy}>
|
|
{busy ? "Saving…" : "Save"}
|
|
</button>
|
|
{has(own) && (
|
|
<button type="button" className="btn" disabled={busy} onClick={() => void save(own?.ignoreServices?.length ? { ignoreServices: own.ignoreServices } : null)}>
|
|
Follow Settings again
|
|
</button>
|
|
)}
|
|
</div>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|