add string returns for metrics and properties

This commit is contained in:
2026-03-16 09:17:44 -07:00
parent adb1387693
commit 61421305ed
16 changed files with 524 additions and 390 deletions

View File

@ -21,6 +21,7 @@ function safeSetText(id, txt) {
------------------------------------------------------------------ */
// helper function for table row ordering
function renderStatsTable(data) {
socket.emit('tableRendered');
renderGenericTable('host_metrics', data, 'No Stats available');
}
@ -48,59 +49,56 @@ function renderGenericTable(containerId, data, emptyMsg) {
3. Merge rows by name
------------------------------------------------------------ */
function mergeRowsByName(data) {
const groups = {}; // { name: { types: [], metrics: [], props: [], values: [] } }
const groups = {}; // { source: { ... } }
data.forEach(row => {
const name = row.name;
if (!name) return; // ignore rows without a name
if (!groups[name]) {
groups[name] = { types: [], metrics: [], props: [], values: [] };
const source = row.Source; // <-- changed
if (!source) return;
if (!groups[source]) {
groups[source] = { Metric: [], Data: [], Property: [], Value: [] };
}
// Metric rows - contain type + metric
if ('type' in row && 'metric' in row) {
groups[name].types.push(row.type);
groups[name].metrics.push(row.metric);
}
// Property rows - contain property + value
else if ('property' in row && 'value' in row) {
groups[name].props.push(row.property);
groups[name].values.push(row.value);
if ('Metric' in row && 'Data' in row) {
groups[source].Metric.push(row.Metric);
groups[source].Data.push(row.Data);
} else if ('Property' in row && 'Value' in row) {
groups[source].Property.push(row.Property);
groups[source].Value.push(row.Value);
}
});
// Convert each group into a single row object
const merged = [];
Object.entries(groups).forEach(([name, grp]) => {
Object.entries(groups).forEach(([source, grp]) => {
merged.push({
name,
type: grp.types, // array of types
metric: grp.metrics, // array of metrics
property: grp.props, // array of property names
value: grp.values, // array of property values
Source: source, // <-- keep the original key
Metric: grp.Metric,
Data: grp.Data,
Property: grp.Property,
Value: grp.Value
});
});
return merged;
}
/* ------------------------------------------------------------------
3b. Order rows - put “System”, “CPU”, “RAM” first
------------------------------------------------------------------ */
// 3b. Order rows put “System”, “CPU”, “RAM” first
function orderRows(rows) {
// this should be updatable if i want
// Priority list can be updated later
const priority = ['System', 'CPU', 'RAM'];
const priorityMap = {};
priority.forEach((name, idx) => (priorityMap[name] = idx));
// Map source → priority index
const priorityMap = {};
priority.forEach((src, idx) => {
priorityMap[src] = idx;
});
// Stable sort: keep original position if priorities are equal
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]
const aIdx = priorityMap.hasOwnProperty(a.Source)
? priorityMap[a.Source]
: Infinity; // anything not in priority goes to the end
const bIdx = priorityMap.hasOwnProperty(b.Source)
? priorityMap[b.Source]
: Infinity;
// If both have the same priority (or both Infinity), keep original order
return aIdx - bIdx;
});
}
@ -109,7 +107,7 @@ function orderRows(rows) {
4. Build an HTML table from an array of objects
------------------------------------------------------------ */
function buildTable(data) {
const cols = ['name', 'type', 'metric', 'property', 'value']; // explicit order
const cols = ['Source', 'Property', 'Value', 'Metric', 'Data']; // explicit order
const table = document.createElement('table');
// Header