tianji/src/client/pages/Servers.tsx

277 lines
7.0 KiB
TypeScript
Raw Normal View History

2023-10-14 17:35:20 +00:00
import React, { useEffect, useMemo, useRef, useState } from 'react';
import {
Badge,
Button,
Form,
Input,
Modal,
Steps,
Table,
Typography,
} 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-10-03 13:06:58 +00:00
import { max } from 'lodash-es';
import dayjs from 'dayjs';
2023-10-14 17:35:20 +00:00
import { useCurrentWorkspaceId } from '../store/user';
import { useWatch } from '../hooks/useWatch';
import { Loading } from '../components/Loading';
import { without } from 'lodash-es';
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}
2023-10-14 17:35:20 +00:00
destroyOnClose={true}
okText="Done"
onOk={handleOk}
onCancel={() => setIsModalOpen(false)}
>
2023-10-14 17:35:20 +00:00
<div>
<AddServerStep />
</div>
</Modal>
</div>
);
2023-08-31 16:11:47 +00:00
});
Servers.displayName = 'Servers';
2023-10-14 17:35:20 +00:00
function useServerMap(): Record<string, ServerStatusInfo> {
2023-10-03 12:45:00 +00:00
const serverMap = useSocketSubscribe<Record<string, ServerStatusInfo>>(
'onServerStatusUpdate',
{}
);
2023-10-14 17:35:20 +00:00
return serverMap;
}
export const ServerList: React.FC = React.memo(() => {
const serverMap = useServerMap();
2023-10-03 12:45:00 +00:00
const dataSource = Object.values(serverMap);
2023-10-03 13:06:58 +00:00
const lastUpdatedAt = max(dataSource.map((d) => d.updatedAt));
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 ? (
<Badge status="success" text="online" />
) : (
<Badge status="error" text="offline" />
);
2023-10-03 12:45:00 +00:00
},
},
{
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 (
2023-10-03 13:06:58 +00:00
<div>
<div className="text-right text-sm opacity-80">
Last updated at: {dayjs(lastUpdatedAt).format('YYYY-MM-DD HH:mm:ss')}
</div>
<Table
rowKey="hostname"
columns={columns}
dataSource={dataSource}
pagination={false}
/>
</div>
2023-10-03 12:45:00 +00:00
);
});
ServerList.displayName = 'ServerList';
2023-10-14 17:35:20 +00:00
export const AddServerStep: React.FC = React.memo(() => {
const workspaceId = useCurrentWorkspaceId();
const [current, setCurrent] = useState(0);
const serverMap = useServerMap();
const [checking, setChecking] = useState(false);
const oldServerMapNames = useRef<string[]>([]);
const [diffServerNames, setDiffServerNames] = useState<string[]>([]);
const allServerNames = useMemo(() => Object.keys(serverMap), [serverMap]);
useWatch([checking], () => {
if (checking === true) {
oldServerMapNames.current = [...allServerNames];
}
});
useWatch([allServerNames], () => {
if (checking === true) {
setDiffServerNames(without(allServerNames, ...oldServerMapNames.current));
}
});
return (
<Steps
direction="vertical"
current={current}
items={[
{
title: 'Download Client Reportor',
description: (
<div>
Download reporter from{' '}
<Typography.Link
href="https://github.com/msgbyte/tianji/releases"
target="_blank"
onClick={() => {
if (current === 0) {
setCurrent(1);
setChecking(true);
}
}}
>
Releases Page
</Typography.Link>
</div>
),
},
{
title: 'Run',
description: (
<div>
run reporter with{' '}
<Typography.Text code={true} copyable={true}>
./reporter --url {window.location.origin} --workspace{' '}
{workspaceId}
</Typography.Text>
<Button
type="link"
size="small"
disabled={current !== 1}
onClick={() => {
if (current === 1) {
setCurrent(2);
setChecking(true);
}
}}
>
Next step
</Button>
</div>
),
},
{
title: 'Waiting for receive UDP pack',
description: (
<div>
{diffServerNames.length === 0 || checking === false ? (
<Loading />
) : (
<div>
Is this your servers?
{diffServerNames.map((n) => (
<div key={n}>- {n}</div>
))}
</div>
)}
</div>
),
},
]}
/>
);
});
AddServerStep.displayName = 'AddServerStep';