Skip to content

Commit d91293f

Browse files
committed
fix(node-stats-modal): optimize uplink and downlink calculations and improve total usage logic
1 parent 4436c12 commit d91293f

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

dashboard/src/components/dialogs/node-stats-modal.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Card, CardContent } from '../ui/card'
33
import { Badge } from '../ui/badge'
44
import { Button } from '../ui/button'
55
import { useTranslation } from 'react-i18next'
6-
import { formatBytes } from '@/utils/formatByte'
6+
import { formatBytes, gbToBytes } from '@/utils/formatByte'
77
import { Upload, Download, Calendar, Activity, ChevronLeft, ChevronRight } from 'lucide-react'
88
import { dateUtils } from '@/utils/dateFormatter'
99
import useDirDetection from '@/hooks/use-dir-detection'
@@ -143,15 +143,6 @@ const NodeStatsModal = ({ open, onClose, data, chartConfig, period, allChartData
143143
const canGoLeft = isRTL ? canGoNext : canGoPrevious
144144
const canGoRight = isRTL ? canGoPrevious : canGoNext
145145

146-
// Calculate total uplink and downlink
147-
const totalUplink = Object.keys(data)
148-
.filter(key => key.startsWith('_uplink_'))
149-
.reduce((sum, key) => sum + (data[key] || 0), 0)
150-
151-
const totalDownlink = Object.keys(data)
152-
.filter(key => key.startsWith('_downlink_'))
153-
.reduce((sum, key) => sum + (data[key] || 0), 0)
154-
155146
// Get nodes with usage > 0
156147
const activeNodes = Object.keys(data)
157148
.filter(key => !key.startsWith('_') && key !== 'time' && key !== '_period_start' && (data[key] || 0) > 0)
@@ -164,6 +155,14 @@ const NodeStatsModal = ({ open, onClose, data, chartConfig, period, allChartData
164155
}))
165156
.sort((a, b) => b.usage - a.usage) // Sort by usage descending
166157

158+
// Calculate total uplink and downlink from activeNodes
159+
const totalUplink = activeNodes.reduce((sum, node) => sum + (node.uplink || 0), 0)
160+
const totalDownlink = activeNodes.reduce((sum, node) => sum + (node.downlink || 0), 0)
161+
// Calculate total usage - if uplink/downlink are available use them, otherwise convert usage from GB to bytes
162+
const totalUsage = totalUplink + totalDownlink > 0
163+
? totalUplink + totalDownlink
164+
: activeNodes.reduce((sum, node) => sum + (gbToBytes(node.usage) || 0), 0)
165+
167166
return (
168167
<Dialog open={open} onOpenChange={onClose}>
169168
<DialogContent className="max-w-md sm:max-w-lg md:max-w-xl" dir={dir}>
@@ -200,7 +199,7 @@ const NodeStatsModal = ({ open, onClose, data, chartConfig, period, allChartData
200199
</span>
201200
</div>
202201
<Badge dir="ltr" variant="secondary" className="px-2 py-1 font-mono text-xs sm:text-sm">
203-
{formatBytes(totalUplink + totalDownlink)}
202+
{formatBytes(totalUsage)}
204203
</Badge>
205204
</div>
206205

0 commit comments

Comments
 (0)