Files
maptool/views/status.erb
T

102 lines
5.2 KiB
ERB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<title>Server status</title>
<link rel="icon" href="/icons/icon.svg" type="image/svg+xml">
<style>
:root { color-scheme: light dark; --muted: GrayText; --line: color-mix(in srgb, CanvasText 15%, transparent); --ok: #2e7d32; --bad: #c62828; }
body { margin: 0; padding: 16px; background: Canvas; color: CanvasText; font: 15px/1.4 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
header { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; margin-bottom: 12px; }
h1 { font-size: 1.2rem; margin: 0; }
#status { color: var(--muted); font-size: 0.85rem; }
input, button { padding: 6px 10px; font-size: 0.9rem; }
/* the whole width: as many columns as fit, each at least 380px */
#cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(380px, 100%), 1fr)); align-items: start; }
details { border: 1px solid var(--line); border-radius: 8px; min-width: 0; }
summary { display: flex; gap: 10px; align-items: baseline; padding: 8px 12px; cursor: pointer; }
summary code { flex: 1; font-weight: 600; overflow-wrap: anywhere; }
summary .meta { color: var(--muted); font-size: 0.8rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; align-self: center; background: var(--ok); }
.bad .dot { background: var(--bad); }
.bad summary code { color: var(--bad); }
pre { margin: 0; padding: 8px 12px; max-height: 420px; overflow: auto; border-top: 1px solid var(--line); font: 0.8rem/1.35 ui-monospace, SFMono-Regular, Menlo, monospace; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
</style>
</head>
<body>
<header>
<h1>Server status</h1>
<span id="status">loading…</span>
<input id="filter" type="search" placeholder="Filter endpoints or content…">
<button id="pause" type="button">Pause</button>
<a href="/can.html">CANopen diagnostics</a>
</header>
<div id="cards"></div>
<script>
// Every read-only status endpoint (GET, login), its JSON, refreshed every
// REFRESH_MS. Not the check-update ones (they ask the internet) nor the
// big exports (library, sync feed).
const ENDPOINTS = [
"/api/health", "/api/update", "/api/can", "/api/gnss/position", "/api/sim/gnss",
"/api/trip", "/api/trip/log", "/api/vehicles", "/api/live", "/api/livetrack/sessions",
"/api/offline-tiles", "/api/offline-tiles/build-status", "/api/offline-regions", "/api/offline-tiles/cache",
"/api/terrain-tiles", "/api/terrain-tiles/build-status", "/api/routing-tiles", "/api/routing-tiles/build-status",
"/api/build-history", "/api/hiking-trails", "/api/kiwix", "/api/plugins",
];
const REFRESH_MS = 5000;
const cards = document.getElementById("cards");
const filter = document.getElementById("filter");
const pause = document.getElementById("pause");
const status = document.getElementById("status");
const esc = (s) => s.replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
let paused = false;
let last = [];
async function load(url) {
const started = performance.now();
try {
const response = await fetch(url, { cache: "no-store" });
const body = await response.text();
let text = body;
try { text = JSON.stringify(JSON.parse(body), null, 2); } catch { /* not JSON */ }
return { url, ok: response.ok, code: response.status, ms: performance.now() - started, text };
} catch (e) {
return { url, ok: false, code: "—", ms: performance.now() - started, text: `request failed: ${e.message}` };
}
}
function render() {
const q = filter.value.trim().toLowerCase();
const closed = new Set([...cards.querySelectorAll("details:not([open])")].map((d) => d.dataset.url));
cards.innerHTML = last.filter((r) => !q || r.url.includes(q) || r.text.toLowerCase().includes(q)).map((r) => `
<details data-url="${r.url}" class="${r.ok ? "" : "bad"}" ${closed.has(r.url) ? "" : "open"}>
<summary><span class="dot"></span><code>${r.url}</code><span class="meta">${r.code} · ${Math.round(r.ms)} ms</span></summary>
<pre>${esc(r.text)}</pre>
</details>`).join("");
const bad = last.filter((r) => !r.ok).length;
status.textContent = `${last.length} endpoints, ${bad ? `${bad} failing` : "all OK"} · ${new Date().toLocaleTimeString()}${paused ? " · paused" : ""}`;
}
async function refresh() {
if (!paused) {
last = await Promise.all(ENDPOINTS.map(load));
render();
if (last.some((r) => r.code === 401)) status.insertAdjacentHTML("beforeend", ' · some need the login — <a href="/login">log in</a>');
}
setTimeout(refresh, REFRESH_MS);
}
filter.addEventListener("input", render);
pause.addEventListener("click", () => {
paused = !paused;
pause.textContent = paused ? "Resume" : "Pause";
render();
});
refresh();
</script>
</body>
</html>