feat: add monitor chart livetime update

This commit is contained in:
moonrailgun 2023-10-11 01:21:05 +08:00
parent f53fba35f4
commit 5a3a621fd6
2 changed files with 36 additions and 23 deletions

View File

@ -9,6 +9,8 @@ import { NotFoundTip } from '../NotFoundTip';
import { MonitorInfo as MonitorInfoType } from '../../../types'; import { MonitorInfo as MonitorInfoType } from '../../../types';
import { Area, AreaConfig } from '@ant-design/charts'; import { Area, AreaConfig } from '@ant-design/charts';
import { MonitorHealthBar } from './MonitorHealthBar'; import { MonitorHealthBar } from './MonitorHealthBar';
import { useSocketSubscribeList } from '../../api/socketio';
import { uniqBy } from 'lodash';
interface MonitorInfoProps { interface MonitorInfoProps {
monitorId: string; monitorId: string;
@ -74,6 +76,11 @@ const MonitorDataChart: React.FC<{ monitorId: string }> = React.memo(
const workspaceId = useCurrentWorkspaceId(); const workspaceId = useCurrentWorkspaceId();
const { monitorId } = props; const { monitorId } = props;
const [rangeType, setRangeType] = useState('recent'); const [rangeType, setRangeType] = useState('recent');
const newDataList = useSocketSubscribeList('onMonitorReceiveNewData', {
filter: (data) => {
return data.monitorId === props.monitorId;
},
});
const range = useMemo((): [Dayjs, Dayjs] => { const range = useMemo((): [Dayjs, Dayjs] => {
if (rangeType === '3h') { if (rangeType === '3h') {
@ -102,7 +109,8 @@ const MonitorDataChart: React.FC<{ monitorId: string }> = React.memo(
const { data, annotations } = useMemo(() => { const { data, annotations } = useMemo(() => {
const annotations: AreaConfig['annotations'] = []; const annotations: AreaConfig['annotations'] = [];
let start: number | null = null; let start: number | null = null;
const data = _data.map((d, i, arr) => { const data = uniqBy([..._data, ...newDataList], 'createdAt').map(
(d, i, arr) => {
const value = d.value > 0 ? d.value : null; const value = d.value > 0 ? d.value : null;
const time = dayjs(d.createdAt).valueOf(); const time = dayjs(d.createdAt).valueOf();
@ -125,10 +133,11 @@ const MonitorDataChart: React.FC<{ monitorId: string }> = React.memo(
value, value,
time, time,
}; };
}); }
);
return { data, annotations }; return { data, annotations };
}, [_data]); }, [_data, newDataList]);
const config = useMemo<AreaConfig>(() => { const config = useMemo<AreaConfig>(() => {
return { return {

View File

@ -101,6 +101,10 @@ export const monitorRouter = router({
monitorId, monitorId,
}, },
take: -take, take: -take,
select: {
value: true,
createdAt: true,
},
}); });
}), }),
}); });