import React, { useEffect, useState } from 'react'; interface Process { name: string; cpu: number; mem: number; pid: number; } interface SystemInfo { cpu: { manufacturer: string; brand: string; cores: number; physicalCores: number; speed: number; temperature: number | null; }; memory: { total: number; used: number; available: number; usedPercent: number; }; load: { avgLoad: number; currentLoad: number; currentLoadUser: number; currentLoadSystem: number; }; } const SystemMonitorWidget: React.FC<{ widgetId: string }> = () => { const [processes, setProcesses] = useState([]); const [systemInfo, setSystemInfo] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const fetchData = async () => { try { const [processesRes, systemRes] = await Promise.all([ fetch('http://localhost:3001/api/sys/processes'), fetch('http://localhost:3001/api/sys/system') ]); if (!processesRes.ok || !systemRes.ok) { throw new Error('Failed to fetch system data'); } const processesData = await processesRes.json(); const systemData = await systemRes.json(); setProcesses(processesData); setSystemInfo(systemData); setError(null); } catch (err) { setError(err instanceof Error ? err.message : 'Unknown error'); } finally { setLoading(false); } }; useEffect(() => { fetchData(); const interval = setInterval(fetchData, 2000); // Update every 2 seconds return () => clearInterval(interval); }, []); const formatBytes = (bytes: number) => { const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; if (bytes === 0) return '0 Bytes'; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i]; }; if (loading) { return (
Loading system data...
); } if (error) { return (
Failed to load system data
{error}
); } return (
{/* System Overview */}
CPU
{systemInfo?.load.currentLoad.toFixed(1)}%
{systemInfo?.cpu.temperature ? `${systemInfo.cpu.temperature}°C` : 'No temp sensor'}
Memory
{systemInfo?.memory.usedPercent.toFixed(1)}%
{formatBytes(systemInfo?.memory.used || 0)} / {formatBytes(systemInfo?.memory.total || 0)}
{/* Top Processes */}
Top Processes
{/* Header */}
PROCESS CPU %
{/* Process List */}
{processes.map((process, index) => (
{process.name}
10 ? 'text-red-500 dark:text-red-400' : process.cpu > 5 ? 'text-yellow-500 dark:text-yellow-400' : 'text-green-500 dark:text-green-400' }`}> {process.cpu}%
))}
{/* System Details */} {systemInfo && (
CPU: {systemInfo.cpu.brand}
Cores: {systemInfo.cpu.cores} ({systemInfo.cpu.physicalCores} physical)
Load Avg: {systemInfo.load.avgLoad.toFixed(2)}
Memory: {formatBytes(systemInfo.memory.available)} free
)}
); }; export default SystemMonitorWidget;