计算移动到后端进行
This commit is contained in:
@@ -238,7 +238,9 @@ async function getOverviewMetrics(url, sourceName) {
|
||||
diskUsed: 0,
|
||||
netRx: 0,
|
||||
netTx: 0,
|
||||
up: false
|
||||
up: false,
|
||||
memPercent: 0,
|
||||
diskPercent: 0
|
||||
});
|
||||
}
|
||||
const inst = instances.get(token);
|
||||
@@ -308,6 +310,9 @@ async function getOverviewMetrics(url, sourceName) {
|
||||
if (!inst.up && (inst.cpuPercent > 0 || inst.memTotal > 0)) {
|
||||
inst.up = true;
|
||||
}
|
||||
// Calculate percentages on backend
|
||||
inst.memPercent = inst.memTotal > 0 ? (inst.memUsed / inst.memTotal * 100) : 0;
|
||||
inst.diskPercent = inst.diskTotal > 0 ? (inst.diskUsed / inst.diskTotal * 100) : 0;
|
||||
}
|
||||
|
||||
const allInstancesList = Array.from(instances.values());
|
||||
@@ -606,6 +611,9 @@ async function getServerDetails(baseUrl, instance, job) {
|
||||
percent: p.size > 0 ? ((p.size - p.free) / p.size * 100) : 0
|
||||
})).sort((a, b) => a.mountpoint.localeCompare(b.mountpoint));
|
||||
|
||||
// Calculate total disk size
|
||||
results.totalDiskSize = results.partitions.reduce((sum, p) => sum + (p.size || 0), 0);
|
||||
|
||||
delete results.partitions_size;
|
||||
delete results.partitions_free;
|
||||
|
||||
@@ -659,7 +667,11 @@ async function getServerHistory(baseUrl, instance, job, metric, range = '1h', st
|
||||
r.values.forEach(v => series[r.name].push(parseFloat(v[1])));
|
||||
});
|
||||
|
||||
return { timestamps, series };
|
||||
// Pre-calculate busy percentage: 100 - idle
|
||||
const idleValues = series.idle || [];
|
||||
const busyValues = idleValues.map(idleVal => Math.max(0, 100 - idleVal));
|
||||
|
||||
return { timestamps, series, values: busyValues };
|
||||
}
|
||||
|
||||
// Map metric keys to Prometheus expressions
|
||||
@@ -690,7 +702,30 @@ async function getServerHistory(baseUrl, instance, job, metric, range = '1h', st
|
||||
const tx = txResult.length > 0 ? txResult[0].values.map(v => parseFloat(v[1])) : new Array(timestamps.length).fill(0);
|
||||
const rx = rxResult.length > 0 ? rxResult[0].values.map(v => parseFloat(v[1])) : new Array(timestamps.length).fill(0);
|
||||
|
||||
return { timestamps, tx, rx };
|
||||
// Calculate statistics on backend
|
||||
let rxTotal = 0;
|
||||
let txTotal = 0;
|
||||
for (let i = 0; i < timestamps.length - 1; i++) {
|
||||
const duration = (timestamps[i+1] - timestamps[i]) / 1000;
|
||||
rxTotal += (rx[i] || 0) * duration;
|
||||
txTotal += (tx[i] || 0) * duration;
|
||||
}
|
||||
|
||||
const sortedTx = [...tx].sort((a, b) => a - b);
|
||||
const p95Idx = Math.floor(sortedTx.length * 0.95);
|
||||
const p95 = sortedTx.length > 0 ? sortedTx[p95Idx] : 0;
|
||||
|
||||
return {
|
||||
timestamps,
|
||||
tx,
|
||||
rx,
|
||||
stats: {
|
||||
rxTotal,
|
||||
txTotal,
|
||||
p95,
|
||||
total: rxTotal + txTotal
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const expr = metricMap[metric];
|
||||
|
||||
Reference in New Issue
Block a user