From 896170836c3f89340feae6cfe5dadf3cacad779f Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 1 Nov 2023 00:39:37 +0800 Subject: [PATCH] refactor: improve server status list data display --- reporter/main.go | 2 +- src/client/hooks/useIntervalUpdate.ts | 17 ++++++++ src/client/pages/Servers.tsx | 56 +++++++++++++++++++++++---- 3 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 src/client/hooks/useIntervalUpdate.ts diff --git a/reporter/main.go b/reporter/main.go index 91663d2..71d8bca 100644 --- a/reporter/main.go +++ b/reporter/main.go @@ -27,7 +27,7 @@ var ( Url = flag.String("url", "", "The http url of tianji, for example: https://tianji.msgbyte.com") WorkspaceId = flag.String("workspace", "", "The workspace id for tianji, this should be a uuid") Name = flag.String("name", "", "The identification name for this machine") - Interval = flag.Int("interval", 20.0, "Input the INTERVAL") + Interval = flag.Int("interval", 10.0, "Input the INTERVAL, seconed") IsVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only") ) diff --git a/src/client/hooks/useIntervalUpdate.ts b/src/client/hooks/useIntervalUpdate.ts new file mode 100644 index 0000000..15b7103 --- /dev/null +++ b/src/client/hooks/useIntervalUpdate.ts @@ -0,0 +1,17 @@ +import { useEffect, useReducer } from 'react'; + +export function useIntervalUpdate(timeout: number) { + // update list in 10 second + const [inc, update] = useReducer((state) => state + 1, 0); + useEffect(() => { + const timer = window.setInterval(() => { + update(); + }, timeout); + + return () => { + window.clearInterval(timer); + }; + }, []); + + return inc; +} diff --git a/src/client/pages/Servers.tsx b/src/client/pages/Servers.tsx index 93e4057..38a90c8 100644 --- a/src/client/pages/Servers.tsx +++ b/src/client/pages/Servers.tsx @@ -1,12 +1,15 @@ -import React, { useEffect, useMemo, useRef, useState } from 'react'; +import React, { useEffect, useMemo, useReducer, useRef, useState } from 'react'; import { Badge, Button, + Empty, Form, Input, Modal, Steps, + Switch, Table, + Tooltip, Typography, } from 'antd'; import { ColumnsType } from 'antd/es/table'; @@ -22,9 +25,12 @@ import { useCurrentWorkspaceId } from '../store/user'; import { useWatch } from '../hooks/useWatch'; import { Loading } from '../components/Loading'; import { without } from 'lodash-es'; +import { useIntervalUpdate } from '../hooks/useIntervalUpdate'; +import clsx from 'clsx'; export const Servers: React.FC = React.memo(() => { const [isModalOpen, setIsModalOpen] = useState(false); + const [hideOfflineServer, setHideOfflineServer] = useState(false); const handleOk = () => { setIsModalOpen(false); @@ -34,7 +40,15 @@ export const Servers: React.FC = React.memo(() => {
Servers
-
+
+
+ + Hide Offline +
+
- + { return serverMap; } -export const ServerList: React.FC = React.memo(() => { +export const ServerList: React.FC<{ + hideOfflineServer: boolean; +}> = React.memo((props) => { const serverMap = useServerMap(); + const inc = useIntervalUpdate(2 * 1000); + const { hideOfflineServer } = props; - const dataSource = Object.values(serverMap); + const dataSource = useMemo( + () => + Object.values(serverMap) + .sort((info) => (isServerOnline(info) ? -1 : 1)) + .filter((info) => { + if (hideOfflineServer) { + return isServerOnline(info); + } + + return true; + }), // make online server is up and offline is down + [serverMap, inc, hideOfflineServer] + ); const lastUpdatedAt = max(dataSource.map((d) => d.updatedAt)); const columns = useMemo((): ColumnsType => { @@ -86,10 +116,16 @@ export const ServerList: React.FC = React.memo(() => { key: 'status', title: 'Status', render: (val, record) => { - return Date.now() - (record.updatedAt + record.timeout) < 0 ? ( + return isServerOnline(record) ? ( ) : ( - + + + ); }, }, @@ -174,6 +210,8 @@ export const ServerList: React.FC = React.memo(() => { columns={columns} dataSource={dataSource} pagination={false} + locale={{ emptyText: }} + rowClassName={(record) => clsx(!isServerOnline(record) && 'opacity-80')} />
); @@ -274,3 +312,7 @@ export const AddServerStep: React.FC = React.memo(() => { ); }); AddServerStep.displayName = 'AddServerStep'; + +function isServerOnline(info: ServerStatusInfo): boolean { + return new Date(info.updatedAt).valueOf() + info.timeout * 1000 > Date.now(); +}