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 { apiFetch } 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 res = await apiFetch('/api/v1/admin/performance'); 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 res = await apiFetch('/api/v1/admin/stats'); if (!res.ok) throw new Error('Failed to fetch stats'); return res.json(); }, refetchInterval: 30_000, }); // Only services with a measured round trip: an unhealthy probe's number is // its timeout, not a latency. const sortedServices = perfData ? perfData.services.items .filter((s) => s.status === 'healthy') .sort((a, b) => b.latencyMs - a.latencyMs) : []; // Fit the y-axis to the longest label (12px text, about 7px a character). const yAxisWidth = Math.max(48, ...sortedServices.map((s) => s.name.length * 7 + 12)); const weeklySignups = statsData?.users?.weeklySignups ?? []; return (
{/* KPI Cards */}
Uptime {perfLoading ? ( ) : (

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

)}
Avg service latency {perfLoading ? ( ) : (

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

)}
App memory {perfLoading ? ( ) : ( // The app process's resident memory (RSS), not the whole stack: // Postgres and Traefik are separate processes.

{perfData ? ( <> {`${perfData.server.memoryMB.rss} MB`} {`${perfData.server.memoryMB.heapUsed} MB heap`} ) : ( '--' )}

)}
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.

)}
); }