Sync local changes 2026-12-04

This commit is contained in:
Reditus committed 2026-12-04 09:35:24 +00:00
1 parent 91c7abc771
commit a490786827
5 files changed
+342 -20

No files matched your search

+150 -1
View File
@@ -47,6 +47,16 @@
{% endfor %}
</div>
<div class="spacer"></div>
<div class="nav-stats-h" id="nav-stats-h" style="display:none;">
<span class="ns-item"><span class="conn-dot" id="ns-dot-h"></span><strong id="ns-online-h">–</strong>&nbsp;online</span>
<span class="ns-item">CPU <canvas id="ns-cpu-h" width="64" height="18"></canvas> <span id="ns-cpuv-h">–</span></span>
<span class="ns-item">RAM <canvas id="ns-mem-h" width="64" height="18"></canvas> <span id="ns-memv-h">–</span></span>
</div>
<div class="nav-stats-v" id="nav-stats-v" style="display:none;">
<div class="ns-vrow"><span class="conn-dot" id="ns-dot-v"></span><strong id="ns-online-v">–</strong><span class="ns-vlabel">players online</span></div>
<div class="ns-vrow ns-vgraph"><span class="ns-vlabel">CPU</span><canvas id="ns-cpu-v" width="168" height="28"></canvas><span id="ns-cpuv-v">–</span></div>
<div class="ns-vrow ns-vgraph"><span class="ns-vlabel">RAM</span><canvas id="ns-mem-v" width="168" height="28"></canvas><span id="ns-memv-v">–</span></div>
</div>
<div class="nav-tools">
<div class="theme-dropdown">
<button class="theme-btn" title="Theme">🎨</button>
@@ -173,6 +183,7 @@
const next = current === 'horizontal' ? 'vertical' : 'horizontal';
body.setAttribute('data-nav-layout', next);
localStorage.setItem('dune-nav-layout', next);
if (typeof applyNavStatsLayout === 'function') applyNavStatsLayout();
}
function initNavLayout() {
@@ -190,8 +201,9 @@
const isMobile = window.innerWidth <= 768;
const current = document.body.getAttribute('data-nav-layout');
if (isMobile && current !== 'vertical') {
document.body.setAttribute('data-nav-layout', 'vertical');
document.body.setAttribute('data-nav-layout', current);
}
if (typeof applyNavStatsLayout === 'function') applyNavStatsLayout();
});
initTheme();
@@ -489,6 +501,143 @@
refreshUpdateBanner();
setInterval(refreshUpdateBanner, 10 * 60 * 1000);
// Sidebar/topbar live stats: CPU + RAM sparklines and online count.
// History persists across page changes in localStorage (30 points,
// roughly 5 minutes at the 10s poll rate) and doubles as seed data
// for the big Server charts.
const NS_MAX = 30;
const NS_INTERVAL = 10000;
function nsLoad(key) {
try {
const arr = JSON.parse(localStorage.getItem(key) || '[]');
if (!Array.isArray(arr)) return [];
// Same freshness rule as the big charts: drop anything older
// than 6 minutes so a restart/refresh starts clean instead of
// replaying stale history the big graphs have discarded.
const cutoff = Date.now() - 6 * 60 * 1000;
return arr.filter(p => p && typeof p.v === 'number'
&& typeof p.t === 'number' && p.t >= cutoff).slice(-NS_MAX);
} catch (e) { return []; }
}
let nsCpu = nsLoad('dune-metrics-cpu');
let nsMem = nsLoad('dune-metrics-mem');
function nsSave() {
try {
localStorage.setItem('dune-metrics-cpu', JSON.stringify(nsCpu.slice(-NS_MAX)));
localStorage.setItem('dune-metrics-mem', JSON.stringify(nsMem.slice(-NS_MAX)));
} catch (e) { /* storage full or unavailable - keep memory-only */ }
}
function nsCssVar(name, fallback) {
const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim();
return v || fallback;
}
// Memory total (GiB), latched once like the big charts so the scale
// never moves. Persisted so minis open correctly scaled on load.
let nsMemTotal = parseFloat(localStorage.getItem('dune-mem-total') || '0') || 0;
function drawSpark(canvas, values, color, ymax) {
if (!canvas) return;
const dpr = window.devicePixelRatio || 1;
const w = canvas.clientWidth || canvas.width, h = canvas.clientHeight || canvas.height;
if (!w || !h) return;
canvas.width = w * dpr;
canvas.height = h * dpr;
const ctx = canvas.getContext('2d');
ctx.scale(dpr, dpr);
ctx.clearRect(0, 0, w, h);
if (!values.length) return;
const min = 0, max = (ymax > 0) ? ymax : 100;
const step = values.length > 1 ? w / (NS_MAX - 1) : 0;
const x0 = w - step * (values.length - 1);
ctx.beginPath();
values.forEach((p, i) => {
const v = (typeof p === 'number') ? p : p.v;
const x = x0 + step * i;
const y = h - 2 - ((Math.min(Math.max(v, min), max) - min) / (max - min)) * (h - 4);
if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y);
});
ctx.strokeStyle = color;
ctx.lineWidth = 1.5;
ctx.stroke();
ctx.lineTo(x0 + step * (values.length - 1), h);
ctx.lineTo(x0, h);
ctx.closePath();
ctx.fillStyle = color + '33';
ctx.fill();
}
// Last known values so layout switches re-render instantly
// instead of waiting for the next poll.
let nsLast = { cpu: '–', mem: '–', online: null };
function applyNavStatsLayout() {
const h = document.getElementById('nav-stats-h');
const v = document.getElementById('nav-stats-v');
const layout = document.body.getAttribute('data-nav-layout') || 'horizontal';
if (h) h.style.display = (layout === 'horizontal') ? '' : 'none';
if (v) v.style.display = (layout === 'vertical') ? '' : 'none';
}
function renderNavStats(cpuStr, memStr, online) {
nsLast = { cpu: cpuStr, mem: memStr, online: online };
const onlineTxt = (online === null || online === undefined) ? '–' : String(online);
const cpuColor = nsCssVar('--primary', '#c9a24e');
const memColor = nsCssVar('--success', '#27ae60');
[['h'], ['v']].forEach(([sfx]) => {
const on = document.getElementById('ns-online-' + sfx);
const cv = document.getElementById('ns-cpuv-' + sfx);
const mv = document.getElementById('ns-memv-' + sfx);
if (on) on.textContent = onlineTxt;
if (cv) cv.textContent = cpuStr;
if (mv) mv.textContent = memStr;
const dot = document.getElementById('ns-dot-' + sfx);
if (dot) dot.className = 'conn-dot ' + ((online > 0) ? 'online' : 'offline');
drawSpark(document.getElementById('ns-cpu-' + sfx), nsCpu.map(p => p.v), cpuColor, 100);
// Pre-GiB entries have no g: skip them rather than plotting
// percent values on a GiB scale.
drawSpark(document.getElementById('ns-mem-' + sfx),
nsMem.filter(p => typeof p.g === 'number').map(p => p.g), memColor,
nsMemTotal > 0 ? Math.ceil(nsMemTotal) : 100);
});
applyNavStatsLayout();
}
async function pollNavStats() {
if (document.hidden) return; // save the server while tab is backgrounded
try {
const resp = await fetch('/server/metrics');
const data = await resp.json();
const now = Date.now();
if (typeof data.cpu_num === 'number') {
nsCpu.push({ t: now, v: data.cpu_num });
nsCpu = nsCpu.slice(-NS_MAX);
}
// Percent only for the graph scale; GiB rides along so the big
// Server charts can seed from this same history.
if (typeof data.memory_pct_num === 'number') {
const gib = (typeof data.memory_gib === 'number') ? data.memory_gib : null;
nsMem.push({ t: now, v: data.memory_pct_num, g: gib });
nsMem = nsMem.slice(-NS_MAX);
// Latch the RAM total once, then only re-latch when the
// machine itself actually changed (implied total moved
// >5%): per-sample rounding wobble never moves the ruler.
if (data.memory_pct_num > 0 && gib > 0) {
const implied = gib / (data.memory_pct_num / 100);
if (!nsMemTotal || Math.abs(implied - nsMemTotal) / nsMemTotal > 0.05) {
nsMemTotal = implied;
try { localStorage.setItem('dune-mem-total', String(nsMemTotal)); } catch (e) {}
}
}
}
// Drop stale points on every poll (not just at load) so a
// wake-from-sleep recovers on the next poll instead of
// replaying old history for minutes.
const freshCutoff = Date.now() - 6 * 60 * 1000;
nsCpu = nsCpu.filter(p => p && p.t >= freshCutoff);
nsMem = nsMem.filter(p => p && p.t >= freshCutoff);
nsSave();
renderNavStats(data.cpu || '–', data.memory || '–',
(data.online === null || data.online === undefined) ? null : data.online);
} catch (e) { /* keep last values on failure */ }
}
pollNavStats();
setInterval(pollNavStats, NS_INTERVAL);
</script>
{% block scripts %}{% endblock %}
<style>