@@ -3,12 +3,13 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle, CardFooter }
33import { ChartConfig , ChartContainer , ChartTooltip } from '../ui/chart'
44import { formatBytes } from '@/utils/formatByte'
55import { useTranslation } from 'react-i18next'
6- import { useGetUsersUsage , Period } from '@/service/api'
6+ import { useGetUsersUsage , useGetUsage , Period } from '@/service/api'
77import { useMemo , useState } from 'react'
88import { SearchXIcon , TrendingUp , TrendingDown } from 'lucide-react'
99import { Select , SelectTrigger , SelectValue , SelectContent , SelectItem } from '../ui/select'
1010import { dateUtils } from '@/utils/dateFormatter'
1111import dayjs from '@/lib/dayjs'
12+ import { useAdmin } from '@/hooks/use-admin'
1213
1314interface PeriodOption {
1415 label : string
@@ -30,7 +31,7 @@ const PERIOD_KEYS = [
3031 { key : 'all' , period : 'day' as Period , allTime : true } ,
3132]
3233
33- const transformUsageData = ( apiData : any , periodOption : any ) => {
34+ const transformUsageData = ( apiData : any , periodOption : any , isNodeUsage : boolean = false ) => {
3435 if ( ! apiData ?. stats || ! Array . isArray ( apiData . stats ) ) {
3536 return [ ]
3637 }
@@ -54,11 +55,16 @@ const transformUsageData = (apiData: any, periodOption: any) => {
5455 }
5556 }
5657
58+ // Node usage has uplink + downlink, user usage has total_traffic
59+ const traffic = isNodeUsage
60+ ? ( stat . uplink || 0 ) + ( stat . downlink || 0 )
61+ : ( stat . total_traffic || 0 )
62+
5763 return {
5864 date : displayLabel ,
5965 fullDate : stat . period_start ,
6066 localFullDate : d . toISOString ( ) ,
61- traffic : stat . total_traffic || 0 ,
67+ traffic,
6268 }
6369 } )
6470}
@@ -182,6 +188,8 @@ function CustomBarTooltip({ active, payload, period }: TooltipProps<any, any> &
182188
183189const DataUsageChart = ( { admin_username } : { admin_username ?: string } ) => {
184190 const { t, i18n } = useTranslation ( )
191+ const { admin } = useAdmin ( )
192+ const is_sudo = admin ?. is_sudo || false
185193 const [ activeIndex , setActiveIndex ] = useState < number | null > ( null )
186194 const PERIOD_OPTIONS : PeriodOption [ ] = useMemo (
187195 ( ) => [
@@ -215,20 +223,43 @@ const DataUsageChart = ({ admin_username }: { admin_username?: string }) => {
215223 return { startDate : start . toISOString ( ) , endDate : now . toISOString ( ) }
216224 } , [ periodOption ] )
217225
218- const { data, isLoading } = useGetUsersUsage (
226+ // For sudo admins: fetch from nodes, for non-sudo: fetch from users
227+ const nodeUsageParams = useMemo ( ( ) => ( {
228+ period : periodOption . period ,
229+ start : startDate ,
230+ end : dateUtils . toDayjs ( endDate ) . endOf ( 'day' ) . toISOString ( ) ,
231+ } ) , [ periodOption . period , startDate , endDate ] )
232+
233+ const userUsageParams = useMemo ( ( ) => ( {
234+ ...( admin_username ? { admin : [ admin_username ] } : { } ) ,
235+ period : periodOption . period ,
236+ start : startDate ,
237+ end : dateUtils . toDayjs ( endDate ) . endOf ( 'day' ) . toISOString ( ) ,
238+ } ) , [ admin_username , periodOption . period , startDate , endDate ] )
239+
240+ const { data : nodeData , isLoading : isLoadingNodes } = useGetUsage (
241+ nodeUsageParams ,
219242 {
220- ... ( admin_username ? { admin : [ admin_username ] } : { } ) ,
221- period : periodOption . period ,
222- start : startDate ,
223- end : dateUtils . toDayjs ( endDate ) . endOf ( 'day' ) . toISOString ( ) ,
243+ query : {
244+ enabled : is_sudo ,
245+ refetchInterval : 1000 * 60 * 5 ,
246+ } ,
224247 } ,
248+ )
249+
250+ const { data : userData , isLoading : isLoadingUsers } = useGetUsersUsage (
251+ userUsageParams ,
225252 {
226253 query : {
254+ enabled : ! is_sudo ,
227255 refetchInterval : 1000 * 60 * 5 ,
228256 } ,
229257 } ,
230258 )
231259
260+ const data = is_sudo ? nodeData : userData
261+ const isLoading = is_sudo ? isLoadingNodes : isLoadingUsers
262+
232263 // Extract correct stats array from grouped or flat API response (like CostumeBarChart)
233264 let statsArr : any [ ] = [ ]
234265 if ( data ?. stats ) {
@@ -240,7 +271,7 @@ const DataUsageChart = ({ admin_username }: { admin_username?: string }) => {
240271 }
241272 }
242273
243- const chartData = useMemo ( ( ) => transformUsageData ( { stats : statsArr } , periodOption ) , [ statsArr , periodOption ] )
274+ const chartData = useMemo ( ( ) => transformUsageData ( { stats : statsArr } , periodOption , is_sudo ) , [ statsArr , periodOption , is_sudo ] )
244275
245276 // Calculate trend
246277 const trend = useMemo ( ( ) => {
0 commit comments