From 763f90e8e5af856cf2cae4b6cceb7daf2a9e5f78 Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Sat, 4 Apr 2026 20:01:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=A1=E7=AE=97=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/index.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/server/index.js b/server/index.js index d72c283..cf5d673 100644 --- a/server/index.js +++ b/server/index.js @@ -504,20 +504,18 @@ app.get('/api/metrics/overview', async (req, res) => { allServers = allServers.concat(m.servers); } - // --- 24h Traffic from DB --- + // --- 24h Traffic from DB (Integrating Bandwidth) --- try { - // Get the oldest record within 24h and the latest overall - const [oldest] = await db.query('SELECT rx_bytes, tx_bytes, timestamp FROM traffic_stats WHERE timestamp >= NOW() - INTERVAL 1 DAY ORDER BY timestamp ASC LIMIT 1'); - const [latest] = await db.query('SELECT rx_bytes, tx_bytes, timestamp FROM traffic_stats ORDER BY timestamp DESC LIMIT 1'); - - if (oldest.length > 0 && latest.length > 0) { - // Calculate difference. Handle counter resets (though unlikely for aggregated total) - traffic24hRx = latest[0].rx_bytes > oldest[0].rx_bytes ? (latest[0].rx_bytes - oldest[0].rx_bytes) : 0; - traffic24hTx = latest[0].tx_bytes > oldest[0].tx_bytes ? (latest[0].tx_bytes - oldest[0].tx_bytes) : 0; + // Each record represents a 5-second interval + const [sumRows] = await db.query('SELECT SUM(rx_bandwidth) as sumRx, SUM(tx_bandwidth) as sumTx FROM traffic_stats WHERE timestamp >= NOW() - INTERVAL 1 DAY'); + + if (sumRows.length > 0 && sumRows[0].sumRx !== null) { + // Total bytes = Sum of (bytes/sec) * 5 seconds + traffic24hRx = sumRows[0].sumRx * 5; + traffic24hTx = sumRows[0].sumTx * 5; } } catch (err) { - console.error('Error calculating 24h traffic from DB:', err); - // Fallback to what we got from Prometheus directly in each source if DB fails + console.error('Error calculating 24h traffic from DB integration:', err); } res.json({