fix: distinguish track mileage evidence

This commit is contained in:
lingniu
2026-07-19 12:32:01 +08:00
parent f825c9f417
commit 4e4f383988
10 changed files with 63 additions and 32 deletions

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import type { TrackPlaybackResponse } from '../../api/types';
import { buildTrackRailSelection, formatDuration, sampledEventIndex, trackAddressCoordinate, trackCsv, trackPlaybackInterval } from './track';
import { buildTrackRailSelection, formatDuration, sampledEventIndex, trackAddressCoordinate, trackCsv, trackPlaybackInterval, trackPointMileageAvailable } from './track';
describe('track domain', () => {
it('formats durations and maps original event indices to sampled points', () => {
@@ -39,6 +39,11 @@ describe('track domain', () => {
expect(csv).toContain('"粤A,001"');
});
it('keeps missing protocol mileage distinct from a real zero', () => {
expect(trackPointMileageAvailable({ totalMileageKm: 123, mileageAvailable: true } as never)).toBe(true);
expect(trackPointMileageAvailable({ totalMileageKm: 0, mileageAvailable: false } as never)).toBe(false);
});
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');

View File

@@ -38,6 +38,10 @@ export function trackPlaybackInterval(speed: number) {
return Math.max(55, 300 / Math.max(0.5, speed));
}
export function trackPointMileageAvailable(point?: HistoryLocationRow) {
return Boolean(point && (point.mileageAvailable ?? point.totalMileageKm > 0));
}
export function buildTrackRailSelection(track?: TrackPlaybackResponse) {
const pointCount = track?.points.length ?? 0;
const stopIndexesByPoint: Array<number[] | undefined> = new Array(pointCount);
@@ -65,7 +69,7 @@ function csvCell(value: unknown) {
export function trackCsv(track: TrackPlaybackResponse) {
const headers = ['VIN', '车牌', '协议', '设备时间', '服务时间', '经度', '纬度', '速度(km/h)', '总里程(km)'];
const rows = track.points.map((point) => [point.vin, point.plate, point.protocol, point.deviceTime, point.serverTime, point.longitude, point.latitude, point.speedKmh, point.totalMileageKm]);
const rows = track.points.map((point) => [point.vin, point.plate, point.protocol, point.deviceTime, point.serverTime, point.longitude, point.latitude, point.speedKmh, trackPointMileageAvailable(point) ? point.totalMileageKm : '']);
return `\uFEFF${[headers, ...rows].map((row) => row.map(csvCell).join(',')).join('\n')}`;
}