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(
filter: (data) => { 'onMonitorReceiveNewData',
return data.monitorId === props.monitorId; {
}, filter: (data) => {
}); 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,35 +225,37 @@ 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(
const value = d.value > 0 ? d.value : null; [...fetchedData, ...subscribedDataList],
const time = dayjs(d.createdAt).valueOf(); 'createdAt'
).map((d, i, arr) => {
const value = d.value > 0 ? d.value : null;
const time = dayjs(d.createdAt).valueOf();
if (!value && !start && arr[i - 1]) { if (!value && !start && arr[i - 1]) {
start = dayjs(arr[i - 1]['createdAt']).valueOf(); start = dayjs(arr[i - 1]['createdAt']).valueOf();
} else if (value && start) { } else if (value && start) {
annotations.push({ annotations.push({
type: 'region', type: 'region',
start: [start, 'min'], start: [start, 'min'],
end: [time, 'max'], end: [time, 'max'],
style: { style: {
fill: 'red', fill: 'red',
fillOpacity: 0.25, fillOpacity: 0.25,
}, },
}); });
start = null; start = null;
}
return {
value,
time,
};
} }
);
return {
value,
time,
};
});
return { data, annotations }; return { data, annotations };
}, [_data, newDataList]); }, [_recentData, _data, subscribedDataList]);
const config = useMemo<AreaConfig>(() => { const config = useMemo<AreaConfig>(() => {
return { return {