fix: serve cached server data before background sync

This commit is contained in:
Luca Sacchi Ricciardi
2026-04-25 15:57:37 +02:00
parent f60781bd7f
commit 9649f2ccfb
4 changed files with 306 additions and 46 deletions
+54
View File
@@ -1,5 +1,6 @@
const SERVER_STORAGE_KEY = "llm_monitor_servers";
const ACTIVE_SERVER_KEY = "llm_monitor_active_server";
const DATA_REFRESH_INTERVAL_MS = 30000;
function normalizeHost(host) {
if (!host) {
@@ -97,3 +98,56 @@ function buildServerUrl(path, serverId) {
}
return `${url.pathname}${url.search}`;
}
function getServerStorageKey(serverId, suffix) {
return `llm_monitor_${suffix}_${serverId}`;
}
function readServerCache(serverId, suffix) {
if (!serverId) {
return null;
}
const raw = localStorage.getItem(getServerStorageKey(serverId, suffix));
if (!raw) {
return null;
}
try {
return JSON.parse(raw);
} catch {
return null;
}
}
function writeServerCache(serverId, suffix, value) {
if (!serverId) {
return;
}
localStorage.setItem(getServerStorageKey(serverId, suffix), JSON.stringify(value));
}
function getCacheTimestamp(cacheValue) {
if (!cacheValue || !cacheValue.timestamp) {
return 0;
}
const parsed = Date.parse(cacheValue.timestamp);
return Number.isNaN(parsed) ? 0 : parsed;
}
function getLatestServerCacheTimestamp(serverId, suffixes) {
return suffixes.reduce((latest, suffix) => {
const value = readServerCache(serverId, suffix);
return Math.max(latest, getCacheTimestamp(value));
}, 0);
}
function isCacheStale(timestamp, maxAgeMs = DATA_REFRESH_INTERVAL_MS) {
if (!timestamp) {
return true;
}
return (Date.now() - timestamp) >= maxAgeMs;
}