import { IconActivity as Activity, IconClock as Clock, IconCpu as Cpu, IconDatabase as HardDrive, } from '@tabler/icons-react'; import { useQuery } from '@tanstack/react-query'; import { Area, AreaChart, Bar, BarChart, Cell, XAxis, YAxis } from 'recharts'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { type ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent, } from '@/components/ui/chart'; import { Skeleton } from '@/components/ui/skeleton'; import { getAuthHeaders } from '@/lib/api'; interface PerformanceData { server: { uptimeSeconds: number; memoryMB: { rss: number; heapUsed: number; heapTotal: number }; disk: { usedGB: number; totalGB: number; usedPercent: number } | null; nodeVersion: string; platform: string; }; services: { healthy: number; total: number; avgLatencyMs: number; items: Array<{ name: string; latencyMs: number; status: 'healthy' | 'unhealthy' | 'unknown'; }>; }; } interface StatsResponse { users: { weeklySignups: Array<{ week: string; count: number }>; }; } function formatUptime(seconds: number): string { const days = Math.floor(seconds / 86400); const hours = Math.floor((seconds % 86400) / 3600); const minutes = Math.floor((seconds % 3600) / 60); return `${days}d ${hours}h ${minutes}m`; } function getLatencyColor(ms: number): string { if (ms < 50) return 'var(--chart-4)'; if (ms < 200) return 'var(--chart-2)'; return 'var(--chart-5)'; } const latencyConfig = { latencyMs: { label: 'Latency', color: 'var(--chart-1)' }, } satisfies ChartConfig; const signupsConfig = { count: { label: 'Signups', color: 'var(--chart-1)' }, } satisfies ChartConfig; export function PerformanceMetrics() { const { data: perfData, isLoading: perfLoading } = useQuery({ queryKey: ['admin', 'performance'], queryFn: async () => { const headers = await getAuthHeaders(); const res = await fetch('/api/v1/admin/performance', { headers }); if (!res.ok) throw new Error('Failed to fetch performance data'); return res.json(); }, refetchInterval: 30_000, }); const { data: statsData, isLoading: statsLoading } = useQuery({ queryKey: ['admin', 'stats'], queryFn: async () => { const headers = await getAuthHeaders(); const res = await fetch('/api/v1/admin/stats', { headers }); if (!res.ok) throw new Error('Failed to fetch stats'); return res.json(); }, refetchInterval: 30_000, }); const sortedServices = perfData ? [...perfData.services.items].sort((a, b) => b.latencyMs - a.latencyMs) : []; const weeklySignups = statsData?.users?.weeklySignups ?? []; return (
{/* KPI Cards */}
Uptime {perfLoading ? ( ) : (

{perfData ? formatUptime(perfData.server.uptimeSeconds) : '--'}

)}
Avg API Latency {perfLoading ? ( ) : (

{perfData ? `${Math.round(perfData.services.avgLatencyMs)}ms` : '--'}

)}
Memory Used {perfLoading ? ( ) : (

{perfData ? `${Math.round(perfData.server.memoryMB.heapUsed)} MB` : '--'}

)}
Disk Used {perfLoading ? ( ) : (

{perfData?.server.disk ? ( <> {perfData.server.disk.usedPercent}% {perfData.server.disk.usedGB} / {perfData.server.disk.totalGB} GB ) : ( '--' )}

)}
{/* Service Latency Chart */} Service Latency {perfLoading ? ( ) : sortedServices.length > 0 ? ( } /> {sortedServices.map((entry) => ( ))} ) : (

No service data available.

)}
{/* User Growth Chart */} User Growth {statsLoading ? ( ) : weeklySignups.length > 0 ? ( } /> ) : (

No signup data available.

)}
); }