cosmostat has working drive health dashboard

This commit is contained in:
2026-04-19 14:23:32 -07:00
parent c6007d9c33
commit 46d9f86d55
48 changed files with 4295 additions and 257 deletions

View File

@ -24,19 +24,26 @@ try {
console.error('Failed to load config.yaml:', e);
process.exit(1);
}
const API_PORT = config.custom_api_port || 5000; // fallback to 5000
const API_HOST = config.api_bind_ip || '192.168.37.1'; // fallback IP
const API_BASE = `http://${API_HOST}:${API_PORT}`;
console.log('API URL:', API_BASE);
// ---------------------------------------------------------------------
// ---------- 2. Socket.io ------------------------------------------------
// ---------------------------------------------------------------------
/* --------------------------------------------------------------------- */
/* ---------- 2. Socket.io -------------------------------------------- */
/* --------------------------------------------------------------------- */
io.on('connection', async socket => {
console.log('client connected:', socket.id);
// Call the external API every time a client connects
/* ------------- send cached client_summary ------------- */
if (clientSummaryCache.last) {
socket.emit('client_summary', clientSummaryCache.last);
console.log('sent cached client_summary to', socket.id);
}
/* ----------------------------------------------------------------- */
/* Call the external API every time a client connects */
/* ----------------------------------------------------------------- */
try {
const resp = await fetch(`${API_BASE}/start_timer`, { method: 'GET' });
const data = await resp.json();
@ -45,7 +52,9 @@ io.on('connection', async socket => {
console.error('Failed to hit start_timer endpoint:', err);
}
// Listen for tableRendered event from the client
/* ----------------------------------------------------------------- */
/* Listen for tableRendered event from the client */
/* ----------------------------------------------------------------- */
socket.on('tableRendered', async () => {
console.log('Client reported table rendered - starting timer');
try {
@ -62,24 +71,29 @@ io.on('connection', async socket => {
/* ---------- 3. Serve static files ----------------------------------- */
/* --------------------------------------------------------------------- */
app.use(express.static('public'));
/* --- 4. Redis subscriber (patched) --------------------------------- */
/* --------------------------------------------------------------------- */
/* ---------- 4. Redis subscriber ------------------------------------- */
/* --------------------------------------------------------------------- */
const redisClient = createClient({
url: 'redis://0.0.0.0:6379',
socket: { keepAlive: 60000, // 60s TCP keep-alive
reconnectStrategy: attempts => Math.min(attempts * 100, 3000) } // back-off
});
redisClient.on('error', err => console.error('Redis error', err));
/* --- local cache for client_summary -------------------------------- */
const clientSummaryCache = {}; // { last: <payload> }
/* --------------------------------------------------------------------- */
(async () => {
await redisClient.connect();
const sub = redisClient.duplicate();
await sub.connect();
// --------------------------------------------------------------------
// Helper that re-subscribes to a channel (and re-sends the handler)
// --------------------------------------------------------------------
/* --------------------------------------------------------------------- */
/* Helper that re-subscribes to a channel (and re-sends the handler) */
/* --------------------------------------------------------------------- */
async function safeSubscribe(channel, handler) {
try {
await sub.subscribe(channel, handler);
@ -89,27 +103,33 @@ redisClient.on('error', err => console.error('Redis error', err));
}
}
// ---------------------------------------------------------------
// Subscribe to all required channels
// ---------------------------------------------------------------
/* --------------------------------------------------------------------- */
/* Subscribe to all required channels */
/* --------------------------------------------------------------------- */
await safeSubscribe('host_metrics', (msg) => forward('host_metrics', msg));
await safeSubscribe('client_summary', (msg) => forward('client_summary', msg));
// ---------------------------------------------------------------
// Forward messages to Socket.io
// ---------------------------------------------------------------
/* --------------------------------------------------------------------- */
/* Forward messages to Socket.io */
/* --------------------------------------------------------------------- */
function forward(channel, message) {
try {
const payload = JSON.parse(message);
/* ----- update cache on client_summary ----- */
if (channel === 'client_summary') {
clientSummaryCache.last = payload;
}
io.emit(channel, payload);
} catch (e) {
console.error(`Failed to parse message from ${channel}`, e);
}
}
// ----------------------------------------------------------------
// Re-subscribe automatically when the Redis connection reconnects
// ----------------------------------------------------------------
/* --------------------------------------------------------------------- */
/* Re-subscribe automatically when the Redis connection reconnects */
/* --------------------------------------------------------------------- */
sub.on('reconnecting', () => console.log('Redis reconnecting…'));
sub.on('ready', async () => {
console.log('Redis ready - re-subscribing to all channels');
@ -117,7 +137,7 @@ redisClient.on('error', err => console.error('Redis error', err));
await safeSubscribe('client_summary', (msg) => forward('client_summary', msg));
});
// Optional: if the connection ends for any reason, close the process
/* Optional: if the connection ends for any reason, close the process */
sub.on('end', () => {
console.error('Redis connection closed - exiting');
process.exit(1);