diff --git a/AppImage/components/network-metrics.tsx b/AppImage/components/network-metrics.tsx index d1c105a..3b02a6e 100644 --- a/AppImage/components/network-metrics.tsx +++ b/AppImage/components/network-metrics.tsx @@ -155,6 +155,11 @@ export function NetworkMetrics() { const [timeframe, setTimeframe] = useState<"hour" | "day" | "week" | "month" | "year">("day") const [networkTotals, setNetworkTotals] = useState<{ received: number; sent: number }>({ received: 0, sent: 0 }) + const { data: interfaceHistoricalData } = useSWR(`/api/node/metrics?timeframe=${timeframe}`, fetcher, { + refreshInterval: 30000, + revalidateOnFocus: false, + }) + if (isLoading) { return (
@@ -186,6 +191,35 @@ export function NetworkMetrics() { ) } + const calculateInterfaceTraffic = (interfaceName: string) => { + if (!interfaceHistoricalData?.data) { + return { received: 0, sent: 0 } + } + + let totalReceived = 0 + let totalSent = 0 + + // Sum up the traffic from RRD data + for (const point of interfaceHistoricalData.data) { + const netinKey = `netin:${interfaceName}` + const netoutKey = `netout:${interfaceName}` + + if (point[netinKey] !== null && point[netinKey] !== undefined) { + // RRD data is in bytes/second, multiply by time interval to get total bytes + // Assuming 1 minute intervals for most timeframes + const interval = 60 // seconds + totalReceived += point[netinKey] * interval + } + + if (point[netoutKey] !== null && point[netoutKey] !== undefined) { + const interval = 60 + totalSent += point[netoutKey] * interval + } + } + + return { received: totalReceived, sent: totalSent } + } + const trafficInFormatted = formatStorage(networkTotals.received * 1024 * 1024 * 1024) // Convert GB to bytes const trafficOutFormatted = formatStorage(networkTotals.sent * 1024 * 1024 * 1024) const packetsRecvK = networkData.traffic.packets_recv ? (networkData.traffic.packets_recv / 1000).toFixed(0) : "0" @@ -244,7 +278,6 @@ export function NetworkMetrics() { ↑ {trafficOutFormatted}
-

Total data transferred

@@ -335,17 +368,6 @@ export function NetworkMetrics() { -
-
- Received: - ↓ {trafficInFormatted} -
-
- Sent: - ↑ {trafficOutFormatted} -
-

Data transferred in {getTimeframeLabel().toLowerCase()}

-
@@ -365,6 +387,7 @@ export function NetworkMetrics() {
{networkData.physical_interfaces.map((interface_, index) => { const typeBadge = getInterfaceTypeBadge(interface_.type) + const traffic = calculateInterfaceTraffic(interface_.name) return (
Traffic
- ↓ {formatBytes(interface_.bytes_recv)} + ↓ {formatBytes(traffic.received)} {" / "} - ↑ {formatBytes(interface_.bytes_sent)} + ↑ {formatBytes(traffic.sent)}
@@ -450,6 +473,7 @@ export function NetworkMetrics() {
{networkData.bridge_interfaces.map((interface_, index) => { const typeBadge = getInterfaceTypeBadge(interface_.type) + const traffic = calculateInterfaceTraffic(interface_.name) return (
Traffic
- ↓ {formatBytes(interface_.bytes_recv)} + ↓ {formatBytes(traffic.received)} {" / "} - ↑ {formatBytes(interface_.bytes_sent)} + ↑ {formatBytes(traffic.sent)}
@@ -565,6 +589,7 @@ export function NetworkMetrics() {
{networkData.vm_lxc_interfaces.map((interface_, index) => { const vmTypeBadge = getVMTypeBadge(interface_.vm_type) + const traffic = calculateInterfaceTraffic(interface_.name) return (
Traffic
- ↓ {formatBytes(interface_.bytes_recv)} + ↓ {formatBytes(traffic.received)} {" / "} - ↑ {formatBytes(interface_.bytes_sent)} + ↑ {formatBytes(traffic.sent)}