2020-04-10 19:49:09 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2019-08-17 23:03:10 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-07-15 03:48:41 +00:00
|
|
|
import { faEthernet, faHdd, faMemory, faMicrochip, faServer } from '@fortawesome/free-solid-svg-icons';
|
2019-08-17 23:03:10 +00:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import { Server } from '@/api/server/getServer';
|
|
|
|
import getServerResourceUsage, { ServerStats } from '@/api/server/getServerResourceUsage';
|
2020-06-10 20:00:43 +00:00
|
|
|
import { bytesToHuman, megabytesToHuman } from '@/helpers';
|
2020-07-03 21:50:37 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
2020-07-12 18:15:54 +00:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2019-08-17 23:03:10 +00:00
|
|
|
|
|
|
|
// 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 => {
|
2020-07-04 21:22:25 +00:00
|
|
|
const limitInBytes = limit * 1024 * 1024;
|
2019-08-17 23:03:10 +00:00
|
|
|
|
|
|
|
return current / limitInBytes >= 0.90;
|
|
|
|
};
|
|
|
|
|
2020-07-12 18:15:54 +00:00
|
|
|
export default ({ server, className }: { server: Server; className?: string }) => {
|
2020-04-10 19:49:09 +00:00
|
|
|
const interval = useRef<number>(null);
|
2019-08-17 23:03:10 +00:00
|
|
|
const [ stats, setStats ] = useState<ServerStats | null>(null);
|
2020-04-17 19:03:16 +00:00
|
|
|
const [ statsError, setStatsError ] = useState(false);
|
2019-08-17 23:03:10 +00:00
|
|
|
|
2020-04-17 19:03:16 +00:00
|
|
|
const getStats = () => {
|
|
|
|
setStatsError(false);
|
|
|
|
return getServerResourceUsage(server.uuid)
|
|
|
|
.then(data => setStats(data))
|
|
|
|
.catch(error => {
|
|
|
|
setStatsError(true);
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
};
|
2019-08-17 23:03:10 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getStats().then(() => {
|
2020-04-10 19:49:09 +00:00
|
|
|
// @ts-ignore
|
|
|
|
interval.current = setInterval(() => getStats(), 20000);
|
2019-08-17 23:03:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
2020-04-10 19:49:09 +00:00
|
|
|
interval.current && clearInterval(interval.current);
|
2019-08-17 23:03:10 +00:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-07-11 18:29:04 +00:00
|
|
|
|
2020-07-12 17:41:41 +00:00
|
|
|
const disklimit = server.limits.disk !== 0 ? megabytesToHuman(server.limits.disk) : 'Unlimited';
|
|
|
|
const memorylimit = server.limits.memory !== 0 ? megabytesToHuman(server.limits.memory) : 'Unlimited';
|
2019-08-17 23:03:10 +00:00
|
|
|
|
|
|
|
return (
|
2020-07-12 18:15:54 +00:00
|
|
|
<GreyRowBox as={Link} to={`/server/${server.id}`} className={className}>
|
2019-08-17 23:03:10 +00:00
|
|
|
<div className={'icon'}>
|
|
|
|
<FontAwesomeIcon icon={faServer}/>
|
|
|
|
</div>
|
2020-07-03 21:50:37 +00:00
|
|
|
<div css={tw`flex-1 ml-4`}>
|
|
|
|
<p css={tw`text-lg`}>{server.name}</p>
|
2019-08-17 23:03:10 +00:00
|
|
|
</div>
|
2020-07-03 21:50:37 +00:00
|
|
|
<div css={tw`w-1/4 overflow-hidden`}>
|
|
|
|
<div css={tw`flex ml-4`}>
|
|
|
|
<FontAwesomeIcon icon={faEthernet} css={tw`text-neutral-500`}/>
|
|
|
|
<p css={tw`text-sm text-neutral-400 ml-2`}>
|
2019-08-17 23:03:10 +00:00
|
|
|
{
|
2020-07-09 04:42:37 +00:00
|
|
|
server.allocations.filter(alloc => alloc.isDefault).map(allocation => (
|
2019-08-17 23:03:10 +00:00
|
|
|
<span key={allocation.ip + allocation.port.toString()}>{allocation.alias || allocation.ip}:{allocation.port}</span>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-07-12 18:15:54 +00:00
|
|
|
<div css={tw`w-1/3 flex items-baseline justify-center relative`}>
|
2019-08-17 23:03:10 +00:00
|
|
|
{!stats ?
|
2020-04-17 19:03:16 +00:00
|
|
|
!statsError ?
|
2020-07-12 18:15:54 +00:00
|
|
|
<Spinner size={'small'}/>
|
2020-04-17 19:03:16 +00:00
|
|
|
:
|
2020-04-26 00:59:46 +00:00
|
|
|
server.isInstalling ?
|
2020-07-03 21:50:37 +00:00
|
|
|
<div css={tw`flex-1 text-center`}>
|
|
|
|
<span css={tw`bg-neutral-500 rounded px-2 py-1 text-neutral-100 text-xs`}>
|
2020-04-26 00:59:46 +00:00
|
|
|
Installing
|
2020-04-26 00:58:09 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
:
|
2020-07-03 21:50:37 +00:00
|
|
|
<div css={tw`flex-1 text-center`}>
|
|
|
|
<span css={tw`bg-red-500 rounded px-2 py-1 text-red-100 text-xs`}>
|
2020-04-26 00:59:46 +00:00
|
|
|
{server.isSuspended ? 'Suspended' : 'Connection Error'}
|
2020-04-26 00:58:09 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
2019-08-17 23:03:10 +00:00
|
|
|
:
|
|
|
|
<React.Fragment>
|
2020-07-03 21:50:37 +00:00
|
|
|
<div css={tw`flex-1 flex ml-4 justify-center`}>
|
2019-08-17 23:03:10 +00:00
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faMicrochip}
|
2020-07-03 21:50:37 +00:00
|
|
|
css={[
|
|
|
|
!alarms.cpu && tw`text-neutral-500`,
|
|
|
|
alarms.cpu && tw`text-red-400`,
|
|
|
|
]}
|
2019-08-17 23:03:10 +00:00
|
|
|
/>
|
|
|
|
<p
|
2020-07-03 21:50:37 +00:00
|
|
|
css={[
|
|
|
|
tw`text-sm ml-2`,
|
|
|
|
!alarms.cpu && tw`text-neutral-400`,
|
|
|
|
alarms.cpu && tw`text-white`,
|
|
|
|
]}
|
2019-08-17 23:03:10 +00:00
|
|
|
>
|
|
|
|
{stats.cpuUsagePercent} %
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-07-03 21:50:37 +00:00
|
|
|
<div css={tw`flex-1 ml-4`}>
|
|
|
|
<div css={tw`flex justify-center`}>
|
2019-08-17 23:03:10 +00:00
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faMemory}
|
2020-07-03 21:50:37 +00:00
|
|
|
css={[
|
|
|
|
!alarms.memory && tw`text-neutral-500`,
|
|
|
|
alarms.memory && tw`text-red-400`,
|
|
|
|
]}
|
2019-08-17 23:03:10 +00:00
|
|
|
/>
|
|
|
|
<p
|
2020-07-03 21:50:37 +00:00
|
|
|
css={[
|
|
|
|
tw`text-sm ml-2`,
|
|
|
|
!alarms.memory && tw`text-neutral-400`,
|
|
|
|
alarms.memory && tw`text-white`,
|
|
|
|
]}
|
2019-08-17 23:03:10 +00:00
|
|
|
>
|
|
|
|
{bytesToHuman(stats.memoryUsageInBytes)}
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-07-03 21:50:37 +00:00
|
|
|
<p css={tw`text-xs text-neutral-600 text-center mt-1`}>of {memorylimit}</p>
|
2019-08-17 23:03:10 +00:00
|
|
|
</div>
|
2020-07-03 21:50:37 +00:00
|
|
|
<div css={tw`flex-1 ml-4`}>
|
|
|
|
<div css={tw`flex justify-center`}>
|
2019-08-17 23:03:10 +00:00
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faHdd}
|
2020-07-03 21:50:37 +00:00
|
|
|
css={[
|
|
|
|
!alarms.disk && tw`text-neutral-500`,
|
|
|
|
alarms.disk && tw`text-red-400`,
|
|
|
|
]}
|
2019-08-17 23:03:10 +00:00
|
|
|
/>
|
|
|
|
<p
|
2020-07-03 21:50:37 +00:00
|
|
|
css={[
|
|
|
|
tw`text-sm ml-2`,
|
|
|
|
!alarms.disk && tw`text-neutral-400`,
|
|
|
|
alarms.disk && tw`text-white`,
|
|
|
|
]}
|
2019-08-17 23:03:10 +00:00
|
|
|
>
|
|
|
|
{bytesToHuman(stats.diskUsageInBytes)}
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-07-03 21:50:37 +00:00
|
|
|
<p css={tw`text-xs text-neutral-600 text-center mt-1`}>of {disklimit}</p>
|
2019-08-17 23:03:10 +00:00
|
|
|
</div>
|
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
</div>
|
2020-07-03 21:50:37 +00:00
|
|
|
</GreyRowBox>
|
2019-08-17 23:03:10 +00:00
|
|
|
);
|
|
|
|
};
|