2020-10-04 02:36:26 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-12-04 16:48:47 +00:00
|
|
|
import tw, { TwStyle } from 'twin.macro';
|
2022-05-14 03:00:59 +00:00
|
|
|
import {
|
|
|
|
faArrowCircleDown,
|
|
|
|
faArrowCircleUp,
|
|
|
|
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';
|
2022-05-14 03:00:59 +00:00
|
|
|
import { bytesToHuman, formatIp, megabytesToHuman } from '@/helpers';
|
2020-10-04 02:36:26 +00:00
|
|
|
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';
|
2021-01-31 02:43:46 +00:00
|
|
|
import { SocketEvent, SocketRequest } from '@/components/server/events';
|
2021-10-03 19:59:44 +00:00
|
|
|
import UptimeDuration from '@/components/server/UptimeDuration';
|
2020-11-02 07:32:38 +00:00
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
type Stats = Record<'memory' | 'cpu' | 'disk' | 'uptime' | 'rx' | 'tx', number>;
|
2020-10-04 02:36:26 +00:00
|
|
|
|
2021-10-03 19:59:44 +00:00
|
|
|
function statusToColor (status: string | null, installing: boolean): TwStyle {
|
2020-12-04 16:48:47 +00:00
|
|
|
if (installing) {
|
|
|
|
status = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case 'offline':
|
|
|
|
return tw`text-red-500`;
|
|
|
|
case 'running':
|
|
|
|
return tw`text-green-500`;
|
|
|
|
default:
|
|
|
|
return tw`text-yellow-500`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 02:36:26 +00:00
|
|
|
const ServerDetailsBlock = () => {
|
2022-05-14 03:00:59 +00:00
|
|
|
const [ stats, setStats ] = useState<Stats>({ memory: 0, cpu: 0, disk: 0, uptime: 0, tx: 0, rx: 0 });
|
2020-10-04 02:36:26 +00:00
|
|
|
|
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,
|
2022-05-14 03:00:59 +00:00
|
|
|
tx: stats.network.tx_bytes,
|
|
|
|
rx: stats.network.rx_bytes,
|
2021-10-03 19:59:44 +00:00
|
|
|
uptime: stats.uptime || 0,
|
2020-10-04 02:36:26 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!connected || !instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-31 02:43:46 +00:00
|
|
|
instance.addListener(SocketEvent.STATS, statsListener);
|
|
|
|
instance.send(SocketRequest.SEND_STATS);
|
2020-10-04 02:36:26 +00:00
|
|
|
|
|
|
|
return () => {
|
2021-01-31 02:43:46 +00:00
|
|
|
instance.removeListener(SocketEvent.STATS, statsListener);
|
2020-10-04 02:36:26 +00:00
|
|
|
};
|
|
|
|
}, [ instance, connected ]);
|
|
|
|
|
|
|
|
const name = ServerContext.useStoreState(state => state.server.data!.name);
|
2020-12-04 16:48:47 +00:00
|
|
|
const isInstalling = ServerContext.useStoreState(state => state.server.data!.isInstalling);
|
2020-12-16 16:34:47 +00:00
|
|
|
const isTransferring = ServerContext.useStoreState(state => state.server.data!.isTransferring);
|
2020-10-04 02:36:26 +00:00
|
|
|
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(
|
2021-12-04 18:35:55 +00:00
|
|
|
allocation => (allocation.alias || formatIp(allocation.ip)) + ':' + allocation.port,
|
2020-11-01 07:50:35 +00:00
|
|
|
)).toString();
|
2020-10-04 02:36:26 +00:00
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
const diskLimit = limits.disk ? megabytesToHuman(limits.disk) : 'Unlimited';
|
|
|
|
const memoryLimit = limits.memory ? megabytesToHuman(limits.memory) : 'Unlimited';
|
2021-06-05 15:40:48 +00:00
|
|
|
const cpuLimit = limits.cpu ? limits.cpu + '%' : 'Unlimited';
|
2020-10-04 02:36:26 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TitledGreyBox css={tw`break-words`} title={name} icon={faServer}>
|
|
|
|
<p css={tw`text-xs uppercase`}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faCircle}
|
|
|
|
fixedWidth
|
|
|
|
css={[
|
|
|
|
tw`mr-1`,
|
2020-12-16 16:34:47 +00:00
|
|
|
statusToColor(status, isInstalling || isTransferring),
|
2020-10-04 02:36:26 +00:00
|
|
|
]}
|
|
|
|
/>
|
2020-12-16 16:34:47 +00:00
|
|
|
{!status ? 'Connecting...' : (isInstalling ? 'Installing' : (isTransferring) ? 'Transferring' : status)}
|
2021-10-03 19:59:44 +00:00
|
|
|
{stats.uptime > 0 &&
|
2021-10-24 21:41:01 +00:00
|
|
|
<span css={tw`ml-2 lowercase`}>
|
2021-10-03 19:59:44 +00:00
|
|
|
(<UptimeDuration uptime={stats.uptime / 1000}/>)
|
|
|
|
</span>
|
|
|
|
}
|
2020-10-04 02:36:26 +00:00
|
|
|
</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)}%
|
2021-06-05 15:40:48 +00:00
|
|
|
<span css={tw`text-neutral-500`}> / {cpuLimit}</span>
|
2020-10-04 02:36:26 +00:00
|
|
|
</p>
|
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faMemory} fixedWidth css={tw`mr-1`}/> {bytesToHuman(stats.memory)}
|
2020-12-16 16:34:47 +00:00
|
|
|
<span css={tw`text-neutral-500`}> / {memoryLimit}</span>
|
2020-10-04 02:36:26 +00:00
|
|
|
</p>
|
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faHdd} fixedWidth css={tw`mr-1`}/> {bytesToHuman(stats.disk)}
|
2020-12-16 16:34:47 +00:00
|
|
|
<span css={tw`text-neutral-500`}> / {diskLimit}</span>
|
2020-10-04 02:36:26 +00:00
|
|
|
</p>
|
2022-05-14 03:00:59 +00:00
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faEthernet} fixedWidth css={tw`mr-1`}/>
|
|
|
|
<FontAwesomeIcon icon={faArrowCircleUp} fixedWidth css={tw`mr-1`}/>{bytesToHuman(stats.tx)}
|
|
|
|
<FontAwesomeIcon icon={faArrowCircleDown} fixedWidth css={tw`mx-1`}/>{bytesToHuman(stats.rx)}
|
|
|
|
</p>
|
2020-10-04 02:36:26 +00:00
|
|
|
</TitledGreyBox>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ServerDetailsBlock;
|