修复数据不显示的错误

This commit is contained in:
CN-JS-HuiBai
2026-04-04 18:46:29 +08:00
parent 7289418b1b
commit 0200a00844
2 changed files with 101 additions and 4 deletions

View File

@@ -402,6 +402,51 @@ function mergeCpuHistories(histories) {
};
}
/**
* Get aggregated traffic history range for preloading (past 24h, 5-min intervals)
*/
async function getTrafficHistoryRange(url) {
const now = Math.floor(Date.now() / 1000);
const start = now - 86400; // 24h ago
const step = 300; // 5 minutes
const queries = [
'sum(node_network_receive_bytes_total{device!~"lo|veth.*|docker.*|br-.*"})',
'sum(node_network_transmit_bytes_total{device!~"lo|veth.*|docker.*|br-.*"})',
'sum(rate(node_network_receive_bytes_total{device!~"lo|veth.*|docker.*|br-.*"}[5m]))',
'sum(rate(node_network_transmit_bytes_total{device!~"lo|veth.*|docker.*|br-.*"}[5m]))'
];
const results = await Promise.all(queries.map(q =>
queryRange(url, q, start, now, step).catch(() => [])
));
const rxBytesRes = results[0];
const txBytesRes = results[1];
const rxBWRes = results[2];
const txBWRes = results[3];
// Map results by timestamp
const dataMap = new Map();
const process = (res, field) => {
if (res.length > 0 && res[0].values) {
for (const [ts, val] of res[0].values) {
const entry = dataMap.get(ts) || { ts, rxBytes: 0, txBytes: 0, rxBW: 0, txBW: 0 };
entry[field] = parseFloat(val) || 0;
dataMap.set(ts, entry);
}
}
};
process(rxBytesRes, 'rxBytes');
process(txBytesRes, 'txBytes');
process(rxBWRes, 'rxBW');
process(txBWRes, 'txBW');
return Array.from(dataMap.values());
}
module.exports = {
testConnection,
query,
@@ -410,5 +455,6 @@ module.exports = {
getNetworkHistory,
mergeNetworkHistories,
getCpuHistory,
mergeCpuHistories
mergeCpuHistories,
getTrafficHistoryRange
};