2022-05-14 03:00:59 +00:00
|
|
|
import React, { useCallback, useRef, useState } from 'react';
|
2019-09-29 20:23:15 +00:00
|
|
|
import Chart, { ChartConfiguration } from 'chart.js';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2020-07-05 04:46:49 +00:00
|
|
|
import merge from 'deepmerge';
|
2019-09-29 20:23:15 +00:00
|
|
|
import TitledGreyBox from '@/components/elements/TitledGreyBox';
|
2022-05-14 03:00:59 +00:00
|
|
|
import { faEthernet, faMemory, faMicrochip } from '@fortawesome/free-solid-svg-icons';
|
2020-07-04 22:40:41 +00:00
|
|
|
import tw from 'twin.macro';
|
2021-01-31 02:43:46 +00:00
|
|
|
import { SocketEvent } from '@/components/server/events';
|
|
|
|
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
const chartDefaults = (ticks?: Chart.TickOptions): ChartConfiguration => ({
|
2019-09-29 20:23:15 +00:00
|
|
|
type: 'line',
|
|
|
|
options: {
|
|
|
|
legend: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
tooltips: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
animation: {
|
2020-05-27 04:00:50 +00:00
|
|
|
duration: 0,
|
2019-09-29 20:23:15 +00:00
|
|
|
},
|
|
|
|
elements: {
|
|
|
|
point: {
|
|
|
|
radius: 0,
|
|
|
|
},
|
|
|
|
line: {
|
2020-05-27 04:00:50 +00:00
|
|
|
tension: 0.3,
|
2019-09-29 20:23:15 +00:00
|
|
|
backgroundColor: 'rgba(15, 178, 184, 0.45)',
|
|
|
|
borderColor: '#32D0D9',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
xAxes: [ {
|
|
|
|
ticks: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
gridLines: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
} ],
|
|
|
|
yAxes: [ {
|
|
|
|
gridLines: {
|
|
|
|
drawTicks: false,
|
|
|
|
color: 'rgba(229, 232, 235, 0.15)',
|
|
|
|
zeroLineColor: 'rgba(15, 178, 184, 0.45)',
|
|
|
|
zeroLineWidth: 3,
|
|
|
|
},
|
2020-07-05 05:36:28 +00:00
|
|
|
ticks: merge(ticks || {}, {
|
2019-09-29 20:23:15 +00:00
|
|
|
fontSize: 10,
|
|
|
|
fontFamily: '"IBM Plex Mono", monospace',
|
|
|
|
fontColor: 'rgb(229, 232, 235)',
|
|
|
|
min: 0,
|
|
|
|
beginAtZero: true,
|
|
|
|
maxTicksLimit: 5,
|
2020-07-05 05:36:28 +00:00
|
|
|
}),
|
2019-09-29 20:23:15 +00:00
|
|
|
} ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
labels: Array(20).fill(''),
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
fill: true,
|
|
|
|
data: Array(20).fill(0),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
type ChartState = [ (node: HTMLCanvasElement | null) => void, Chart | undefined ];
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
/**
|
|
|
|
* Creates an element ref and a chart instance.
|
|
|
|
*/
|
|
|
|
const useChart = (options?: Chart.TickOptions): ChartState => {
|
|
|
|
const [ chart, setChart ] = useState<Chart>();
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
const ref = useCallback<(node: HTMLCanvasElement | null) => void>(node => {
|
|
|
|
if (!node) return;
|
|
|
|
|
|
|
|
const chart = new Chart(node.getContext('2d')!, chartDefaults(options));
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
setChart(chart);
|
2019-09-29 20:23:15 +00:00
|
|
|
}, []);
|
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
return [ ref, chart ];
|
|
|
|
};
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
const updateChartDataset = (chart: Chart | null | undefined, value: Chart.ChartPoint & number): void => {
|
|
|
|
if (!chart || !chart.data?.datasets) return;
|
|
|
|
|
|
|
|
const data = chart.data.datasets[0].data!;
|
|
|
|
data.push(value);
|
|
|
|
data.shift();
|
|
|
|
chart.update({ lazy: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
|
|
|
const limits = ServerContext.useStoreState(state => state.server.data!.limits);
|
|
|
|
|
|
|
|
const previous = useRef<Record<'tx' | 'rx', number>>({ tx: -1, rx: -1 });
|
|
|
|
const [ cpuRef, cpu ] = useChart({ callback: (value) => `${value}% `, suggestedMax: limits.cpu });
|
|
|
|
const [ memoryRef, memory ] = useChart({ callback: (value) => `${value}Mb `, suggestedMax: limits.memory });
|
|
|
|
const [ txRef, tx ] = useChart({ callback: (value) => `${value}Kb/s ` });
|
|
|
|
const [ rxRef, rx ] = useChart({ callback: (value) => `${value}Kb/s ` });
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2021-01-31 02:43:46 +00:00
|
|
|
useWebsocketEvent(SocketEvent.STATS, (data: string) => {
|
2019-09-29 20:23:15 +00:00
|
|
|
let stats: any = {};
|
|
|
|
try {
|
|
|
|
stats = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
updateChartDataset(cpu, stats.cpu_absolute);
|
|
|
|
updateChartDataset(memory, Math.floor(stats.memory_bytes / 1024 / 1024));
|
|
|
|
updateChartDataset(tx, previous.current.tx < 0 ? 0 : Math.max(0, stats.network.tx_bytes - previous.current.tx) / 1024);
|
|
|
|
updateChartDataset(rx, previous.current.rx < 0 ? 0 : Math.max(0, stats.network.rx_bytes - previous.current.rx) / 1024);
|
2019-09-29 20:23:15 +00:00
|
|
|
|
2022-05-14 03:00:59 +00:00
|
|
|
previous.current = { tx: stats.network.tx_bytes, rx: stats.network.rx_bytes };
|
2021-01-31 02:43:46 +00:00
|
|
|
});
|
2019-09-29 20:23:15 +00:00
|
|
|
|
|
|
|
return (
|
2022-05-14 03:00:59 +00:00
|
|
|
<div css={tw`mt-4 grid grid-cols-1 sm:grid-cols-2 gap-4`}>
|
|
|
|
<TitledGreyBox title={'Memory usage'} icon={faMemory}>
|
|
|
|
{status !== 'offline' ?
|
|
|
|
<canvas
|
|
|
|
id={'memory_chart'}
|
|
|
|
ref={memoryRef}
|
|
|
|
aria-label={'Server Memory Usage Graph'}
|
|
|
|
role={'img'}
|
|
|
|
/>
|
|
|
|
:
|
|
|
|
<p css={tw`text-xs text-neutral-400 text-center p-3`}>
|
|
|
|
Server is offline.
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
</TitledGreyBox>
|
|
|
|
<TitledGreyBox title={'CPU usage'} icon={faMicrochip}>
|
|
|
|
{status !== 'offline' ?
|
|
|
|
<canvas id={'cpu_chart'} ref={cpuRef} aria-label={'Server CPU Usage Graph'} role={'img'}/>
|
|
|
|
:
|
|
|
|
<p css={tw`text-xs text-neutral-400 text-center p-3`}>
|
|
|
|
Server is offline.
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
</TitledGreyBox>
|
|
|
|
<TitledGreyBox title={'Inbound Data'} icon={faEthernet}>
|
|
|
|
{status !== 'offline' ?
|
|
|
|
<canvas id={'rx_chart'} ref={rxRef} aria-label={'Server Inbound Data'} role={'img'}/>
|
|
|
|
:
|
|
|
|
<p css={tw`text-xs text-neutral-400 text-center p-3`}>
|
|
|
|
Server is offline.
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
</TitledGreyBox>
|
|
|
|
<TitledGreyBox title={'Outbound Data'} icon={faEthernet}>
|
|
|
|
{status !== 'offline' ?
|
|
|
|
<canvas id={'tx_chart'} ref={txRef} aria-label={'Server Outbound Data'} role={'img'}/>
|
|
|
|
:
|
|
|
|
<p css={tw`text-xs text-neutral-400 text-center p-3`}>
|
|
|
|
Server is offline.
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
</TitledGreyBox>
|
2019-09-29 20:23:15 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|