feat(status): add system status footer powered by Uptime Kuma

Replace the static "All systems normal" indicator in the Footer with a dynamic SystemStatus component backed by an internal API route and a server-side fetch of Uptime Kuma's public status/heartbeat endpoints. The light is rendered on first paint (correct without JS) and refreshed every minute, pausing when the tab is hidden.

- src/lib/status.ts: fetches and reduces Uptime Kuma data to one state
  (up/degraded/down/maintenance/unknown), with in-process caching and stale detection
- src/pages/api/status.ts: exposes a small snapshot to the browser, avoiding CORS issues and the ~200 KB heartbeat payload
- src/components/SystemStatus.astro: renders the light server-side and keeps it current client-side
- README.md: documents the feature and optional environment variables (STATUS_PAGE_URL, STATUS_PAGE_SLUG, STATUS_WATCH_GROUPS, STATUS_CACHE_TTL_SECONDS, STATUS_TIMEOUT_SECONDS, STATUS_STALE_AFTER_SECONDS)
- Tests for the summarization logic covering outage, degraded, and maintenance scenarios
This commit is contained in:
spaghet committed 2026-11-05 16:51:46 -05:00
1 parent ab6cd7c162
commit 480259a4eb
6 files changed
+686 -8

No files matched your search

+172
View File
@@ -0,0 +1,172 @@
---
import type { StatusSnapshot } from "@lib/status"
interface Props {
snapshot: StatusSnapshot
}
const { snapshot } = Astro.props
// Refresh cadence for the browser. The server already caches Uptime Kuma for a
// minute, so polling faster than that only adds requests without new signal.
const REFRESH_MS = 60_000
function details(s: StatusSnapshot): string {
const { total, up, down, pending, maintenance } = s.summary
if (s.state === "unknown") {
return s.reason === "stale"
? "Uptime Kuma has not reported recently"
: "Status could not be read"
}
const parts = [`${up}/${total} monitors up`]
if (down > 0) parts.push(`${down} down`)
if (pending > 0) parts.push(`${pending} pending`)
if (maintenance > 0) parts.push(`${maintenance} in maintenance`)
if (s.incidents > 0) parts.push(`${s.incidents} incident${s.incidents === 1 ? "" : "s"}`)
return parts.join(", ")
}
const state = snapshot.state
const label = snapshot.label
const detailsText = details(snapshot)
---
<a
href={snapshot.statusPageUrl}
target="_blank"
rel="noopener noreferrer"
class="status group flex gap-2 justify-center sm:justify-end items-center hover:text-black dark:hover:text-white"
data-status-root
data-status={state}
data-refresh-ms={REFRESH_MS}
data-checked-at={snapshot.checkedAt}
aria-label={`System status: ${label}. ${detailsText}. Open status page`}
title={detailsText}
>
<span class="relative flex h-3 w-3" aria-hidden="true">
<span class="ping animate-ping absolute inline-flex h-full w-full rounded-full"></span>
<span class="core relative inline-flex rounded-full h-3 w-3"></span>
</span>
<span data-status-label>{label}</span>
</a>
<style>
/* Colours hang off data-status so the client script only has to flip one
attribute; Tailwind cannot generate classes built at runtime anyway. */
.status {
transition: color 300ms ease-in-out;
}
.core {
background-color: #22c55e;
transition: background-color 300ms ease-in-out;
}
.ping {
background-color: #86efac;
}
[data-status="degraded"] .core {
background-color: #f59e0b;
}
[data-status="degraded"] .ping {
background-color: #fcd34d;
}
[data-status="down"] .core {
background-color: var(--color-marinara);
}
[data-status="down"] .ping {
background-color: var(--color-spaghetti-dark-red);
}
[data-status="maintenance"] .core {
background-color: #4962bd;
}
[data-status="maintenance"] .ping {
background-color: #93a5e0;
}
[data-status="unknown"] .core {
background-color: #9ca3af;
}
[data-status="unknown"] .ping {
display: none;
}
@media (prefers-reduced-motion: reduce) {
.ping {
display: none;
}
}
</style>
<script>
const root = document.querySelector<HTMLElement>("[data-status-root]")
const labelNode = root?.querySelector<HTMLElement>("[data-status-label]")
if (root && labelNode) {
const refreshMs = Number(root.dataset.refreshMs) || 60_000
let timer: ReturnType<typeof setInterval> | null = null
const render = (snapshot: {
state: string
label: string
summary?: { up: number; total: number }
}) => {
root.dataset.status = snapshot.state
labelNode.textContent = snapshot.label
const details = snapshot.summary
? `${snapshot.summary.up}/${snapshot.summary.total} monitors up`
: ""
root.title = details || snapshot.label
root.setAttribute(
"aria-label",
`System status: ${snapshot.label}. ${details}. Open status page`
)
}
const refresh = async () => {
try {
const response = await fetch("/api/status", {
headers: { accept: "application/json" },
cache: "default",
})
if (!response.ok) throw new Error(`status endpoint returned ${response.status}`)
render(await response.json())
} catch {
// The site's own route failed (or Uptime Kuma is unreachable). Say so
// rather than leaving a confident green light on a dead stack.
root.dataset.status = "unknown"
labelNode.textContent = "Status unavailable"
}
}
const start = () => {
if (timer !== null) return
timer = setInterval(refresh, refreshMs)
}
const stop = () => {
if (timer === null) return
clearInterval(timer)
timer = null
}
// A background tab should not burn requests; refresh the moment it is seen.
document.addEventListener("visibilitychange", () => {
if (document.hidden) stop()
else {
void refresh()
start()
}
})
// No immediate fetch when the server just rendered the state. Prerendered
// routes (blog posts, legal pages) bake the build-time snapshot, so compare
// timestamps and refresh straight away if what we were handed is old.
const checkedAt = Date.parse(root.dataset.checkedAt ?? "")
if (Number.isNaN(checkedAt) || Date.now() - checkedAt > refreshMs) void refresh()
start()
}
</script>