tianji/src/client/pages/Servers.tsx

153 lines
3.7 KiB
TypeScript
Raw Normal View History

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(() => {
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';
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-10-03 12:45:00 +00:00
const columns = useMemo((): ColumnsType<ServerStatusInfo> => {
return [
{
2023-10-03 12:45:00 +00:00
key: 'status',
title: 'Status',
2023-10-03 12:45:00 +00:00
render: (val, record) => {
return Date.now() - (record.updatedAt + record.timeout) < 0
? 'online'
: 'offline';
},
},
{
2023-10-03 12:45:00 +00:00
dataIndex: 'name',
title: 'Node Name',
},
{
2023-10-03 12:45:00 +00:00
dataIndex: 'hostname',
title: 'Host Name',
},
{
2023-10-03 12:45:00 +00:00
dataIndex: ['payload', 'system'],
title: 'System',
},
{
dataIndex: ['payload', 'uptime'],
title: 'Uptime',
2023-10-03 12:45:00 +00:00
render: (val) => prettyMilliseconds(Number(val) * 1000),
},
{
2023-10-03 12:45:00 +00:00
dataIndex: ['payload', 'load'],
title: 'Load',
},
{
2023-10-03 12:45:00 +00:00
key: 'nework',
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-10-03 12:45:00 +00:00
key: 'traffic',
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-10-03 12:45:00 +00:00
dataIndex: ['payload', 'cpu'],
title: 'cpu',
2023-10-03 12:45:00 +00:00
render: (val) => `${val}%`,
},
{
2023-10-03 12:45:00 +00:00
key: 'ram',
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-10-03 12:45:00 +00:00
key: 'hdd',
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-10-03 12:45:00 +00:00
return (
<Table
rowKey="hostname"
columns={columns}
dataSource={dataSource}
pagination={false}
/>
);
});
ServerList.displayName = 'ServerList';