2020-10-04 02:36:26 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import tw from 'twin.macro';
|
2020-11-01 07:50:35 +00:00
|
|
|
import { faCircle, faEthernet, faHdd, faMemory, faMicrochip, faServer } from '@fortawesome/free-solid-svg-icons';
|
2020-10-04 02:36:26 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { bytesToHuman, megabytesToHuman } from '@/helpers';
|
|
|
|
import TitledGreyBox from '@/components/elements/TitledGreyBox';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2020-11-01 07:50:35 +00:00
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
2020-11-02 07:32:38 +00:00
|
|
|
|
2020-10-04 02:36:26 +00:00
|
|
|
interface Stats {
|
|
|
|
memory: number;
|
|
|
|
cpu: number;
|
|
|
|
disk: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ServerDetailsBlock = () => {
|
|
|
|
const [ stats, setStats ] = useState<Stats>({ memory: 0, cpu: 0, disk: 0 });
|
|
|
|
|
2020-10-04 02:37:36 +00:00
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
2020-10-04 02:36:26 +00:00
|
|
|
const connected = ServerContext.useStoreState(state => state.socket.connected);
|
|
|
|
const instance = ServerContext.useStoreState(state => state.socket.instance);
|
|
|
|
|
|
|
|
const statsListener = (data: string) => {
|
|
|
|
let stats: any = {};
|
|
|
|
try {
|
|
|
|
stats = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStats({
|
|
|
|
memory: stats.memory_bytes,
|
|
|
|
cpu: stats.cpu_absolute,
|
|
|
|
disk: stats.disk_bytes,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!connected || !instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.addListener('stats', statsListener);
|
|
|
|
instance.send('send stats');
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
instance.removeListener('stats', statsListener);
|
|
|
|
};
|
|
|
|
}, [ instance, connected ]);
|
|
|
|
|
|
|
|
const name = ServerContext.useStoreState(state => state.server.data!.name);
|
|
|
|
const limits = ServerContext.useStoreState(state => state.server.data!.limits);
|
2020-11-01 07:50:35 +00:00
|
|
|
const primaryAllocation = ServerContext.useStoreState(state => state.server.data!.allocations.filter(alloc => alloc.isDefault).map(
|
2020-11-01 07:55:29 +00:00
|
|
|
allocation => (allocation.alias || allocation.ip) + ':' + allocation.port
|
2020-11-01 07:50:35 +00:00
|
|
|
)).toString();
|
2020-10-04 02:36:26 +00:00
|
|
|
|
|
|
|
const disklimit = limits.disk ? megabytesToHuman(limits.disk) : 'Unlimited';
|
|
|
|
const memorylimit = limits.memory ? megabytesToHuman(limits.memory) : 'Unlimited';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TitledGreyBox css={tw`break-words`} title={name} icon={faServer}>
|
|
|
|
<p css={tw`text-xs uppercase`}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faCircle}
|
|
|
|
fixedWidth
|
|
|
|
css={[
|
|
|
|
tw`mr-1`,
|
|
|
|
status === 'offline' ? tw`text-red-500` : (status === 'running' ? tw`text-green-500` : tw`text-yellow-500`),
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
{!status ? 'Connecting...' : status}
|
|
|
|
</p>
|
2020-11-07 22:25:05 +00:00
|
|
|
<CopyOnClick text={primaryAllocation}>
|
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faEthernet} fixedWidth css={tw`mr-1`}/>
|
|
|
|
<code css={tw`ml-1`}>{primaryAllocation}</code>
|
|
|
|
</p>
|
|
|
|
</CopyOnClick>
|
2020-10-04 02:36:26 +00:00
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faMicrochip} fixedWidth css={tw`mr-1`}/> {stats.cpu.toFixed(2)}%
|
|
|
|
</p>
|
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faMemory} fixedWidth css={tw`mr-1`}/> {bytesToHuman(stats.memory)}
|
|
|
|
<span css={tw`text-neutral-500`}> / {memorylimit}</span>
|
|
|
|
</p>
|
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faHdd} fixedWidth css={tw`mr-1`}/> {bytesToHuman(stats.disk)}
|
|
|
|
<span css={tw`text-neutral-500`}> / {disklimit}</span>
|
|
|
|
</p>
|
|
|
|
</TitledGreyBox>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ServerDetailsBlock;
|