import React, { useEffect, useState } from 'react'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faServer } from '@fortawesome/free-solid-svg-icons/faServer'; import { faEthernet } from '@fortawesome/free-solid-svg-icons/faEthernet'; import { faMicrochip } from '@fortawesome/free-solid-svg-icons/faMicrochip'; import { faMemory } from '@fortawesome/free-solid-svg-icons/faMemory'; import { faHdd } from '@fortawesome/free-solid-svg-icons/faHdd'; import { Link } from 'react-router-dom'; import { Server } from '@/api/server/getServer'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import getServerResourceUsage, { ServerStats } from '@/api/server/getServerResourceUsage'; import { bytesToHuman } from '@/helpers'; import classNames from 'classnames'; // Determines if the current value is in an alarm threshold so we can show it in red rather // than the more faded default style. const isAlarmState = (current: number, limit: number): boolean => { const limitInBytes = limit * 1000 * 1000; return current / limitInBytes >= 0.90; }; export default ({ server, className }: { server: Server; className: string | undefined }) => { const [ stats, setStats ] = useState(null); const getStats = () => getServerResourceUsage(server.uuid).then(data => setStats(data)); useEffect(() => { let interval: any = null; getStats().then(() => { interval = setInterval(() => getStats(), 20000); }); return () => { interval && clearInterval(interval); }; }, []); const alarms = { cpu: false, memory: false, disk: false }; if (stats) { alarms.cpu = server.limits.cpu === 0 ? false : (stats.cpuUsagePercent >= (server.limits.cpu * 0.9)); alarms.memory = isAlarmState(stats.memoryUsageInBytes, server.limits.memory); alarms.disk = server.limits.disk === 0 ? false : isAlarmState(stats.diskUsageInBytes, server.limits.disk); } return (

{server.name}

{ server.allocations.filter(alloc => alloc.default).map(allocation => ( {allocation.alias || allocation.ip}:{allocation.port} )) }

{!stats ? :

{stats.cpuUsagePercent} %

{bytesToHuman(stats.memoryUsageInBytes)}

of {bytesToHuman(server.limits.memory * 1000 * 1000)}

{bytesToHuman(stats.diskUsageInBytes)}

of {bytesToHuman(server.limits.disk * 1000 * 1000)}

}
); };