cosmostat system_metrics.js proofreading
This commit is contained in:
@ -1,14 +1,9 @@
|
||||
/* ==============================================================
|
||||
system_metrics.js
|
||||
==============================================================
|
||||
Updated to use the unique `short_id` (the system’s 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({
|
||||
transports: ['websocket', 'polling'],
|
||||
@ -22,7 +17,7 @@
|
||||
});
|
||||
|
||||
/* ==========================================================
|
||||
Color constants – unchanged
|
||||
Color constants
|
||||
========================================================== */
|
||||
const GREEN = [ 39, 174, 96]; // #27ae60
|
||||
const YELLOW = [243, 156, 18]; // #f39c12
|
||||
@ -65,21 +60,21 @@
|
||||
if (el) el.textContent = txt;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------
|
||||
Get the *short_id* from the query string
|
||||
------------------------------------------------------------------ */
|
||||
/* ==========================================================
|
||||
Get the short_id from the query string
|
||||
========================================================== */
|
||||
function getSelectedId() {
|
||||
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) {
|
||||
const ul = document.getElementById('endpointList');
|
||||
const current = Array.from(ul.children).map(li => li.dataset.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();
|
||||
ul.innerHTML = ''; // reset list
|
||||
@ -87,12 +82,12 @@
|
||||
systemList.forEach(item => {
|
||||
const li = document.createElement('li');
|
||||
|
||||
// status dot – keyed by short_id
|
||||
// status dot - keyed by short_id
|
||||
const status = document.createElement('span');
|
||||
status.className = 'host-status';
|
||||
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');
|
||||
a.href = '?host=' + encodeURIComponent(item.short_id);
|
||||
a.textContent = item.hostname;
|
||||
@ -105,7 +100,7 @@
|
||||
}
|
||||
|
||||
/* ==========================================================
|
||||
Update status colours every second
|
||||
Update status colors every second
|
||||
========================================================== */
|
||||
function updateStatusColors() {
|
||||
const nowSec = Date.now() / 1000;
|
||||
@ -122,7 +117,7 @@
|
||||
setInterval(updateStatusColors, 1000);
|
||||
|
||||
/* ==========================================================
|
||||
Utility helpers (unchanged)
|
||||
Utility helpers
|
||||
========================================================== */
|
||||
function arraysEqual(a, b) {
|
||||
if (a.length !== b.length) return false;
|
||||
@ -226,17 +221,17 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// 1) Build the list first (so <span> elements exist)
|
||||
// Build the list first (so <span> elements exist)
|
||||
buildList(payload);
|
||||
|
||||
// 2) Store the timestamp for every short_id
|
||||
// Store the timestamp for every short_id
|
||||
payload.forEach(hostObj => {
|
||||
if (hostObj.short_id && hostObj.data_timestamp) {
|
||||
hostTimestamps[hostObj.short_id] = hostObj.data_timestamp; // seconds
|
||||
}
|
||||
});
|
||||
|
||||
// 3) Immediately update colours for the current view
|
||||
// Immediately update colors for the current view
|
||||
updateStatusColors();
|
||||
|
||||
// Metric table for selected host
|
||||
@ -249,7 +244,7 @@
|
||||
}
|
||||
|
||||
/* ==========================================================
|
||||
Socket event wiring – unchanged
|
||||
Socket event wiring
|
||||
========================================================== */
|
||||
socket.on('client_summary', handleSummary);
|
||||
socket.on('connect', () => {
|
||||
@ -265,7 +260,7 @@
|
||||
});
|
||||
|
||||
/* ==========================================================
|
||||
Request logic – unchanged
|
||||
Request logic
|
||||
========================================================== */
|
||||
function requestSummary() {
|
||||
if (!socket.connected) return; // guard against stale emits
|
||||
@ -273,7 +268,7 @@
|
||||
}
|
||||
|
||||
/* ==========================================================
|
||||
Recursive polling – unchanged
|
||||
Recursive polling
|
||||
========================================================== */
|
||||
let pollTimer = null;
|
||||
function pollLoop() {
|
||||
@ -286,7 +281,7 @@
|
||||
});
|
||||
|
||||
/* ==========================================================
|
||||
Watchdog – force reconnect if no data for 15 s
|
||||
Watchdog - force reconnect if no data for 15 s
|
||||
========================================================== */
|
||||
function watchdog() {
|
||||
if (Date.now() - lastUpdate > 15000 && socket.connected) {
|
||||
|
||||
Reference in New Issue
Block a user