/** * CPU Monitor Component * * Displays CPU usage percentage with historical chart and event loop lag indicator. */ import { useMemo } from 'react'; import { Cpu, Activity, AlertTriangle } from 'lucide-react'; import { cn } from '@/lib/utils'; import type { CPUDataPoint, ServerCPUMetrics } from '@automaker/types'; interface CPUMonitorProps { history: CPUDataPoint[]; current: ServerCPUMetrics | null; eventLoopLag?: number; className?: string; } /** * Simple sparkline chart for CPU data */ function CPUSparkline({ data, className }: { data: CPUDataPoint[]; className?: string }) { const pathD = useMemo(() => { if (data.length < 2) { return ''; } const w = 200; const h = 40; const padding = 2; // CPU percentage is 0-100, but we'll use 0-100 as our range const points = data.map((d, i) => { const x = (i / (data.length - 1)) * (w - padding * 2) + padding; const y = h - padding - (d.percentage / 100) * (h - padding * 2); return `${x},${y}`; }); return `M ${points.join(' L ')}`; }, [data]); if (data.length < 2) { return (
Collecting data...
); } return ( ); } /** * CPU usage gauge */ function CPUGauge({ percentage }: { percentage: number }) { const isHigh = percentage > 60; const isCritical = percentage > 80; return (
{/* Background circle */} {/* Center text */}
{percentage.toFixed(0)}%
); } /** * Event loop lag indicator */ function EventLoopLag({ lag }: { lag?: number }) { if (lag === undefined) { return null; } const isBlocked = lag > 50; const isSevere = lag > 100; return (
{isSevere ? : } Event Loop: {lag.toFixed(0)}ms
); } export function CPUMonitor({ history, current, eventLoopLag, className }: CPUMonitorProps) { const percentage = current?.percentage ?? 0; return (
{/* Header */}
CPU
{/* Main content */}
{/* Gauge */} {/* Sparkline */}
{/* Details */} {current && (
User: {(current.user / 1000).toFixed(1)}ms
System: {(current.system / 1000).toFixed(1)}ms
)}
); }