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 (
{label} {used} / {total} {unit} ({percent.toFixed(1)}%)
); }; const Gauge = ({ value, label }) => { const radius = 40; const circumference = 2 * Math.PI * radius; const offset = circumference - (value / 100) * circumference; const colorClass = getStatusColor(value, { yellow: 50, red: 80 }).split(' ')[0]; return (
{value}%
{label}
); }; const getLoadColor = (load) => { if (load < 1.0) return 'text-green-500'; if (load < 2.0) return 'text-yellow-500'; return 'text-red-500'; }; return (
{error && (
{error}
)}
{/* CPU Gauge Card */}
{/* Load Average Card */}

Load Average

{metrics.load.one.toFixed(2)}
1 Min
{metrics.load.five.toFixed(2)}
5 Min
{metrics.load.fifteen.toFixed(2)}
15 Min
{/* Uptime Card */}

System Uptime

{metrics.uptime.human}
{/* Memory Usage */}

Memory Breakdown

{/* Disk Usage */}

Storage Health

{metrics.disk.length > 0 ? ( metrics.disk.map((p, idx) => { const usedVal = parseFloat(p.used); const totalVal = parseFloat(p.total); const unit = p.unit || 'GB'; const percentNum = parseInt(p.percent); return ( ); }) ) : (
Scanning disks...
)}
); }; export default SystemMonitor;