refactor: update monitor recent data strategy

This commit is contained in:
moonrailgun 2023-10-24 22:25:44 +08:00
parent dd6b4943f9
commit c161219137
2 changed files with 42 additions and 31 deletions

View File

@ -75,7 +75,7 @@ export const MonitorInfoEditor: React.FC<MonitorInfoEditorProps> = React.memo(
</Form.Item> </Form.Item>
<Form.Item <Form.Item
label="Check Interval" label="Check Interval(s)"
name="interval" name="interval"
rules={[{ required: true }]} rules={[{ required: true }]}
> >

View File

@ -183,11 +183,14 @@ 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', { const subscribedDataList = useSocketSubscribeList(
'onMonitorReceiveNewData',
{
filter: (data) => { filter: (data) => {
return data.monitorId === props.monitorId; return data.monitorId === props.monitorId;
}, },
}); }
);
const range = useMemo((): [Dayjs, Dayjs] => { const range = useMemo((): [Dayjs, Dayjs] => {
if (rangeType === '3h') { if (rangeType === '3h') {
@ -206,6 +209,12 @@ const MonitorDataChart: React.FC<{ monitorId: string }> = React.memo(
return [dayjs().subtract(0.5, 'hour'), dayjs()]; return [dayjs().subtract(0.5, 'hour'), dayjs()];
}, [rangeType]); }, [rangeType]);
const { data: _recentData = [] } = trpc.monitor.recentData.useQuery({
workspaceId,
monitorId,
take: 40,
});
const { data: _data = [] } = trpc.monitor.data.useQuery({ const { data: _data = [] } = trpc.monitor.data.useQuery({
workspaceId, workspaceId,
monitorId, monitorId,
@ -216,8 +225,11 @@ 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 = uniqBy([..._data, ...newDataList], 'createdAt').map( let fetchedData = rangeType === 'recent' ? _recentData : _data;
(d, i, arr) => { const data = uniqBy(
[...fetchedData, ...subscribedDataList],
'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();
@ -240,11 +252,10 @@ const MonitorDataChart: React.FC<{ monitorId: string }> = React.memo(
value, value,
time, time,
}; };
} });
);
return { data, annotations }; return { data, annotations };
}, [_data, newDataList]); }, [_recentData, _data, subscribedDataList]);
const config = useMemo<AreaConfig>(() => { const config = useMemo<AreaConfig>(() => {
return { return {