// Observability (PEP-202): shared pieces of the monitors list and the monitor page. // glossary-ignore-file: the section is for the superadmin only. import { durationText, FEATURE_NAMES, FEATURES, type MonitorKind, type MonitorState, type MonitorView } from "@peppa/core"; import type { Translator } from "@peppa/shared/i18n"; import { Badge } from "@/components/ui/badge"; import { Input, Label, NativeSelect } from "@/components/ui/input"; import { cn } from "@/lib/utils"; import { m } from "@peppa/shared/i18n"; export const KIND_LABEL: Record = { http: "HTTP(S)", tcp: "TCP", dns: "DNS", internet: m("Интернет"), heartbeat: "Heartbeat" }; const STATE_LABEL: Record = { up: m("работает"), down: m("не работает"), degraded: m("нестабилен"), unknown: m("проверяется") }; const STATE_VARIANT = { up: "ok", down: "danger", degraded: "warn", unknown: "muted" } as const; export function StateBadge({ m, t }: { m: Pick; t: Translator }) { if (m.paused) return {t("на паузе")}; return {t(STATE_LABEL[m.state])}; } export const pct = (v: number | null) => (v == null ? "—" : `${(v * 100).toFixed(v >= 0.9995 ? 0 : 2)}%`); /** The last checks as a strip of bars, oldest left. */ export function CheckStrip({ recent, t }: { recent: MonitorView["recent"]; t: Translator }) { return (
{Array.from({ length: Math.max(0, 30 - recent.length) }, (_, i) => ( ))} {recent.map((c) => ( ))}
); } /** Fields of the create / edit form; `where` is read by the kind (address, host, host:port, hosts). */ export function MonitorFields({ t, m, kindFixed, remindDefault }: { t: Translator; m?: MonitorView; kindFixed?: boolean; remindDefault: number }) { const tg = (m?.target ?? {}) as Record; const where = !m ? "" : m.kind === "http" ? String(tg.url) : m.kind === "tcp" ? `${tg.host}:${tg.port}` : m.kind === "dns" ? String(tg.host) : m.kind === "internet" ? (tg.hosts as string[]).join(", ") : ""; return (
{t("Что отключать при сбое")} {FEATURE_NAMES.map((f) => ( ))}
); } /** PEP-223: «не работает 1 ч 30 мин · напоминаний: 3 · следующее в 14:30». */ export function OutageLine({ m, t, now = new Date() }: { m: MonitorView; t: Translator; now?: Date }) { if (!m.outage || m.paused || (m.state !== "down" && m.state !== "degraded")) return null; const o = m.outage; const duration = t.text(durationText(now.getTime() - Date.parse(o.startedAt))); const parts = [ m.state === "down" ? t("не работает {duration}", { duration }) : t("нестабилен {duration}", { duration }), t("напоминаний: {n}", { n: o.reminders }), o.nextRemindAt ? t("следующее в {time}", { time: hhmm(o.nextRemindAt) }) : o.quiet ? t("без напоминаний: вместе с интернетом") : t("без напоминаний"), ]; return {parts.join(" · ")}; } const hhmm = (iso: string) => new Date(iso).toLocaleTimeString("ru-RU", { hour: "2-digit", minute: "2-digit", timeZone: process.env.PEPPA_TZ || undefined });