2023-09-02 07:40:38 +00:00
|
|
|
import React, { useMemo, useState } from 'react';
|
|
|
|
import { Button, Form, Input, Modal, Table } from 'antd';
|
|
|
|
import { ColumnsType } from 'antd/es/table';
|
|
|
|
import { PlusOutlined } from '@ant-design/icons';
|
2023-10-03 12:45:00 +00:00
|
|
|
import { ServerStatusInfo } from '../../types';
|
|
|
|
import { useSocketSubscribe } from '../api/socketio';
|
|
|
|
import { filesize } from 'filesize';
|
|
|
|
import prettyMilliseconds from 'pretty-ms';
|
2023-10-03 12:57:24 +00:00
|
|
|
import { UpDownCounter } from '../components/UpDownCounter';
|
2023-08-31 16:11:47 +00:00
|
|
|
|
|
|
|
export const Servers: React.FC = React.memo(() => {
|
2023-09-02 07:40:38 +00:00
|
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
|
|
|
|
|
|
const handleOk = () => {
|
|
|
|
setIsModalOpen(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="h-24 flex items-center">
|
|
|
|
<div className="text-2xl flex-1">Servers</div>
|
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
icon={<PlusOutlined />}
|
|
|
|
size="large"
|
|
|
|
onClick={() => setIsModalOpen(true)}
|
|
|
|
>
|
|
|
|
Add Server
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<ServerList />
|
|
|
|
|
|
|
|
<Modal
|
|
|
|
title="Add Server"
|
|
|
|
open={isModalOpen}
|
|
|
|
onOk={handleOk}
|
|
|
|
onCancel={() => setIsModalOpen(false)}
|
|
|
|
>
|
|
|
|
<Form layout="vertical">
|
|
|
|
<Form.Item label="Server Name">
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
);
|
2023-08-31 16:11:47 +00:00
|
|
|
});
|
|
|
|
Servers.displayName = 'Servers';
|
2023-09-02 07:40:38 +00:00
|
|
|
|
|
|
|
export const ServerList: React.FC = React.memo(() => {
|
2023-10-03 12:45:00 +00:00
|
|
|
const serverMap = useSocketSubscribe<Record<string, ServerStatusInfo>>(
|
|
|
|
'onServerStatusUpdate',
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
const dataSource = Object.values(serverMap);
|
2023-09-02 07:40:38 +00:00
|
|
|
|
2023-10-03 12:45:00 +00:00
|
|
|
const columns = useMemo((): ColumnsType<ServerStatusInfo> => {
|
2023-09-02 07:40:38 +00:00
|
|
|
return [
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
key: 'status',
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'Status',
|
2023-10-03 12:45:00 +00:00
|
|
|
render: (val, record) => {
|
|
|
|
return Date.now() - (record.updatedAt + record.timeout) < 0
|
|
|
|
? 'online'
|
|
|
|
: 'offline';
|
|
|
|
},
|
2023-09-02 07:40:38 +00:00
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
dataIndex: 'name',
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'Node Name',
|
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
dataIndex: 'hostname',
|
|
|
|
title: 'Host Name',
|
2023-09-02 07:40:38 +00:00
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
dataIndex: ['payload', 'system'],
|
|
|
|
title: 'System',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dataIndex: ['payload', 'uptime'],
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'Uptime',
|
2023-10-03 12:45:00 +00:00
|
|
|
render: (val) => prettyMilliseconds(Number(val) * 1000),
|
2023-09-02 07:40:38 +00:00
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
dataIndex: ['payload', 'load'],
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'Load',
|
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
key: 'nework',
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'Network',
|
2023-10-03 12:45:00 +00:00
|
|
|
render: (_, record) => {
|
2023-10-03 12:57:24 +00:00
|
|
|
return (
|
|
|
|
<UpDownCounter
|
|
|
|
up={filesize(record.payload.network_out)}
|
|
|
|
down={filesize(record.payload.network_in)}
|
|
|
|
/>
|
|
|
|
);
|
2023-10-03 12:45:00 +00:00
|
|
|
},
|
2023-09-02 07:40:38 +00:00
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
key: 'traffic',
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'Traffic',
|
2023-10-03 12:45:00 +00:00
|
|
|
render: (_, record) => {
|
2023-10-03 12:57:24 +00:00
|
|
|
return (
|
|
|
|
<UpDownCounter
|
|
|
|
up={filesize(record.payload.network_tx) + '/s'}
|
|
|
|
down={filesize(record.payload.network_rx) + '/s'}
|
|
|
|
/>
|
|
|
|
);
|
2023-10-03 12:45:00 +00:00
|
|
|
},
|
2023-09-02 07:40:38 +00:00
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
dataIndex: ['payload', 'cpu'],
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'cpu',
|
2023-10-03 12:45:00 +00:00
|
|
|
render: (val) => `${val}%`,
|
2023-09-02 07:40:38 +00:00
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
key: 'ram',
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'ram',
|
2023-10-03 12:45:00 +00:00
|
|
|
render: (_, record) => {
|
|
|
|
return `${filesize(record.payload.memory_used * 1000)} / ${filesize(
|
|
|
|
record.payload.memory_total * 1000
|
|
|
|
)}`;
|
|
|
|
},
|
2023-09-02 07:40:38 +00:00
|
|
|
},
|
|
|
|
{
|
2023-10-03 12:45:00 +00:00
|
|
|
key: 'hdd',
|
2023-09-02 07:40:38 +00:00
|
|
|
title: 'hdd',
|
2023-10-03 12:45:00 +00:00
|
|
|
render: (_, record) => {
|
|
|
|
return `${filesize(record.payload.hdd_used * 1000)} / ${filesize(
|
|
|
|
record.payload.hdd_total * 1000
|
|
|
|
)}`;
|
|
|
|
},
|
2023-09-02 07:40:38 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}, []);
|
|
|
|
|
2023-10-03 12:45:00 +00:00
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
rowKey="hostname"
|
|
|
|
columns={columns}
|
|
|
|
dataSource={dataSource}
|
|
|
|
pagination={false}
|
|
|
|
/>
|
|
|
|
);
|
2023-09-02 07:40:38 +00:00
|
|
|
});
|
|
|
|
ServerList.displayName = 'ServerList';
|