2019-09-18 05:33:14 +00:00
|
|
|
import React, { lazy, useEffect, useState } from 'react';
|
2019-07-10 04:25:57 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2019-09-18 04:59:35 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-07-05 01:46:09 +00:00
|
|
|
import { faCircle, faHdd, faMemory, faMicrochip, faServer } from '@fortawesome/free-solid-svg-icons';
|
2019-09-18 05:33:14 +00:00
|
|
|
import { bytesToHuman } from '@/helpers';
|
2019-09-28 21:59:05 +00:00
|
|
|
import SuspenseSpinner from '@/components/elements/SuspenseSpinner';
|
2019-09-29 20:23:15 +00:00
|
|
|
import TitledGreyBox from '@/components/elements/TitledGreyBox';
|
2020-03-30 05:12:50 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-04-17 18:17:01 +00:00
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
2020-04-26 20:21:39 +00:00
|
|
|
import ContentContainer from '@/components/elements/ContentContainer';
|
2020-07-04 22:40:41 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import StopOrKillButton from '@/components/server/StopOrKillButton';
|
2019-09-18 05:33:14 +00:00
|
|
|
|
2020-07-04 22:40:41 +00:00
|
|
|
export type PowerAction = 'start' | 'stop' | 'restart' | 'kill';
|
2019-09-28 20:29:49 +00:00
|
|
|
|
2019-10-12 22:29:45 +00:00
|
|
|
const ChunkedConsole = lazy(() => import(/* webpackChunkName: "console" */'@/components/server/Console'));
|
|
|
|
const ChunkedStatGraphs = lazy(() => import(/* webpackChunkName: "graphs" */'@/components/server/StatGraphs'));
|
2019-06-29 05:17:29 +00:00
|
|
|
|
2019-06-29 23:59:50 +00:00
|
|
|
export default () => {
|
2019-09-18 05:33:14 +00:00
|
|
|
const [ memory, setMemory ] = useState(0);
|
|
|
|
const [ cpu, setCpu ] = useState(0);
|
2020-04-12 03:57:49 +00:00
|
|
|
const [ disk, setDisk ] = useState(0);
|
2019-09-18 05:33:14 +00:00
|
|
|
|
2019-09-18 04:59:35 +00:00
|
|
|
const server = ServerContext.useStoreState(state => state.server.data!);
|
2019-07-10 04:25:57 +00:00
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
2019-06-29 23:59:50 +00:00
|
|
|
|
2019-09-18 05:33:14 +00:00
|
|
|
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
|
|
|
|
|
|
|
|
const statsListener = (data: string) => {
|
|
|
|
let stats: any = {};
|
|
|
|
try {
|
|
|
|
stats = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setMemory(stats.memory_bytes);
|
|
|
|
setCpu(stats.cpu_absolute);
|
2020-04-12 03:57:49 +00:00
|
|
|
setDisk(stats.disk_bytes);
|
2019-09-18 05:33:14 +00:00
|
|
|
};
|
|
|
|
|
2019-09-28 20:29:49 +00:00
|
|
|
const sendPowerCommand = (command: PowerAction) => {
|
2019-09-18 06:12:38 +00:00
|
|
|
instance && instance.send('set state', command);
|
|
|
|
};
|
|
|
|
|
2019-09-18 05:33:14 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!connected || !instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.addListener('stats', statsListener);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
instance.removeListener('stats', statsListener);
|
|
|
|
};
|
2019-12-07 19:11:40 +00:00
|
|
|
}, [ instance, connected ]);
|
2019-09-18 05:33:14 +00:00
|
|
|
|
2020-07-04 22:40:41 +00:00
|
|
|
const disklimit = server.limits.disk ? bytesToHuman(server.limits.disk * 1000 * 1000) : 'Unlimited';
|
|
|
|
const memorylimit = server.limits.memory ? bytesToHuman(server.limits.memory * 1000 * 1000) : 'Unlimited';
|
2020-05-16 07:16:06 +00:00
|
|
|
|
2019-06-29 23:59:50 +00:00
|
|
|
return (
|
2020-07-04 22:40:41 +00:00
|
|
|
<PageContentBlock css={tw`flex`}>
|
|
|
|
<div css={tw`w-1/4`}>
|
2019-09-29 20:23:15 +00:00
|
|
|
<TitledGreyBox title={server.name} icon={faServer}>
|
2020-07-04 22:40:41 +00:00
|
|
|
<p css={tw`text-xs uppercase`}>
|
2019-09-29 20:23:15 +00:00
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faCircle}
|
2020-07-04 22:40:41 +00:00
|
|
|
fixedWidth
|
2020-07-05 02:01:49 +00:00
|
|
|
css={[
|
|
|
|
tw`mr-1`,
|
|
|
|
status === 'offline' ? tw`text-red-500` : (status === 'running' ? tw`text-green-500` : tw`text-yellow-500`),
|
|
|
|
]}
|
2019-09-29 20:23:15 +00:00
|
|
|
/>
|
|
|
|
{status}
|
|
|
|
</p>
|
2020-07-04 22:40:41 +00:00
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faMicrochip} fixedWidth css={tw`mr-1`}/> {cpu.toFixed(2)}%
|
2020-04-12 03:57:49 +00:00
|
|
|
</p>
|
2020-07-04 22:40:41 +00:00
|
|
|
<p css={tw`text-xs mt-2`}>
|
|
|
|
<FontAwesomeIcon icon={faMemory} fixedWidth css={tw`mr-1`}/> {bytesToHuman(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(disk)}
|
|
|
|
<span css={tw`text-neutral-500`}> / {disklimit}</span>
|
2019-09-29 20:23:15 +00:00
|
|
|
</p>
|
|
|
|
</TitledGreyBox>
|
2020-04-26 20:21:39 +00:00
|
|
|
{!server.isInstalling ?
|
2020-07-04 22:40:41 +00:00
|
|
|
<Can action={[ 'control.start', 'control.stop', 'control.restart' ]} matchAny>
|
|
|
|
<div css={tw`shadow-md bg-neutral-700 rounded p-3 flex text-xs mt-4 justify-center`}>
|
2020-04-26 20:21:39 +00:00
|
|
|
<Can action={'control.start'}>
|
2020-07-04 22:40:41 +00:00
|
|
|
<Button
|
|
|
|
size={'xsmall'}
|
|
|
|
color={'green'}
|
|
|
|
isSecondary
|
|
|
|
css={tw`mr-2`}
|
2020-04-26 20:21:39 +00:00
|
|
|
disabled={status !== 'offline'}
|
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
sendPowerCommand('start');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Start
|
2020-07-04 22:40:41 +00:00
|
|
|
</Button>
|
2020-04-26 20:21:39 +00:00
|
|
|
</Can>
|
|
|
|
<Can action={'control.restart'}>
|
2020-07-04 22:40:41 +00:00
|
|
|
<Button
|
|
|
|
size={'xsmall'}
|
|
|
|
isSecondary
|
|
|
|
css={tw`mr-2`}
|
2020-04-26 20:21:39 +00:00
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
sendPowerCommand('restart');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Restart
|
2020-07-04 22:40:41 +00:00
|
|
|
</Button>
|
2020-04-26 20:21:39 +00:00
|
|
|
</Can>
|
|
|
|
<Can action={'control.stop'}>
|
|
|
|
<StopOrKillButton onPress={action => sendPowerCommand(action)}/>
|
|
|
|
</Can>
|
|
|
|
</div>
|
|
|
|
</Can>
|
|
|
|
:
|
2020-07-04 22:40:41 +00:00
|
|
|
<div css={tw`mt-4 rounded bg-yellow-500 p-3`}>
|
2020-04-26 20:21:39 +00:00
|
|
|
<ContentContainer>
|
2020-07-04 22:40:41 +00:00
|
|
|
<p css={tw`text-sm text-yellow-900`}>
|
2020-04-26 20:21:39 +00:00
|
|
|
This server is currently running its installation process and most actions are
|
|
|
|
unavailable.
|
|
|
|
</p>
|
|
|
|
</ContentContainer>
|
2020-03-30 05:12:50 +00:00
|
|
|
</div>
|
2020-04-26 20:21:39 +00:00
|
|
|
}
|
2019-09-18 04:59:35 +00:00
|
|
|
</div>
|
2020-07-04 22:40:41 +00:00
|
|
|
<div css={tw`flex-1 ml-4`}>
|
2019-09-29 20:23:15 +00:00
|
|
|
<SuspenseSpinner>
|
2019-09-18 05:33:14 +00:00
|
|
|
<ChunkedConsole/>
|
2019-12-07 19:11:40 +00:00
|
|
|
<ChunkedStatGraphs/>
|
2019-09-29 20:23:15 +00:00
|
|
|
</SuspenseSpinner>
|
|
|
|
</div>
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2019-06-29 23:59:50 +00:00
|
|
|
);
|
|
|
|
};
|