update redis.js to order table rows
This commit is contained in:
@ -16,9 +16,10 @@ function safeSetText(id, txt) {
|
|||||||
if (el) el.textContent = txt;
|
if (el) el.textContent = txt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------------
|
||||||
2. Table rendering - the table remains a <table>
|
2. Table rendering - now orders rows before building the table
|
||||||
------------------------------------------------------------ */
|
------------------------------------------------------------------ */
|
||||||
|
// helper function for table row ordering
|
||||||
function renderStatsTable(data) {
|
function renderStatsTable(data) {
|
||||||
renderGenericTable('host_metrics', data, 'No Stats available');
|
renderGenericTable('host_metrics', data, 'No Stats available');
|
||||||
}
|
}
|
||||||
@ -33,15 +34,18 @@ function renderGenericTable(containerId, data, emptyMsg) {
|
|||||||
/* Merge rows by name (new logic) */
|
/* Merge rows by name (new logic) */
|
||||||
const mergedData = mergeRowsByName(data);
|
const mergedData = mergeRowsByName(data);
|
||||||
|
|
||||||
/* Build the table from the merged data */
|
/* Order the merged rows – priority first */
|
||||||
const table = buildTable(mergedData);
|
const orderedData = orderRows(mergedData);
|
||||||
table.id = 'host_metrics_table';
|
|
||||||
|
/* Build the table from the ordered data */
|
||||||
|
const table = buildTable(orderedData);
|
||||||
|
table.id = 'host_metrics_table';
|
||||||
container.innerHTML = '';
|
container.innerHTML = '';
|
||||||
container.appendChild(table);
|
container.appendChild(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
3. Merge rows by name (new logic)
|
3. Merge rows by name
|
||||||
------------------------------------------------------------ */
|
------------------------------------------------------------ */
|
||||||
function mergeRowsByName(data) {
|
function mergeRowsByName(data) {
|
||||||
const groups = {}; // { name: { types: [], metrics: [], props: [], values: [] } }
|
const groups = {}; // { name: { types: [], metrics: [], props: [], values: [] } }
|
||||||
@ -81,6 +85,26 @@ function mergeRowsByName(data) {
|
|||||||
return merged;
|
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
|
4. Build an HTML table from an array of objects
|
||||||
------------------------------------------------------------ */
|
------------------------------------------------------------ */
|
||||||
|
|||||||
Reference in New Issue
Block a user