import { useEffect, useMemo, useState } from "react"; import { api, type UptimeReport } from "../api/client"; import RangeSelect from "./RangeSelect"; import TimeChart from "./TimeChart"; import { BlockSkeleton } from "./Skeleton"; import { useChartRange } from "../utils/chartRanges"; import { t } from "../i18n"; const DAY_MS = 24 * 3600_000; /** A day of uptime is drawn green when nothing was wrong, amber for some time down, red for most of the day. */ const dayColor = (v: number) => (v >= 99.9 ? "#2fb344" : v >= 50 ? "#f59f00" : "#f03e3e"); /** * How a server (or an integration) has done, a day at a time, as far back as the record goes: the share of each day it was up. The thin ribbon on the lists is the * last 30 days of the same thing; this is the chart, with a value for any day and a range up to a year. */ export default function UptimeHistory({ kind, id, name }: { kind: "servers" | "integrations"; id: number; name: string }) { const [chosen, setDays] = useChartRange(30); // A day is the smallest thing recorded, so 24 hours is not offered here: a chart of a single bar says nothing. const days = chosen < 30 ? 30 : chosen; const [report, setReport] = useState(null); const [failed, setFailed] = useState(false); useEffect(() => { let alive = true; setFailed(false); api.uptime .get(days) .then((res) => alive && setReport(res)) .catch(() => alive && setFailed(true)); return () => { alive = false; }; }, [days]); const series = report?.[kind][id]; const to = report ? Date.parse(report.endsAt) : Date.now(); const lines = useMemo(() => { if (!report || !series) return []; const end = Date.parse(report.endsAt); return [ { name: "Up", color: "#2fb344", // One point per day with something known about it, in the middle of that day. points: series.buckets.flatMap((b, i) => (b.k === 0 ? [] : [{ t: new Date(end - (report.days - i - 0.5) * DAY_MS).toISOString(), v: Math.round((1 - b.d / b.k) * 1000) / 10 }])), }, ]; }, [report, series]); return (

{t("Availability")}

{failed ? (
{t("Couldn't load the record.")}
) : !report ? ( ) : !series ? (
Nothing is known about {name} in this stretch yet.
) : ( <>
{series.percent}% {t("up over the last {days} days", { days })}
Worked out from when it stopped reporting and reported again, so it goes back as far as the change log does; days before it was known are left blank.
)}
); }