forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
417 lines (391 loc) · 13.1 KB
/
Copy pathindex.tsx
File metadata and controls
417 lines (391 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { getRouteApi, useNavigate } from '@tanstack/react-router'
import { Eye, EyeOff } from 'lucide-react'
import { useState, useCallback, useMemo, lazy, Suspense } from 'react'
import { useTranslation } from 'react-i18next'
import { SectionPageLayout } from '@/components/layout'
import { FadeIn } from '@/components/page-transition'
import { Button } from '@/components/ui/button'
import { Skeleton } from '@/components/ui/skeleton'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from '@/components/ui/tooltip'
import { ROLE } from '@/lib/roles'
import { cn } from '@/lib/utils'
import { useAuthStore } from '@/stores/auth-store'
import { ModelsChartPreferences } from './components/models/models-chart-preferences'
import { ModelsFilter } from './components/models/models-filter-dialog'
import { OverviewDashboard } from './components/overview/overview-dashboard'
import { DEFAULT_TIME_GRANULARITY } from './constants'
import {
buildDefaultDashboardFilters,
getDefaultDays,
getSavedChartPreferences,
getSavedGranularity,
saveChartPreferences,
} from './lib'
import {
type DashboardSectionId,
DASHBOARD_DEFAULT_SECTION,
DASHBOARD_SECTION_IDS,
} from './section-registry'
import type {
DashboardChartPreferences,
DashboardFilters,
QuotaDataItem,
UserChartsFilters,
} from './types'
const route = getRouteApi('/_authenticated/dashboard/$section')
const LOG_STAT_CARD_FALLBACK_KEYS = [
'count',
'quota',
'tokens',
'average-rpm',
'average-tpm',
] as const
const PERFORMANCE_METRIC_FALLBACK_KEYS = [
'success-rate',
'average-latency',
'throughput',
] as const
const PERFORMANCE_MODEL_FALLBACK_KEYS = [
'primary-model',
'secondary-model',
] as const
const LazyLogStatCards = lazy(() =>
import('./components/models/log-stat-cards').then((m) => ({
default: m.LogStatCards,
}))
)
const LazyModelCharts = lazy(() =>
import('./components/models/model-charts').then((m) => ({
default: m.ModelCharts,
}))
)
const LazyConsumptionDistributionChart = lazy(() =>
import('./components/models/consumption-distribution-chart').then((m) => ({
default: m.ConsumptionDistributionChart,
}))
)
const LazyPerformanceOverview = lazy(() =>
import('./components/models/performance-overview').then((m) => ({
default: m.PerformanceOverview,
}))
)
const LazyUserCharts = lazy(() =>
import('./components/users/user-charts').then((m) => ({
default: m.UserCharts,
}))
)
const LazyFlowCharts = lazy(() =>
import('./components/flow/flow-charts').then((m) => ({
default: m.FlowCharts,
}))
)
function LogStatCardsFallback() {
return (
<div className='overflow-hidden rounded-lg border'>
<div className='divide-border/60 grid grid-cols-2 divide-x sm:grid-cols-3 lg:grid-cols-5'>
{LOG_STAT_CARD_FALLBACK_KEYS.map((key, index) => (
<div
key={key}
className={cn(
'px-2.5 py-1.5 sm:px-5 sm:py-4',
index === LOG_STAT_CARD_FALLBACK_KEYS.length - 1 &&
'col-span-2 sm:col-span-1'
)}
>
<div className='flex items-center gap-1.5 sm:gap-2'>
<Skeleton className='size-4 rounded-sm sm:size-7 sm:rounded-md' />
<Skeleton className='h-4 w-16' />
</div>
<Skeleton className='mt-1 h-5 w-16 sm:mt-2 sm:h-7 sm:w-20' />
<Skeleton className='mt-1 hidden h-3.5 w-28 md:block' />
</div>
))}
</div>
</div>
)
}
function ModelChartsFallback() {
return (
<div className='overflow-hidden rounded-lg border'>
<div className='flex items-center justify-between border-b px-4 py-3 sm:px-5'>
<Skeleton className='h-5 w-32' />
<Skeleton className='h-8 w-72' />
</div>
<div className='h-96 p-2'>
<Skeleton className='h-full w-full' />
</div>
</div>
)
}
function PerformanceOverviewFallback() {
return (
<div className='overflow-hidden rounded-lg border'>
<div className='flex flex-wrap items-center gap-x-6 gap-y-2 px-4 py-3 sm:px-5'>
<div className='flex items-center gap-2'>
<Skeleton className='h-4 w-24' />
</div>
{PERFORMANCE_METRIC_FALLBACK_KEYS.map((key) => (
<div key={key} className='flex items-center gap-1.5'>
<Skeleton className='h-3 w-14' />
<Skeleton className='h-4 w-16' />
</div>
))}
<div className='ml-auto flex items-center gap-2'>
{PERFORMANCE_MODEL_FALLBACK_KEYS.map((key) => (
<Skeleton key={key} className='h-5 w-28 rounded-full' />
))}
</div>
</div>
</div>
)
}
const SECTION_META: Record<DashboardSectionId, { titleKey: string }> = {
overview: {
titleKey: 'Overview',
},
models: {
titleKey: 'Model Call Analytics',
},
flow: {
titleKey: 'Flow',
},
users: {
titleKey: 'User Analytics',
},
}
export function Dashboard() {
const { t } = useTranslation()
const navigate = useNavigate()
const params = route.useParams()
const userRole = useAuthStore((state) => state.auth.user?.role)
const activeSection = (params.section ??
DASHBOARD_DEFAULT_SECTION) as DashboardSectionId
const [modelData, setModelData] = useState<QuotaDataItem[]>([])
const [dataLoading, setDataLoading] = useState(false)
const [chartPreferences, setChartPreferences] =
useState<DashboardChartPreferences>(() => getSavedChartPreferences())
const [modelFilters, setModelFilters] = useState<DashboardFilters>(() =>
buildDefaultDashboardFilters(getSavedChartPreferences())
)
const [userChartsFilters, setUserChartsFilters] = useState<UserChartsFilters>(
() => {
const granularity = getSavedGranularity()
return {
timeGranularity: granularity,
selectedRange: getDefaultDays(granularity),
topUserLimit: 10,
}
}
)
const [flowSensitiveVisible, setFlowSensitiveVisible] = useState(true)
const handleFilterChange = useCallback((filters: DashboardFilters) => {
setModelFilters(filters)
}, [])
const handleResetFilters = useCallback(() => {
setModelFilters(buildDefaultDashboardFilters(chartPreferences))
}, [chartPreferences])
const handleDataUpdate = useCallback(
(data: QuotaDataItem[], loading: boolean) => {
setModelData(data)
setDataLoading(loading)
},
[]
)
const handleChartPreferencesChange = useCallback(
(preferences: DashboardChartPreferences) => {
setChartPreferences(preferences)
setModelFilters(buildDefaultDashboardFilters(preferences))
saveChartPreferences(preferences)
},
[]
)
const meta = SECTION_META[activeSection] ?? SECTION_META.overview
const isAdmin = Boolean(userRole && userRole >= ROLE.ADMIN)
const visibleSections = useMemo(
() =>
DASHBOARD_SECTION_IDS.filter(
(section) => section !== 'overview' && (section !== 'users' || isAdmin)
),
[isAdmin]
)
const handleSectionChange = useCallback(
(section: string) => {
void navigate({
to: '/dashboard/$section',
params: { section: section as DashboardSectionId },
})
},
[navigate]
)
const showSectionTabs =
activeSection !== 'overview' && visibleSections.length > 1
const modelActions =
activeSection === 'models' ? (
<>
<ModelsChartPreferences
preferences={chartPreferences}
onPreferencesChange={handleChartPreferencesChange}
/>
<ModelsFilter
preferences={chartPreferences}
currentFilters={modelFilters}
onFilterChange={handleFilterChange}
onReset={handleResetFilters}
/>
</>
) : null
const flowActions =
activeSection === 'flow' ? (
<>
<Tooltip>
<TooltipTrigger
render={
<Button
variant='ghost'
size='icon'
onClick={() => setFlowSensitiveVisible((prev) => !prev)}
aria-label={
flowSensitiveVisible
? t('Hide sensitive data')
: t('Show sensitive data')
}
className='text-muted-foreground hover:text-foreground size-8'
/>
}
>
{flowSensitiveVisible ? <Eye /> : <EyeOff />}
</TooltipTrigger>
<TooltipContent>
{flowSensitiveVisible
? t('Hide sensitive data')
: t('Show sensitive data')}
</TooltipContent>
</Tooltip>
<ModelsFilter
preferences={chartPreferences}
currentFilters={modelFilters}
onFilterChange={handleFilterChange}
onReset={handleResetFilters}
titleKey='Flow Filters'
descriptionKey='Filter the traffic flow view by time range and user.'
/>
</>
) : null
const sectionActions = modelActions ?? flowActions
return (
<SectionPageLayout>
<SectionPageLayout.Title>{t(meta.titleKey)}</SectionPageLayout.Title>
<SectionPageLayout.Content>
<div className='space-y-3 sm:space-y-4'>
{activeSection !== 'overview' && (
<div className='flex flex-wrap items-center justify-between gap-1.5 sm:gap-2'>
{showSectionTabs ? (
<Tabs value={activeSection} onValueChange={handleSectionChange}>
<TabsList className='max-w-full flex-wrap justify-start group-data-horizontal/tabs:h-auto'>
{visibleSections.map((section) => (
<TabsTrigger key={section} value={section}>
{t(SECTION_META[section].titleKey)}
</TabsTrigger>
))}
</TabsList>
</Tabs>
) : (
<div />
)}
{sectionActions != null && (
<div className='flex shrink-0 flex-wrap items-center gap-1.5 sm:gap-2'>
{sectionActions}
</div>
)}
</div>
)}
{activeSection === 'overview' && <OverviewDashboard />}
{activeSection === 'models' && (
<>
<FadeIn>
<Suspense fallback={<LogStatCardsFallback />}>
<LazyLogStatCards
filters={modelFilters}
onDataUpdate={handleDataUpdate}
/>
</Suspense>
</FadeIn>
{isAdmin && (
<FadeIn delay={0.05}>
<Suspense fallback={<PerformanceOverviewFallback />}>
<LazyPerformanceOverview />
</Suspense>
</FadeIn>
)}
<FadeIn delay={0.1}>
<Suspense fallback={<ModelChartsFallback />}>
<LazyConsumptionDistributionChart
data={modelData}
loading={dataLoading}
defaultChartType={
chartPreferences.consumptionDistributionChart
}
timeGranularity={
modelFilters.time_granularity || DEFAULT_TIME_GRANULARITY
}
/>
</Suspense>
</FadeIn>
<FadeIn delay={0.15}>
<Suspense fallback={<ModelChartsFallback />}>
<LazyModelCharts
data={modelData}
loading={dataLoading}
defaultChartTab={chartPreferences.modelAnalyticsChart}
timeGranularity={
modelFilters.time_granularity || DEFAULT_TIME_GRANULARITY
}
/>
</Suspense>
</FadeIn>
</>
)}
{activeSection === 'users' && (
<FadeIn>
<Suspense fallback={<ModelChartsFallback />}>
<LazyUserCharts
filters={userChartsFilters}
onFiltersChange={setUserChartsFilters}
/>
</Suspense>
</FadeIn>
)}
{activeSection === 'flow' && (
<FadeIn>
<Suspense fallback={<ModelChartsFallback />}>
<LazyFlowCharts
filters={modelFilters}
sensitiveVisible={flowSensitiveVisible}
/>
</Suspense>
</FadeIn>
)}
</div>
</SectionPageLayout.Content>
</SectionPageLayout>
)
}