2022-05-14 03:00:59 +00:00
|
|
|
import {
|
2022-06-20 21:26:47 +00:00
|
|
|
faClock,
|
|
|
|
faCloudDownloadAlt,
|
|
|
|
faCloudUploadAlt,
|
2022-05-14 03:00:59 +00:00
|
|
|
faHdd,
|
|
|
|
faMemory,
|
|
|
|
faMicrochip,
|
2022-06-20 21:26:47 +00:00
|
|
|
faWifi,
|
2022-05-14 03:00:59 +00:00
|
|
|
} from '@fortawesome/free-solid-svg-icons';
|
2022-11-25 20:25:03 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
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';
|
2022-06-20 21:26:47 +00:00
|
|
|
import StatBlock from '@/components/server/console/StatBlock';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { bytesToString, ip, mbToBytes } from '@/lib/formatters';
|
2022-07-01 00:33:09 +00:00
|
|
|
import { capitalize } from '@/lib/strings';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
|
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
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
function getBackgroundColor(value: number, max: number | null): string | undefined {
|
2022-06-26 19:13:52 +00:00
|
|
|
const delta = !max ? 0 : value / max;
|
2020-12-04 16:48:47 +00:00
|
|
|
|
2022-06-20 21:26:47 +00: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 21:26:47 +00:00
|
|
|
return undefined;
|
2022-11-25 20:25:03 +00:00
|
|
|
}
|
2022-06-20 21:26:47 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
function Limit({ limit, children }: { limit: string | null; children: ReactNode }) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{children}
|
|
|
|
<span className={'ml-1 text-gray-300 text-[70%] select-none'}>/ {limit || <>∞</>}</span>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2022-06-28 00:14:55 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
function ServerDetailsBlock({ className }: { className?: string }) {
|
2022-06-26 19:13:52 +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
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
|
|
|
const connected = ServerContext.useStoreState(state => state.socket.connected);
|
|
|
|
const instance = ServerContext.useStoreState(state => state.socket.instance);
|
|
|
|
const limits = ServerContext.useStoreState(state => state.server.data!.limits);
|
2022-06-28 00:14:55 +00:00
|
|
|
|
|
|
|
const textLimits = useMemo(
|
|
|
|
() => ({
|
|
|
|
cpu: limits?.cpu ? `${limits.cpu}%` : null,
|
|
|
|
memory: limits?.memory ? bytesToString(mbToBytes(limits.memory)) : null,
|
|
|
|
disk: limits?.disk ? bytesToString(mbToBytes(limits.disk)) : null,
|
|
|
|
}),
|
2022-11-25 20:25:03 +00:00
|
|
|
[limits],
|
2022-06-28 00:14:55 +00:00
|
|
|
);
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const allocation = ServerContext.useStoreState(state => {
|
|
|
|
const match = state.server.data!.allocations.find(allocation => allocation.isDefault);
|
2022-06-20 21:26:47 +00:00
|
|
|
|
2022-06-26 18:34:09 +00:00
|
|
|
return !match ? 'n/a' : `${match.alias || ip(match.ip)}:${match.port}`;
|
2022-06-20 21:26:47 +00:00
|
|
|
});
|
2020-10-04 02:36:26 +00:00
|
|
|
|
2022-06-20 21:26:47 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!connected || !instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.send(SocketRequest.SEND_STATS);
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [instance, connected]);
|
2022-06-20 21:26:47 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
useWebsocketEvent(SocketEvent.STATS, data => {
|
2020-10-04 02:36:26 +00: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 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
|
|
|
});
|
2022-06-20 21:26:47 +00:00
|
|
|
});
|
2020-10-04 02:36:26 +00:00
|
|
|
|
|
|
|
return (
|
2022-06-21 22:43:59 +00:00
|
|
|
<div className={classNames('grid grid-cols-6 gap-2 md:gap-4', className)}>
|
2022-06-27 23:18:58 +00:00
|
|
|
<StatBlock icon={faWifi} title={'Address'} copyOnClick={allocation}>
|
2022-06-26 01:31:10 +00:00
|
|
|
{allocation}
|
|
|
|
</StatBlock>
|
2022-06-20 21:26:47 +00:00
|
|
|
<StatBlock
|
|
|
|
icon={faClock}
|
|
|
|
title={'Uptime'}
|
2022-06-26 19:13:52 +00:00
|
|
|
color={getBackgroundColor(status === 'running' ? 0 : status !== 'offline' ? 9 : 10, 10)}
|
2022-06-20 21:26:47 +00:00
|
|
|
>
|
2022-10-05 02:12:07 +00:00
|
|
|
{status === null ? (
|
2022-10-08 18:30:14 +00:00
|
|
|
'Offline'
|
2022-07-01 00:33:09 +00:00
|
|
|
) : stats.uptime > 0 ? (
|
|
|
|
<UptimeDuration uptime={stats.uptime / 1000} />
|
|
|
|
) : (
|
2022-10-05 02:12:07 +00:00
|
|
|
capitalize(status)
|
2022-07-01 00:33:09 +00:00
|
|
|
)}
|
2022-06-20 21:26:47 +00:00
|
|
|
</StatBlock>
|
2022-06-28 00:14:55 +00:00
|
|
|
<StatBlock icon={faMicrochip} title={'CPU Load'} color={getBackgroundColor(stats.cpu, limits.cpu)}>
|
|
|
|
{status === 'offline' ? (
|
|
|
|
<span className={'text-gray-400'}>Offline</span>
|
|
|
|
) : (
|
|
|
|
<Limit limit={textLimits.cpu}>{stats.cpu.toFixed(2)}%</Limit>
|
|
|
|
)}
|
2022-06-20 21:26:47 +00:00
|
|
|
</StatBlock>
|
|
|
|
<StatBlock
|
|
|
|
icon={faMemory}
|
|
|
|
title={'Memory'}
|
|
|
|
color={getBackgroundColor(stats.memory / 1024, limits.memory * 1024)}
|
|
|
|
>
|
2022-06-28 00:14:55 +00:00
|
|
|
{status === 'offline' ? (
|
|
|
|
<span className={'text-gray-400'}>Offline</span>
|
|
|
|
) : (
|
|
|
|
<Limit limit={textLimits.memory}>{bytesToString(stats.memory)}</Limit>
|
|
|
|
)}
|
2022-06-20 21:26:47 +00:00
|
|
|
</StatBlock>
|
2022-06-28 00:14:55 +00:00
|
|
|
<StatBlock icon={faHdd} title={'Disk'} color={getBackgroundColor(stats.disk / 1024, limits.disk * 1024)}>
|
|
|
|
<Limit limit={textLimits.disk}>{bytesToString(stats.disk)}</Limit>
|
2022-06-20 21:26:47 +00:00
|
|
|
</StatBlock>
|
2022-06-28 00:14:55 +00:00
|
|
|
<StatBlock icon={faCloudDownloadAlt} title={'Network (Inbound)'}>
|
2022-06-27 22:30:21 +00:00
|
|
|
{status === 'offline' ? <span className={'text-gray-400'}>Offline</span> : bytesToString(stats.rx)}
|
2022-06-20 21:26:47 +00:00
|
|
|
</StatBlock>
|
2022-06-28 00:14:55 +00:00
|
|
|
<StatBlock icon={faCloudUploadAlt} title={'Network (Outbound)'}>
|
2022-06-27 22:30:21 +00:00
|
|
|
{status === 'offline' ? <span className={'text-gray-400'}>Offline</span> : bytesToString(stats.tx)}
|
2022-06-20 21:26:47 +00:00
|
|
|
</StatBlock>
|
|
|
|
</div>
|
2020-10-04 02:36:26 +00:00
|
|
|
);
|
2022-11-25 20:25:03 +00:00
|
|
|
}
|
2020-10-04 02:36:26 +00:00
|
|
|
|
|
|
|
export default ServerDetailsBlock;
|