Enhance server status handling by adding normalized URL support and retry logic for fetching status

This commit is contained in:
Fabian Schieder 2026-03-01 14:44:27 +01:00
parent f3fae8ae51
commit 0e15466eee
2 changed files with 34 additions and 15 deletions

View File

@ -99,21 +99,18 @@ document.querySelectorAll('.card').forEach(card => {
const cards = Array.from(document.querySelectorAll('.card[data-status-url]'));
if (cards.length === 0) return;
try {
const res = await fetch('/server_status.php', { cache: 'no-store' });
if (!res.ok) return;
const payload = await res.json();
const byUrl = payload && payload.byUrl ? payload.byUrl : {};
const normalize = (url) => {
if (!url) return '';
return String(url).replace(/\/+$/, '');
};
// Normalisierung muss zur PHP-Normalisierung passen (Trailing Slash entfernen)
const normalize = (url) => {
if (!url) return '';
return String(url).replace(/\/+$/, '');
};
const applyPayload = (payload) => {
const byUrl = payload && payload.byUrl ? payload.byUrl : {};
const byUrlNormalized = payload && payload.byUrlNormalized ? payload.byUrlNormalized : {};
for (const card of cards) {
const key = normalize(card.getAttribute('data-status-url'));
const svc = byUrl[key] || byUrl[key + '/'] || null;
const svc = byUrl[key] || byUrl[key + '/'] || byUrlNormalized[key] || null;
if (!svc) continue;
const state = svc.state || 'unknown';
@ -134,7 +131,25 @@ document.querySelectorAll('.card').forEach(card => {
badge.title = svc.detail || 'Unbekannt';
}
}
} catch (e) {
// still fine: keep "Unbekannt"
};
const fetchAndApply = async () => {
const res = await fetch('/server_status.php', { cache: 'no-store' });
if (!res.ok) throw new Error('status_fetch_failed');
const payload = await res.json();
applyPayload(payload);
};
// Ziel: nach ~1s sichtbar springen (nicht sofort beim ersten Paint).
// Falls der Endpoint kurz hängt, probieren wir 1-2x nochmal.
const attempts = [1000, 2000, 3500];
for (let i = 0; i < attempts.length; i++) {
await new Promise(r => setTimeout(r, attempts[i]));
try {
await fetchAndApply();
break;
} catch (e) {
// keep unknown and retry
}
}
})();

View File

@ -40,11 +40,14 @@ if (!is_array($serverStatus)) {
@file_put_contents($cacheFile, json_encode(['ts' => time(), 'data' => $serverStatus], JSON_UNESCAPED_SLASHES));
}
// index by (normalized-ish) url
// index by url + normalized url (trailing slash removed)
$byUrl = [];
$byUrlNormalized = [];
foreach ($serverStatus as $svc) {
if (!empty($svc['url']) && is_string($svc['url'])) {
$byUrl[$svc['url']] = $svc;
$u = $svc['url'];
$byUrl[$u] = $svc;
$byUrlNormalized[rtrim($u, '/')] = $svc;
}
}
@ -52,6 +55,7 @@ $json = json_encode([
'ts' => time(),
'data' => $serverStatus,
'byUrl' => $byUrl,
'byUrlNormalized' => $byUrlNormalized,
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($json === false) {