优化布局

This commit is contained in:
CN-JS-HuiBai
2026-04-05 19:45:05 +08:00
parent 5e40c19ef1
commit 8c25f1735d
3 changed files with 47 additions and 2 deletions

View File

@@ -407,6 +407,32 @@ async function get24hTrafficSum(url) {
};
}
/**
* Get total traffic for a specific server in the past 24h
*/
async function get24hServerTrafficSum(url, instance, job) {
const node = resolveToken(instance);
const now = Math.floor(Date.now() / 1000);
const start = now - 86400;
const step = 60;
const rxExpr = `sum(rate(node_network_receive_bytes_total{instance="${node}",job="${job}",device!~'tap.*|veth.*|br.*|docker.*|virbr*|podman.*|lo.*|vmbr.*|fwbr.|ip.*|gre.*|virbr.*|vnet.*'}[1m]))`;
const txExpr = `sum(rate(node_network_transmit_bytes_total{instance="${node}",job="${job}",device!~'tap.*|veth.*|br.*|docker.*|virbr*|podman.*|lo.*|vmbr.*|fwbr.|ip.*|gre.*|virbr.*|vnet.*'}[1m]))`;
const [rxResult, txResult] = await Promise.all([
queryRange(url, rxExpr, start, now, step).catch(() => []),
queryRange(url, txExpr, start, now, step).catch(() => [])
]);
const rxValues = rxResult.length > 0 ? rxResult[0].values : [];
const txValues = txResult.length > 0 ? txResult[0].values : [];
return {
rx: calculateTrafficFromHistory(rxValues),
tx: calculateTrafficFromHistory(txValues)
};
}
/**
* Get network traffic history (past 24h, 5-min intervals for chart)
*/
@@ -583,6 +609,15 @@ async function getServerDetails(baseUrl, instance, job) {
delete results.partitions_size;
delete results.partitions_free;
// Add 24h traffic sum for this specific server
try {
const traffic24h = await get24hServerTrafficSum(baseUrl, instance, job);
results.traffic24h = traffic24h;
} catch (e) {
console.error(`[Prometheus] Error fetching 24h traffic for ${node}:`, e.message);
results.traffic24h = { rx: 0, tx: 0 };
}
return results;
}