Resources card: CPU, Memory and Network on top, More/Less for Swap, Disk and a bandwidth test
The test downloads random bytes from the VPS for 5 s and shows the fastest half second. It's refused while players are connected, since it saturates the uplink. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a963dc1bf4
commit
169a6154ab
4 changed files with 112 additions and 4 deletions
|
|
@ -72,6 +72,7 @@ async function refresh() {
|
|||
try {
|
||||
[servers, stats] = await Promise.all([api('/api/servers'), api('/api/stats')]);
|
||||
renderHost();
|
||||
applyBandwidthGuard();
|
||||
renderServers();
|
||||
if ($('#manage-dialog').open) renderPlayers();
|
||||
if (view === 'backups') loadBackups();
|
||||
|
|
@ -113,6 +114,70 @@ function drawGraph(id, vals) {
|
|||
title.textContent = `${id === '#g-rx' ? 'Download' : 'Upload'}: peak ${fmtRate(peak)} in the last ${(GRAPH_POINTS * 3) / 60} minutes`;
|
||||
}
|
||||
|
||||
// More/Less on the resources card; remembered per browser.
|
||||
function setResourcesOpen(open) {
|
||||
$('#resources-more').classList.toggle('hidden', !open);
|
||||
$('#resources-toggle').textContent = open ? 'Less ▴' : 'More ▾';
|
||||
$('#resources-toggle').setAttribute('aria-expanded', open);
|
||||
try { localStorage.setItem('resources-open', open ? '1' : ''); } catch { /* private mode */ }
|
||||
}
|
||||
try { setResourcesOpen(!!localStorage.getItem('resources-open')); } catch { /* private mode */ }
|
||||
$('#resources-toggle').addEventListener('click', () => setResourcesOpen($('#resources-more').classList.contains('hidden')));
|
||||
|
||||
// Bandwidth test: download random bytes from the VPS for BW_TEST_S and report the fastest half second,
|
||||
// i.e. the VPS's upload as seen by this browser. app.py refuses it while players are connected.
|
||||
const BW_TEST_S = 5;
|
||||
const fmtMbps = (bytesPerSec) => `${((bytesPerSec * 8) / 1e6).toFixed(bytesPerSec < 125000 ? 2 : 0)} Mbit/s`;
|
||||
let bwRunning = false;
|
||||
|
||||
function applyBandwidthGuard() {
|
||||
const online = Object.values(stats.servers).reduce((n, st) => n + (st.players?.length || 0), 0);
|
||||
const why = online ? `${online} player${online === 1 ? ' is' : 's are'} connected. The test uses all of the VPS's `
|
||||
+ 'bandwidth and would lag them, so it\'s disabled until everyone has left.' : '';
|
||||
$('#bw-btn').disabled = bwRunning || !!why;
|
||||
$('#bw-tip').title = why;
|
||||
}
|
||||
|
||||
$('#bw-btn').addEventListener('click', async () => {
|
||||
bwRunning = true;
|
||||
applyBandwidthGuard();
|
||||
$('#bw-btn').textContent = 'Testing…';
|
||||
const out = $('#bw-result');
|
||||
const ctrl = new AbortController();
|
||||
let total = 0;
|
||||
let peak = 0;
|
||||
const samples = []; // [time, total bytes]
|
||||
try {
|
||||
const res = await fetch('/api/bandwidth-test', { signal: ctrl.signal, cache: 'no-store' });
|
||||
if (!res.ok) throw new Error((await res.json().catch(() => ({}))).detail || `Request failed (${res.status})`);
|
||||
const reader = res.body.getReader();
|
||||
const start = performance.now();
|
||||
setTimeout(() => ctrl.abort(), BW_TEST_S * 1000);
|
||||
samples.push([start, 0]);
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
total += value.length;
|
||||
const now = performance.now();
|
||||
samples.push([now, total]);
|
||||
while (samples.length > 2 && now - samples[1][0] >= 500) samples.shift(); // keep a 0.5 s window
|
||||
const [t0, b0] = samples[0];
|
||||
if (now - t0 >= 500) peak = Math.max(peak, ((total - b0) * 1000) / (now - t0));
|
||||
out.textContent = `Testing… ${fmtMbps((total * 1000) / (now - start))}`;
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.name !== 'AbortError') { out.textContent = 'Test failed'; out.title = err.message; }
|
||||
}
|
||||
if (peak) {
|
||||
out.textContent = `Peak ${fmtMbps(peak)}`;
|
||||
out.title = `Fastest half second of a ${BW_TEST_S} s download from the VPS (${fmtRate(peak)}); `
|
||||
+ `${fmtBytes(total)} transferred. Limited by your own connection too.`;
|
||||
}
|
||||
bwRunning = false;
|
||||
$('#bw-btn').textContent = 'Test bandwidth';
|
||||
applyBandwidthGuard();
|
||||
});
|
||||
|
||||
function fmtDuration(sec) {
|
||||
const h = Math.floor(sec / 3600), m = Math.floor((sec % 3600) / 60);
|
||||
if (h) return `${h}h ${m}m`;
|
||||
|
|
|
|||
|
|
@ -40,14 +40,21 @@
|
|||
<div class="meters">
|
||||
<div class="meter"><div class="meter-label"><span>CPU</span><span id="m-cpu">–</span></div><div class="bar"><div id="b-cpu"></div></div></div>
|
||||
<div class="meter"><div class="meter-label"><span>Memory</span><span id="m-mem">–</span></div><div class="bar"><div id="b-mem"></div></div></div>
|
||||
<div class="meter"><div class="meter-label"><span>Swap</span><span id="m-swap">–</span></div><div class="bar"><div id="b-swap"></div></div></div>
|
||||
<div class="meter"><div class="meter-label"><span>Disk</span><span id="m-disk">–</span></div><div class="bar"><div id="b-disk"></div></div></div>
|
||||
<div class="meter">
|
||||
<div class="meter-label"><span>Network</span><span><span class="rx" id="m-rx">–</span> <span class="tx" id="m-tx">–</span></span></div>
|
||||
<svg class="graph rx" id="g-rx" viewBox="0 0 100 24" preserveAspectRatio="none"><path class="area"/><path class="line"/></svg>
|
||||
<svg class="graph tx" id="g-tx" viewBox="0 0 100 24" preserveAspectRatio="none"><path class="area"/><path class="line"/></svg>
|
||||
</div>
|
||||
</div>
|
||||
<div class="meters more-meters hidden" id="resources-more">
|
||||
<div class="meter"><div class="meter-label"><span>Swap</span><span id="m-swap">–</span></div><div class="bar"><div id="b-swap"></div></div></div>
|
||||
<div class="meter"><div class="meter-label"><span>Disk</span><span id="m-disk">–</span></div><div class="bar"><div id="b-disk"></div></div></div>
|
||||
<div class="meter">
|
||||
<div class="meter-label"><span>Bandwidth</span><span id="bw-result">Not tested</span></div>
|
||||
<span class="tip" id="bw-tip"><button class="small" id="bw-btn">Test bandwidth</button></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="more-toggle"><button class="ghost small" id="resources-toggle" aria-expanded="false" aria-controls="resources-more">More ▾</button></div>
|
||||
</div>
|
||||
|
||||
<h2 class="section-title">Servers</h2>
|
||||
|
|
|
|||
|
|
@ -75,8 +75,12 @@ header { display: flex; justify-content: space-between; align-items: center; gap
|
|||
a { color: var(--accent); }
|
||||
|
||||
/* resources */
|
||||
.meters { display: grid; grid-template-columns: repeat(4, 1fr) 1.4fr; gap: 14px; }
|
||||
@media (max-width: 900px) { .meters { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); } }
|
||||
.meters { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
|
||||
@media (max-width: 640px) { .meters { grid-template-columns: 1fr; } }
|
||||
.more-meters { margin-top: 14px; }
|
||||
.more-toggle { display: flex; justify-content: center; margin: 10px 0 -8px; }
|
||||
#resources-toggle { border: 0; color: var(--muted); }
|
||||
#resources-toggle:hover { color: var(--text); }
|
||||
.meter-label { display: flex; justify-content: space-between; gap: 8px; white-space: nowrap; font-size: 13px; margin-bottom: 4px; }
|
||||
.meter-label span:last-child { color: var(--muted); font-variant-numeric: tabular-nums; }
|
||||
.bar { height: 8px; background: var(--bar); border-radius: 4px; overflow: hidden; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue