perf(tracks): coalesce address lookups

This commit is contained in:
lingniu
2026-07-16 06:22:37 +08:00
parent 41e2b1ab97
commit e6d26faad9
4 changed files with 52 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import type { TrackPlaybackResponse } from '../../api/types';
import { buildTrackRailSelection, formatDuration, sampledEventIndex, trackCsv, trackPlaybackInterval } from './track';
import { buildTrackRailSelection, formatDuration, sampledEventIndex, trackAddressCoordinate, trackCsv, trackPlaybackInterval } from './track';
describe('track domain', () => {
it('formats durations and maps original event indices to sampled points', () => {
@@ -38,4 +38,11 @@ describe('track domain', () => {
expect(csv.startsWith('\uFEFFVIN,')).toBe(true);
expect(csv).toContain('"粤A,001"');
});
it('coalesces GPS jitter into street-level address cells and rejects invalid coordinates', () => {
expect(trackAddressCoordinate(113.123441, 23.123441)).toEqual({ longitude: 113.1234, latitude: 23.1234, key: '113.1234,23.1234' });
expect(trackAddressCoordinate(113.123449, 23.123449)?.key).toBe('113.1234,23.1234');
expect(trackAddressCoordinate(0, 0)).toBeUndefined();
expect(trackAddressCoordinate(181, 23)).toBeUndefined();
});
});

View File

@@ -1,5 +1,22 @@
import type { HistoryLocationRow, TrackPlaybackEvent, TrackPlaybackResponse } from '../../api/types';
export const TRACK_ADDRESS_SETTLE_MS = 650;
export type TrackAddressCoordinate = { longitude: number; latitude: number; key: string };
export function trackAddressCoordinate(longitude?: number, latitude?: number): TrackAddressCoordinate | undefined {
if (!Number.isFinite(longitude) || !Number.isFinite(latitude)
|| Math.abs(longitude ?? 0) > 180 || Math.abs(latitude ?? 0) > 90
|| (longitude === 0 && latitude === 0)) return undefined;
const roundedLongitude = Number(longitude!.toFixed(4));
const roundedLatitude = Number(latitude!.toFixed(4));
return {
longitude: roundedLongitude,
latitude: roundedLatitude,
key: `${roundedLongitude.toFixed(4)},${roundedLatitude.toFixed(4)}`
};
}
export function formatDuration(seconds: number) {
const safe = Math.max(0, Math.floor(seconds || 0));
const hours = Math.floor(safe / 3600);