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';
|
|
|
|
import { faServer } from '@fortawesome/free-solid-svg-icons/faServer';
|
|
|
|
import { faCircle } from '@fortawesome/free-solid-svg-icons/faCircle';
|
|
|
|
import classNames from 'classnames';
|
2019-09-18 05:33:14 +00:00
|
|
|
import { faMemory } from '@fortawesome/free-solid-svg-icons/faMemory';
|
|
|
|
import { faMicrochip } from '@fortawesome/free-solid-svg-icons/faMicrochip';
|
2020-04-12 03:57:49 +00:00
|
|
|
import { faHdd } from '@fortawesome/free-solid-svg-icons/faHdd';
|
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';
|
2019-09-18 05:33:14 +00:00
|
|
|
|
2019-09-28 20:29:49 +00:00
|
|
|
type PowerAction = 'start' | 'stop' | 'restart' | 'kill';
|
|
|
|
|
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-09-28 20:29:49 +00:00
|
|
|
const StopOrKillButton = ({ onPress }: { onPress: (action: PowerAction) => void }) => {
|
2019-09-28 20:17:51 +00:00
|
|
|
const [ clicked, setClicked ] = useState(false);
|
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2019-09-28 21:59:05 +00:00
|
|
|
setClicked(state => [ 'stopping' ].indexOf(status) < 0 ? false : state);
|
|
|
|
}, [ status ]);
|
2019-09-28 20:17:51 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className={'btn btn-red btn-xs'}
|
|
|
|
disabled={status === 'offline'}
|
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
onPress(clicked ? 'kill' : 'stop');
|
|
|
|
setClicked(true);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{clicked ? 'Kill' : 'Stop'}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2019-06-29 23:59:50 +00:00
|
|
|
return (
|
2020-04-17 18:17:01 +00:00
|
|
|
<PageContentBlock className={'flex'}>
|
2019-12-23 05:22:08 +00:00
|
|
|
<div className={'w-1/4'}>
|
2019-09-29 20:23:15 +00:00
|
|
|
<TitledGreyBox title={server.name} icon={faServer}>
|
|
|
|
<p className={'text-xs uppercase'}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faCircle}
|
|
|
|
fixedWidth={true}
|
|
|
|
className={classNames('mr-1', {
|
|
|
|
'text-red-500': status === 'offline',
|
|
|
|
'text-yellow-500': [ 'running', 'offline' ].indexOf(status) < 0,
|
|
|
|
'text-green-500': status === 'running',
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
{status}
|
|
|
|
</p>
|
2020-04-12 03:57:49 +00:00
|
|
|
<p className={'text-xs mt-2'}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faMicrochip}
|
|
|
|
fixedWidth={true}
|
|
|
|
className={'mr-1'}
|
|
|
|
/>
|
|
|
|
{cpu.toFixed(2)} %
|
|
|
|
</p>
|
2019-09-29 20:23:15 +00:00
|
|
|
<p className={'text-xs mt-2'}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faMemory}
|
|
|
|
fixedWidth={true}
|
|
|
|
className={'mr-1'}
|
|
|
|
/>
|
|
|
|
{bytesToHuman(memory)}
|
2020-04-13 22:40:04 +00:00
|
|
|
<span className={'text-neutral-500'}> / {server.limits.memory} MB</span>
|
2019-09-29 20:23:15 +00:00
|
|
|
</p>
|
|
|
|
<p className={'text-xs mt-2'}>
|
|
|
|
<FontAwesomeIcon
|
2020-04-12 03:57:49 +00:00
|
|
|
icon={faHdd}
|
2019-09-29 20:23:15 +00:00
|
|
|
fixedWidth={true}
|
|
|
|
className={'mr-1'}
|
|
|
|
/>
|
2020-04-12 03:57:49 +00:00
|
|
|
{bytesToHuman(disk)}
|
2020-04-13 22:40:04 +00:00
|
|
|
<span className={'text-neutral-500'}> / {server.limits.disk} MB</span>
|
2019-09-29 20:23:15 +00:00
|
|
|
</p>
|
|
|
|
</TitledGreyBox>
|
2020-03-30 05:12:50 +00:00
|
|
|
<Can action={[ 'control.start', 'control.stop', 'control.restart' ]} matchAny={true}>
|
|
|
|
<div className={'grey-box justify-center'}>
|
|
|
|
<Can action={'control.start'}>
|
|
|
|
<button
|
|
|
|
className={'btn btn-secondary btn-xs mr-2'}
|
|
|
|
disabled={status !== 'offline'}
|
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
sendPowerCommand('start');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Start
|
|
|
|
</button>
|
|
|
|
</Can>
|
|
|
|
<Can action={'control.restart'}>
|
|
|
|
<button
|
|
|
|
className={'btn btn-secondary btn-xs mr-2'}
|
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
sendPowerCommand('restart');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Restart
|
|
|
|
</button>
|
|
|
|
</Can>
|
|
|
|
<Can action={'control.stop'}>
|
|
|
|
<StopOrKillButton onPress={action => sendPowerCommand(action)}/>
|
|
|
|
</Can>
|
|
|
|
</div>
|
|
|
|
</Can>
|
2019-09-18 04:59:35 +00:00
|
|
|
</div>
|
2020-04-17 19:41:47 +00:00
|
|
|
<div className={'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
|
|
|
);
|
|
|
|
};
|