2022-11-25 20:25:03 +00:00
|
|
|
import { useEffect, useRef } from 'react';
|
2019-09-29 20:23:15 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2021-01-31 02:43:46 +00:00
|
|
|
import { SocketEvent } from '@/components/server/events';
|
|
|
|
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
|
2022-06-26 00:49:25 +00:00
|
|
|
import { Line } from 'react-chartjs-2';
|
|
|
|
import { useChart, useChartTickLabel } from '@/components/server/console/chart';
|
2022-06-26 18:34:09 +00:00
|
|
|
import { hexToRgba } from '@/lib/helpers';
|
|
|
|
import { bytesToString } from '@/lib/formatters';
|
2022-06-26 00:49:25 +00:00
|
|
|
import { CloudDownloadIcon, CloudUploadIcon } from '@heroicons/react/solid';
|
|
|
|
import { theme } from 'twin.macro';
|
|
|
|
import ChartBlock from '@/components/server/console/ChartBlock';
|
|
|
|
import Tooltip from '@/components/elements/tooltip/Tooltip';
|
2022-05-14 03:00:59 +00:00
|
|
|
|
|
|
|
export default () => {
|
2022-11-25 20:25:03 +00:00
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
|
|
|
const limits = ServerContext.useStoreState(state => state.server.data!.limits);
|
2022-05-14 03:00:59 +00:00
|
|
|
const previous = useRef<Record<'tx' | 'rx', number>>({ tx: -1, rx: -1 });
|
2022-06-26 00:49:25 +00:00
|
|
|
|
2022-07-01 00:23:27 +00:00
|
|
|
const cpu = useChartTickLabel('CPU', limits.cpu, '%', 2);
|
2022-10-09 22:09:24 +00:00
|
|
|
const memory = useChartTickLabel('Memory', limits.memory, 'MiB');
|
2022-06-26 00:49:25 +00:00
|
|
|
const network = useChart('Network', {
|
|
|
|
sets: 2,
|
|
|
|
options: {
|
|
|
|
scales: {
|
|
|
|
y: {
|
|
|
|
ticks: {
|
2022-06-26 19:13:52 +00:00
|
|
|
callback(value) {
|
2022-06-26 18:34:09 +00:00
|
|
|
return bytesToString(typeof value === 'string' ? parseInt(value, 10) : value);
|
2022-06-26 00:49:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-06-26 19:13:52 +00:00
|
|
|
callback(opts, index) {
|
2022-06-26 00:49:25 +00:00
|
|
|
return {
|
|
|
|
...opts,
|
|
|
|
label: !index ? 'Network In' : 'Network Out',
|
|
|
|
borderColor: !index ? theme('colors.cyan.400') : theme('colors.yellow.400'),
|
2022-06-26 18:34:09 +00:00
|
|
|
backgroundColor: hexToRgba(!index ? theme('colors.cyan.700') : theme('colors.yellow.700'), 0.5),
|
2022-06-26 00:49:25 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (status === 'offline') {
|
|
|
|
cpu.clear();
|
|
|
|
memory.clear();
|
|
|
|
network.clear();
|
|
|
|
}
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [status]);
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2021-01-31 02:43:46 +00:00
|
|
|
useWebsocketEvent(SocketEvent.STATS, (data: string) => {
|
2022-06-26 00:49:25 +00:00
|
|
|
let values: any = {};
|
2019-09-29 20:23:15 +00:00
|
|
|
try {
|
2022-06-26 00:49:25 +00:00
|
|
|
values = JSON.parse(data);
|
2019-09-29 20:23:15 +00:00
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-01 00:23:27 +00:00
|
|
|
cpu.push(values.cpu_absolute);
|
2022-06-26 00:49:25 +00:00
|
|
|
memory.push(Math.floor(values.memory_bytes / 1024 / 1024));
|
|
|
|
network.push([
|
|
|
|
previous.current.tx < 0 ? 0 : Math.max(0, values.network.tx_bytes - previous.current.tx),
|
|
|
|
previous.current.rx < 0 ? 0 : Math.max(0, values.network.rx_bytes - previous.current.rx),
|
|
|
|
]);
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2022-06-26 00:49:25 +00:00
|
|
|
previous.current = { tx: values.network.tx_bytes, rx: values.network.rx_bytes };
|
2021-01-31 02:43:46 +00:00
|
|
|
});
|
2019-09-29 20:23:15 +00:00
|
|
|
|
|
|
|
return (
|
2022-06-21 22:43:59 +00:00
|
|
|
<>
|
2022-06-26 00:49:25 +00:00
|
|
|
<ChartBlock title={'CPU Load'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Line {...cpu.props} />
|
2022-06-26 00:49:25 +00:00
|
|
|
</ChartBlock>
|
|
|
|
<ChartBlock title={'Memory'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Line {...memory.props} />
|
2022-06-26 00:49:25 +00:00
|
|
|
</ChartBlock>
|
|
|
|
<ChartBlock
|
|
|
|
title={'Network'}
|
|
|
|
legend={
|
|
|
|
<>
|
|
|
|
<Tooltip arrow content={'Inbound'}>
|
2023-01-12 19:31:47 +00:00
|
|
|
<CloudDownloadIcon className={'mr-2 h-4 w-4 text-yellow-400'} />
|
2022-06-26 00:49:25 +00:00
|
|
|
</Tooltip>
|
|
|
|
<Tooltip arrow content={'Outbound'}>
|
2023-01-12 19:31:47 +00:00
|
|
|
<CloudUploadIcon className={'h-4 w-4 text-cyan-400'} />
|
2022-06-26 00:49:25 +00:00
|
|
|
</Tooltip>
|
|
|
|
</>
|
2022-05-14 03:00:59 +00:00
|
|
|
}
|
2022-06-26 00:49:25 +00:00
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Line {...network.props} />
|
2022-06-26 00:49:25 +00:00
|
|
|
</ChartBlock>
|
2022-06-21 22:43:59 +00:00
|
|
|
</>
|
2019-09-29 20:23:15 +00:00
|
|
|
);
|
|
|
|
};
|