利用 Prometheus 自身的数据持久化能力保证统计的准确性
This commit is contained in:
@@ -188,8 +188,6 @@ async function getOverviewMetrics(url, sourceName) {
|
||||
diskFreeResult,
|
||||
netRxResult,
|
||||
netTxResult,
|
||||
traffic24hRxResult,
|
||||
traffic24hTxResult,
|
||||
targetsResult
|
||||
] = await Promise.all([
|
||||
// CPU usage per instance: 1 - avg idle
|
||||
@@ -208,14 +206,13 @@ async function getOverviewMetrics(url, sourceName) {
|
||||
query(url, 'sum by (instance, job) (rate(node_network_receive_bytes_total{device!~"lo|veth.*|docker.*|br-.*"}[1m]))').catch(() => []),
|
||||
// Network transmit rate (bytes/sec)
|
||||
query(url, 'sum by (instance, job) (rate(node_network_transmit_bytes_total{device!~"lo|veth.*|docker.*|br-.*"}[1m]))').catch(() => []),
|
||||
// Total traffic received in last 24h
|
||||
query(url, 'sum by (instance, job) (increase(node_network_receive_bytes_total{device!~"lo|veth.*|docker.*|br-.*"}[24h]))').catch(() => []),
|
||||
// Total traffic transmitted in last 24h
|
||||
query(url, 'sum by (instance, job) (increase(node_network_transmit_bytes_total{device!~"lo|veth.*|docker.*|br-.*"}[24h]))').catch(() => []),
|
||||
// Targets status from /api/v1/targets
|
||||
getTargets(url).catch(() => [])
|
||||
]);
|
||||
|
||||
// Fetch 24h detailed traffic using the A*duration logic
|
||||
const traffic24hSum = await get24hTrafficSum(url).catch(() => ({ rx: 0, tx: 0 }));
|
||||
|
||||
// Build per-instance data map
|
||||
const instances = new Map();
|
||||
|
||||
@@ -334,13 +331,9 @@ async function getOverviewMetrics(url, sourceName) {
|
||||
totalNetTx += inst.netTx;
|
||||
}
|
||||
|
||||
// Parse 24h traffic
|
||||
for (const r of traffic24hRxResult) {
|
||||
totalTraffic24hRx += parseFloat(r.value[1]) || 0;
|
||||
}
|
||||
for (const r of traffic24hTxResult) {
|
||||
totalTraffic24hTx += parseFloat(r.value[1]) || 0;
|
||||
}
|
||||
// Use the pre-calculated 24h traffic
|
||||
totalTraffic24hRx = traffic24hSum.rx;
|
||||
totalTraffic24hTx = traffic24hSum.tx;
|
||||
|
||||
return {
|
||||
totalServers: allInstancesList.length,
|
||||
@@ -377,13 +370,53 @@ async function getOverviewMetrics(url, sourceName) {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get network traffic history (past 24h, 15-min intervals)
|
||||
* Calculate total traffic from bandwidth data points using the A*duration logic
|
||||
*/
|
||||
function calculateTrafficFromHistory(values) {
|
||||
if (!values || values.length < 2) return 0;
|
||||
|
||||
let totalBytes = 0;
|
||||
for (let i = 0; i < values.length - 1; i++) {
|
||||
const [tsA, valA] = values[i];
|
||||
const [tsB] = values[i+1];
|
||||
const duration = tsB - tsA;
|
||||
totalBytes += parseFloat(valA) * duration;
|
||||
}
|
||||
return totalBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total traffic for the past 24h by fetching all points and integrating
|
||||
*/
|
||||
async function get24hTrafficSum(url) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const start = now - 86400;
|
||||
const step = 60; // 1-minute points for calculation
|
||||
|
||||
const [rxResult, txResult] = await Promise.all([
|
||||
queryRange(url, 'sum(rate(node_network_receive_bytes_total{device!~"lo|veth.*|docker.*|br-.*"}[1m]))', start, now, step).catch(() => []),
|
||||
queryRange(url, 'sum(rate(node_network_transmit_bytes_total{device!~"lo|veth.*|docker.*|br-.*"}[1m]))', 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)
|
||||
*/
|
||||
async function getNetworkHistory(url) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const start = now - 86400; // 24h ago
|
||||
const step = 900; // 15 minutes
|
||||
const step = 300; // 5 minutes for better resolution on chart
|
||||
|
||||
const [rxResult, txResult] = await Promise.all([
|
||||
queryRange(url,
|
||||
@@ -645,6 +678,7 @@ module.exports = {
|
||||
queryRange,
|
||||
getTargets,
|
||||
getOverviewMetrics,
|
||||
get24hTrafficSum,
|
||||
getNetworkHistory,
|
||||
mergeNetworkHistories,
|
||||
getCpuHistory,
|
||||
|
||||
Reference in New Issue
Block a user