import React, { useState, useEffect } from 'react'; const SystemMonitor = () => { const [metrics, setMetrics] = useState({ cpu: 0, memory: { total: 0, used: 0, free: 0 }, disk: [], uptime: { seconds: 0, human: 'Loading...' }, load: { one: 0, five: 0, fifteen: 0 } }); const [error, setError] = useState(null); const fetchCPU = async () => { try { const res = await fetch('/api/v1/system/cpu'); const json = await res.json(); if (json.success) setMetrics(prev => ({ ...prev, cpu: json.usage })); } catch (err) { console.error('CPU fetch failed', err); } }; const fetchOtherMetrics = async () => { try { const [memRes, diskRes, uptimeRes, loadRes] = await Promise.all([ fetch('/api/v1/system/memory'), fetch('/api/v1/system/disk'), fetch('/api/v1/system/uptime'), fetch('/api/v1/system/load') ]); const [memJson, diskJson, uptimeJson, loadJson] = await Promise.all([ memRes.json(), diskRes.json(), uptimeRes.json(), loadRes.json() ]); setMetrics(prev => ({ ...prev, memory: memJson.success ? { total: memJson.total, used: memJson.used, free: memJson.free } : prev.memory, disk: diskJson.success ? diskJson.partitions : prev.disk, uptime: uptimeJson.success ? { seconds: uptimeJson.seconds, human: uptimeJson.human } : prev.uptime, load: loadJson.success ? { one: loadJson.one, five: loadJson.five, fifteen: loadJson.fifteen } : prev.load })); setError(null); } catch (err) { setError('Failed to fetch system metrics'); console.error('Metrics fetch failed', err); } }; useEffect(() => { fetchCPU(); fetchOtherMetrics(); const cpuInterval = setInterval(fetchCPU, 5000); const otherInterval = setInterval(fetchOtherMetrics, 10000); return () => { clearInterval(cpuInterval); clearInterval(otherInterval); }; }, []); const getStatusColor = (percent, thresholds) => { if (percent < thresholds.yellow) return 'text-green-500 bg-green-500'; if (percent < thresholds.red) return 'text-yellow-500 bg-yellow-500'; return 'text-red-500 bg-red-500'; }; const ProgressBar = ({ label, used, total, unit = 'MB', colorClass }) => { const percent = total > 0 ? (used / total) * 100 : 0; const barColor = colorClass.split(' ')[1]; const textColor = colorClass.split(' ')[0]; return (