diff --git a/files/web/html/src/redis.js b/files/web/html/src/redis.js index 82f11c9..d5d1ed0 100644 --- a/files/web/html/src/redis.js +++ b/files/web/html/src/redis.js @@ -16,9 +16,10 @@ function safeSetText(id, txt) { if (el) el.textContent = txt; } -/* ------------------------------------------------------------ - 2. Table rendering - the table remains a - ------------------------------------------------------------ */ +/* ------------------------------------------------------------------ + 2. Table rendering - now orders rows before building the table + ------------------------------------------------------------------ */ +// helper function for table row ordering function renderStatsTable(data) { renderGenericTable('host_metrics', data, 'No Stats available'); } @@ -33,15 +34,18 @@ function renderGenericTable(containerId, data, emptyMsg) { /* Merge rows by name (new logic) */ const mergedData = mergeRowsByName(data); - /* Build the table from the merged data */ - const table = buildTable(mergedData); - table.id = 'host_metrics_table'; + /* Order the merged rows – priority first */ + const orderedData = orderRows(mergedData); + + /* Build the table from the ordered data */ + const table = buildTable(orderedData); + table.id = 'host_metrics_table'; container.innerHTML = ''; container.appendChild(table); } /* ------------------------------------------------------------ - 3. Merge rows by name (new logic) + 3. Merge rows by name ------------------------------------------------------------ */ function mergeRowsByName(data) { const groups = {}; // { name: { types: [], metrics: [], props: [], values: [] } } @@ -81,6 +85,26 @@ function mergeRowsByName(data) { return merged; } +/* ------------------------------------------------------------------ + 3b. Order rows - put “System”, “CPU”, “RAM” first + ------------------------------------------------------------------ */ +function orderRows(rows) { + // this should be updatable if i want + const priority = ['System', 'CPU', 'RAM']; + const priorityMap = {}; + priority.forEach((name, idx) => (priorityMap[name] = idx)); + + return [...rows].sort((a, b) => { + const aIdx = priorityMap.hasOwnProperty(a.name) + ? priorityMap[a.name] + : Infinity; // anything not in priority goes to the end + const bIdx = priorityMap.hasOwnProperty(b.name) + ? priorityMap[b.name] + : Infinity; + return aIdx - bIdx; + }); +} + /* ------------------------------------------------------------ 4. Build an HTML table from an array of objects ------------------------------------------------------------ */