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
label="Check Interval"
label="Check Interval(s)"
name="interval"
rules={[{ required: true }]}
>

View File

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