cosmostat system_metrics.js proofreading

This commit is contained in:
2026-03-29 09:43:52 -07:00
parent 4c4d9e4d6f
commit 9646ee92fd

View File

@ -1,14 +1,9 @@
/* ============================================================== /* ==============================================================
system_metrics.js system_metrics.js
==============================================================
Updated to use the unique `short_id` (the systems key) rather
than the hostname. Hostnames are still displayed to the user
but every internal mapping and URL uses the short_id so duplicate
hostnames no longer collide.
============================================================== */ ============================================================== */
(() => { (() => {
/* ========================================================== /* ==========================================================
Socket.IO setup unchanged Socket.IO setup
========================================================== */ ========================================================== */
const socket = io({ const socket = io({
transports: ['websocket', 'polling'], transports: ['websocket', 'polling'],
@ -22,7 +17,7 @@
}); });
/* ========================================================== /* ==========================================================
Color constants unchanged Color constants
========================================================== */ ========================================================== */
const GREEN = [ 39, 174, 96]; // #27ae60 const GREEN = [ 39, 174, 96]; // #27ae60
const YELLOW = [243, 156, 18]; // #f39c12 const YELLOW = [243, 156, 18]; // #f39c12
@ -65,21 +60,21 @@
if (el) el.textContent = txt; if (el) el.textContent = txt;
} }
/* ------------------------------------------------------------------ /* ==========================================================
Get the *short_id* from the query string Get the short_id from the query string
------------------------------------------------------------------ */ ========================================================== */
function getSelectedId() { function getSelectedId() {
return new URLSearchParams(window.location.search).get('host') || ''; return new URLSearchParams(window.location.search).get('host') || '';
} }
/* ========================================================== /* ==========================================================
Sidebar building uses short_id for status key Sidebar building - uses short_id for status key
========================================================== */ ========================================================== */
function buildList(systemList) { function buildList(systemList) {
const ul = document.getElementById('endpointList'); const ul = document.getElementById('endpointList');
const current = Array.from(ul.children).map(li => li.dataset.id); const current = Array.from(ul.children).map(li => li.dataset.id);
const newIds = systemList.map(s => s.short_id); const newIds = systemList.map(s => s.short_id);
if (arraysEqual(current, newIds)) return; // nothing changed if (arraysEqual(current, newIds)) return;
const selected = getSelectedId().toLowerCase(); const selected = getSelectedId().toLowerCase();
ul.innerHTML = ''; // reset list ul.innerHTML = ''; // reset list
@ -87,12 +82,12 @@
systemList.forEach(item => { systemList.forEach(item => {
const li = document.createElement('li'); const li = document.createElement('li');
// status dot keyed by short_id // status dot - keyed by short_id
const status = document.createElement('span'); const status = document.createElement('span');
status.className = 'host-status'; status.className = 'host-status';
status.dataset.id = item.short_id; status.dataset.id = item.short_id;
// link display hostname, encode short_id in URL // link - display hostname, encode short_id in URL
const a = document.createElement('a'); const a = document.createElement('a');
a.href = '?host=' + encodeURIComponent(item.short_id); a.href = '?host=' + encodeURIComponent(item.short_id);
a.textContent = item.hostname; a.textContent = item.hostname;
@ -105,7 +100,7 @@
} }
/* ========================================================== /* ==========================================================
Update status colours every second Update status colors every second
========================================================== */ ========================================================== */
function updateStatusColors() { function updateStatusColors() {
const nowSec = Date.now() / 1000; const nowSec = Date.now() / 1000;
@ -122,7 +117,7 @@
setInterval(updateStatusColors, 1000); setInterval(updateStatusColors, 1000);
/* ========================================================== /* ==========================================================
Utility helpers (unchanged) Utility helpers
========================================================== */ ========================================================== */
function arraysEqual(a, b) { function arraysEqual(a, b) {
if (a.length !== b.length) return false; if (a.length !== b.length) return false;
@ -226,17 +221,17 @@
return; return;
} }
// 1) Build the list first (so <span> elements exist) // Build the list first (so <span> elements exist)
buildList(payload); buildList(payload);
// 2) Store the timestamp for every short_id // Store the timestamp for every short_id
payload.forEach(hostObj => { payload.forEach(hostObj => {
if (hostObj.short_id && hostObj.data_timestamp) { if (hostObj.short_id && hostObj.data_timestamp) {
hostTimestamps[hostObj.short_id] = hostObj.data_timestamp; // seconds hostTimestamps[hostObj.short_id] = hostObj.data_timestamp; // seconds
} }
}); });
// 3) Immediately update colours for the current view // Immediately update colors for the current view
updateStatusColors(); updateStatusColors();
// Metric table for selected host // Metric table for selected host
@ -249,7 +244,7 @@
} }
/* ========================================================== /* ==========================================================
Socket event wiring unchanged Socket event wiring
========================================================== */ ========================================================== */
socket.on('client_summary', handleSummary); socket.on('client_summary', handleSummary);
socket.on('connect', () => { socket.on('connect', () => {
@ -265,7 +260,7 @@
}); });
/* ========================================================== /* ==========================================================
Request logic unchanged Request logic
========================================================== */ ========================================================== */
function requestSummary() { function requestSummary() {
if (!socket.connected) return; // guard against stale emits if (!socket.connected) return; // guard against stale emits
@ -273,7 +268,7 @@
} }
/* ========================================================== /* ==========================================================
Recursive polling unchanged Recursive polling
========================================================== */ ========================================================== */
let pollTimer = null; let pollTimer = null;
function pollLoop() { function pollLoop() {
@ -286,7 +281,7 @@
}); });
/* ========================================================== /* ==========================================================
Watchdog force reconnect if no data for 15s Watchdog - force reconnect if no data for 15s
========================================================== */ ========================================================== */
function watchdog() { function watchdog() {
if (Date.now() - lastUpdate > 15000 && socket.connected) { if (Date.now() - lastUpdate > 15000 && socket.connected) {