2020-10-04 03:36:26 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-05-14 04:00:59 +01:00
|
|
|
import {
|
2022-06-20 22:26:47 +01:00
|
|
|
faClock,
|
|
|
|
faCloudDownloadAlt,
|
|
|
|
faCloudUploadAlt,
|
2022-05-14 04:00:59 +01:00
|
|
|
faHdd,
|
|
|
|
faMemory,
|
|
|
|
faMicrochip,
|
2022-06-20 22:26:47 +01:00
|
|
|
faWifi,
|
2022-05-14 04:00:59 +01:00
|
|
|
} from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import { bytesToHuman, formatIp, megabytesToHuman } from '@/helpers';
|
2020-10-04 03:36:26 +01:00
|
|
|
import { ServerContext } from '@/state/server';
|
2021-01-31 02:43:46 +00:00
|
|
|
import { SocketEvent, SocketRequest } from '@/components/server/events';
|
2021-10-03 20:59:44 +01:00
|
|
|
import UptimeDuration from '@/components/server/UptimeDuration';
|
2022-06-20 22:26:47 +01:00
|
|
|
import StatBlock from '@/components/server/console/StatBlock';
|
|
|
|
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
|
2022-06-21 23:43:59 +01:00
|
|
|
import classNames from 'classnames';
|
2020-11-02 07:32:38 +00:00
|
|
|
|
2022-05-14 04:00:59 +01:00
|
|
|
type Stats = Record<'memory' | 'cpu' | 'disk' | 'uptime' | 'rx' | 'tx', number>;
|
2020-10-04 03:36:26 +01:00
|
|
|
|
2022-06-20 22:26:47 +01:00
|
|
|
const getBackgroundColor = (value: number, max: number | null): string | undefined => {
|
|
|
|
const delta = !max ? 0 : (value / max);
|
2020-12-04 16:48:47 +00:00
|
|
|
|
2022-06-20 22:26:47 +01:00
|
|
|
if (delta > 0.8) {
|
|
|
|
if (delta > 0.9) {
|
|
|
|
return 'bg-red-500';
|
|
|
|
}
|
|
|
|
return 'bg-yellow-500';
|
2020-12-04 16:48:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 22:26:47 +01:00
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
2022-06-21 23:43:59 +01:00
|
|
|
const ServerDetailsBlock = ({ className }: { className?: string }) => {
|
2022-05-14 04:00:59 +01:00
|
|
|
const [ stats, setStats ] = useState<Stats>({ memory: 0, cpu: 0, disk: 0, uptime: 0, tx: 0, rx: 0 });
|
2020-10-04 03:36:26 +01:00
|
|
|
|
2020-10-04 03:37:36 +01:00
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
2020-10-04 03:36:26 +01:00
|
|
|
const connected = ServerContext.useStoreState(state => state.socket.connected);
|
|
|
|
const instance = ServerContext.useStoreState(state => state.socket.instance);
|
2022-06-20 22:26:47 +01:00
|
|
|
const limits = ServerContext.useStoreState(state => state.server.data!.limits);
|
|
|
|
const allocation = ServerContext.useStoreState(state => {
|
|
|
|
const match = state.server.data!.allocations.find(allocation => allocation.isDefault);
|
|
|
|
|
|
|
|
return !match ? 'n/a' : `${match.alias || formatIp(match.ip)}:${match.port}`;
|
|
|
|
});
|
2020-10-04 03:36:26 +01:00
|
|
|
|
2022-06-20 22:26:47 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!connected || !instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.send(SocketRequest.SEND_STATS);
|
|
|
|
}, [ instance, connected ]);
|
|
|
|
|
|
|
|
useWebsocketEvent(SocketEvent.STATS, (data) => {
|
2020-10-04 03:36:26 +01:00
|
|
|
let stats: any = {};
|
|
|
|
try {
|
|
|
|
stats = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStats({
|
|
|
|
memory: stats.memory_bytes,
|
|
|
|
cpu: stats.cpu_absolute,
|
|
|
|
disk: stats.disk_bytes,
|
2022-05-14 04:00:59 +01:00
|
|
|
tx: stats.network.tx_bytes,
|
|
|
|
rx: stats.network.rx_bytes,
|
2021-10-03 20:59:44 +01:00
|
|
|
uptime: stats.uptime || 0,
|
2020-10-04 03:36:26 +01:00
|
|
|
});
|
2022-06-20 22:26:47 +01:00
|
|
|
});
|
2020-10-04 03:36:26 +01:00
|
|
|
|
|
|
|
return (
|
2022-06-21 23:43:59 +01:00
|
|
|
<div className={classNames('grid grid-cols-6 gap-2 md:gap-4', className)}>
|
2022-06-20 22:26:47 +01:00
|
|
|
<StatBlock
|
|
|
|
icon={faClock}
|
|
|
|
title={'Uptime'}
|
|
|
|
color={getBackgroundColor(status === 'running' ? 0 : (status !== 'offline' ? 9 : 10), 10)}
|
|
|
|
>
|
|
|
|
{stats.uptime > 0 ?
|
|
|
|
<UptimeDuration uptime={stats.uptime / 1000}/>
|
|
|
|
:
|
|
|
|
'Offline'
|
|
|
|
}
|
|
|
|
</StatBlock>
|
|
|
|
<StatBlock
|
|
|
|
icon={faMicrochip}
|
2022-06-26 01:49:25 +01:00
|
|
|
title={'CPU Load'}
|
2022-06-20 22:26:47 +01:00
|
|
|
color={getBackgroundColor(stats.cpu, limits.cpu)}
|
2022-06-26 01:49:25 +01:00
|
|
|
description={limits.cpu
|
2022-06-20 22:26:47 +01:00
|
|
|
? `This server is allowed to use up to ${limits.cpu}% of the host's available CPU resources.`
|
|
|
|
: 'No CPU limit has been configured for this server.'
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{status === 'offline' ?
|
|
|
|
<span className={'text-gray-400'}>Offline</span>
|
|
|
|
:
|
|
|
|
`${stats.cpu.toFixed(2)}%`
|
|
|
|
}
|
|
|
|
</StatBlock>
|
|
|
|
<StatBlock
|
|
|
|
icon={faMemory}
|
|
|
|
title={'Memory'}
|
|
|
|
color={getBackgroundColor(stats.memory / 1024, limits.memory * 1024)}
|
|
|
|
description={limits.memory
|
|
|
|
? `This server is allowed to use up to ${megabytesToHuman(limits.memory)} of memory.`
|
|
|
|
: 'No memory limit has been configured for this server.'
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{status === 'offline' ?
|
|
|
|
<span className={'text-gray-400'}>Offline</span>
|
|
|
|
:
|
|
|
|
bytesToHuman(stats.memory)
|
|
|
|
}
|
|
|
|
</StatBlock>
|
|
|
|
<StatBlock
|
|
|
|
icon={faHdd}
|
|
|
|
title={'Disk'}
|
|
|
|
color={getBackgroundColor(stats.disk / 1024, limits.disk * 1024)}
|
|
|
|
description={limits.disk
|
|
|
|
? `This server is allowed to use up to ${megabytesToHuman(limits.disk)} of disk space.`
|
|
|
|
: 'No disk space limit has been configured for this server.'
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{bytesToHuman(stats.disk)}
|
|
|
|
</StatBlock>
|
|
|
|
<StatBlock
|
|
|
|
icon={faCloudDownloadAlt}
|
|
|
|
title={'Network (Inbound)'}
|
|
|
|
description={'The total amount of network traffic that your server has recieved since it was started.'}
|
|
|
|
>
|
|
|
|
{status === 'offline' ?
|
|
|
|
<span className={'text-gray-400'}>Offline</span>
|
|
|
|
:
|
|
|
|
bytesToHuman(stats.tx)
|
|
|
|
}
|
|
|
|
</StatBlock>
|
|
|
|
<StatBlock
|
|
|
|
icon={faCloudUploadAlt}
|
|
|
|
title={'Network (Outbound)'}
|
|
|
|
description={'The total amount of traffic your server has sent across the internet since it was started.'}
|
|
|
|
>
|
|
|
|
{status === 'offline' ?
|
|
|
|
<span className={'text-gray-400'}>Offline</span>
|
|
|
|
:
|
|
|
|
bytesToHuman(stats.rx)
|
2021-10-03 20:59:44 +01:00
|
|
|
}
|
2022-06-20 22:26:47 +01:00
|
|
|
</StatBlock>
|
2022-06-26 02:27:18 +01:00
|
|
|
<StatBlock icon={faWifi} title={'Address'}>
|
2022-06-20 22:26:47 +01:00
|
|
|
{allocation}
|
|
|
|
</StatBlock>
|
|
|
|
</div>
|
2020-10-04 03:36:26 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ServerDetailsBlock;
|