perf(monitor): aggregate realtime workspace

This commit is contained in:
lingniu
2026-07-16 03:26:14 +08:00
parent 01150844df
commit cad39d6326
12 changed files with 147 additions and 28 deletions

View File

@@ -10,9 +10,14 @@ afterEach(() => {
vi.restoreAllMocks();
});
test('rapid viewport changes retain at most one monitor map payload', async () => {
vi.spyOn(api, 'monitorSummary').mockResolvedValue({ totalVehicles: 0, onlineVehicles: 0, offlineVehicles: 0, drivingVehicles: 0, idleVehicles: 0, unknownVehicles: 0, alertVehicles: 0, activeToday: 0, frameToday: 0, alertDataAvailable: false, truncated: false, asOf: '' });
const monitorMap = vi.spyOn(api, 'monitorMap').mockResolvedValue({ mode: 'points', zoom: 13, total: 0, truncated: false, points: [], clusters: [], asOf: '' });
test('rapid viewport changes retain one workspace payload and avoid duplicate fleet requests', async () => {
const monitorSummary = vi.spyOn(api, 'monitorSummary').mockResolvedValue({ totalVehicles: 0, onlineVehicles: 0, offlineVehicles: 0, drivingVehicles: 0, idleVehicles: 0, unknownVehicles: 0, alertVehicles: 0, activeToday: 0, frameToday: 0, alertDataAvailable: false, truncated: false, asOf: '' });
const vehicleRealtime = vi.spyOn(api, 'vehicleRealtime').mockResolvedValue({ items: [], total: 0, limit: 200, offset: 0 });
const monitorWorkspace = vi.spyOn(api, 'monitorWorkspace').mockResolvedValue({
summary: { totalVehicles: 0, onlineVehicles: 0, offlineVehicles: 0, drivingVehicles: 0, idleVehicles: 0, unknownVehicles: 0, alertVehicles: 0, activeToday: 0, frameToday: 0, alertDataAvailable: false, truncated: false, asOf: '' },
vehicles: { items: [], total: 0, limit: 200, offset: 0 },
map: { mode: 'points', zoom: 13, total: 0, truncated: false, points: [], clusters: [], asOf: '' }
});
const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const wrapper = ({ children }: { children: ReactNode }) => <QueryClientProvider client={client}>{children}</QueryClientProvider>;
const filters = { keyword: '', protocol: '', status: '' };
@@ -21,14 +26,16 @@ test('rapid viewport changes retain at most one monitor map payload', async () =
initialProps: { bounds: '113.0000,22.0000,114.0000,23.0000' }
});
await waitFor(() => expect(monitorMap).toHaveBeenCalledTimes(1));
await waitFor(() => expect(monitorWorkspace).toHaveBeenCalledTimes(1));
expect(monitorSummary).not.toHaveBeenCalled();
expect(vehicleRealtime).not.toHaveBeenCalled();
for (let index = 1; index <= 8; index += 1) {
view.rerender({ bounds: `${113 + index / 1000},22,${114 + index / 1000},23` });
await waitFor(() => expect(monitorMap.mock.calls.length).toBeGreaterThanOrEqual(index + 1));
await waitFor(() => expect(monitorWorkspace.mock.calls.length).toBeGreaterThanOrEqual(index + 1));
}
await waitFor(() => {
const cachedMaps = client.getQueryCache().findAll({ queryKey: ['monitor', 'map'] });
expect(cachedMaps).toHaveLength(1);
const cachedWorkspaces = client.getQueryCache().findAll({ queryKey: ['monitor', 'workspace'] });
expect(cachedWorkspaces).toHaveLength(1);
});
view.unmount();
client.clear();