mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
fix(map): keep routed roads on the device so a whole-trip route opens drawn
With "Show whole trip" on, every leg of every day went to the router one at a time, a second apart, and the route cache lived in memory only, so each visit started over: a trip of a month took 30 to 45 seconds before its route was drawn, on every open. Routes the router answered are now also kept on the device (Dexie v14, routeCache by RouteCalculator's cache key, a month at most, the oldest give way past 3000). A leg found there needs no request and no pause, so a trip whose legs did not change opens with its roads at once; only new or changed legs are routed. Plugin routes stay in memory only, since they depend on trip and day. The cache moves out of RouteCalculator into routeStore.ts. Closes #2794
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
"src/components/Map/MapView.tsx": 1385,
|
||||
"src/components/Map/MapViewGL.test.tsx": 2919,
|
||||
"src/components/Map/MapViewGL.tsx": 2290,
|
||||
"src/components/Map/RouteCalculator.ts": 1076,
|
||||
"src/components/Map/RouteCalculator.ts": 1063,
|
||||
"src/components/PDF/TripPDF.tsx": 1082,
|
||||
"src/components/Planner/DayDetailPanel.test.tsx": 2333,
|
||||
"src/components/Planner/DayPlanSidebar.test.tsx": 5349,
|
||||
|
||||
@@ -426,6 +426,23 @@ describe('calculateRouteWithLegs', () => {
|
||||
expect(second).toBe(first)
|
||||
})
|
||||
|
||||
it('FE-COMP-ROUTECALCULATOR-035b: a route answered once is still there after a reload, without asking the router (#2794)', async () => {
|
||||
let hits = 0
|
||||
server.use(http.get(`${FOSSGIS.driving}/:coords`, () => { hits++; return HttpResponse.json(buildLegsResponse()) }))
|
||||
const wps = freshWaypoints()
|
||||
const first = await calculateRouteWithLegs(wps)
|
||||
const { offlineDb } = await import('../../db/offlineDb')
|
||||
await vi.waitFor(async () => expect(await offlineDb.routeCache.count()).toBeGreaterThan(0))
|
||||
|
||||
// A reload: the module, and with it the cache in memory, starts over.
|
||||
vi.resetModules()
|
||||
const reloaded = await import('./RouteCalculator')
|
||||
const again = await reloaded.calculateRouteWithLegs(wps)
|
||||
|
||||
expect(hits).toBe(1)
|
||||
expect(again).toEqual(first)
|
||||
})
|
||||
|
||||
it('FE-COMP-ROUTECALCULATOR-036: switching the distance unit re-fetches instead of reusing stale text (#1300)', async () => {
|
||||
server.use(http.get(`${FOSSGIS.driving}/:coords`, () => HttpResponse.json(buildLegsResponse())))
|
||||
const wps = freshWaypoints()
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { DistanceUnit, RouteResult, RouteSegment, RouteWithLegs, SnappedWay
|
||||
import { haversineKm } from '../../utils/geo'
|
||||
import { formatDistance } from '../../utils/units'
|
||||
import { countRoute } from './routeUsageCounter'
|
||||
import { cacheRoute, rememberRoute, routeInMemory, storedRoute } from './routeStore'
|
||||
import { valhallaRouteAvoiding, valhallaRun, valhallaAvailable, valhallaAlternates, valhallaSpacing, legAvoids, type AvoidClass, type ValhallaLeg } from './valhallaRoute'
|
||||
import { furthestFrom, sameRoad, type RouteUsageSurface } from '@trek/shared'
|
||||
|
||||
@@ -196,10 +197,6 @@ function retryAfterMs(response: Response): number | null {
|
||||
return Number.isNaN(at) ? null : Math.max(0, at - Date.now())
|
||||
}
|
||||
|
||||
// Cache route responses keyed by the exact waypoint list. Routes are stable, so
|
||||
// this avoids re-hitting the public OSRM demo server on every day switch / reorder.
|
||||
const routeCache = new Map<string, RouteWithLegs>()
|
||||
const ROUTE_CACHE_MAX = 200
|
||||
|
||||
/**
|
||||
* A route profile is either one of the built-in OSRM profiles or a plugin profile
|
||||
@@ -540,7 +537,10 @@ export async function calculateRouteWithLegs(
|
||||
const avoidScope = avoiding.length ? `:avoid=${avoiding.join(',')}` : ''
|
||||
const plainKey = `${profile}:${getDistanceUnit()}:${coords}${pluginScope}`
|
||||
const cacheKey = `${plainKey}${avoidScope}`
|
||||
const cached = routeCache.get(cacheKey)
|
||||
// A plugin route stays in memory: its answer depends on the trip and the day,
|
||||
// and may carry live data such as charging stops. The rest is kept on the device
|
||||
// too, so a trip that shows its whole route opens drawn (#2794).
|
||||
const cached = pluginScope ? routeInMemory(cacheKey) : await storedRoute(cacheKey)
|
||||
if (cached) return cached
|
||||
|
||||
if (avoiding.length) {
|
||||
@@ -552,7 +552,7 @@ export async function calculateRouteWithLegs(
|
||||
// Null is a Valhalla that could not answer at all. OSRM keeps the day drawn rather
|
||||
// than blank, and the answer says that nothing was avoided, with the reason, so the
|
||||
// rail flags the day instead of implying the setting held on a road nobody weighed.
|
||||
const plain = routeCache.get(plainKey) ?? await osrmWithLegs(waypoints, coords, profile, signal)
|
||||
const plain = (await storedRoute(plainKey)) ?? await osrmWithLegs(waypoints, coords, profile, signal)
|
||||
cacheRoute(plainKey, plain)
|
||||
return { ...plain, avoidance: { asked: avoiding, achieved: [], fellBack: true } }
|
||||
}
|
||||
@@ -591,11 +591,7 @@ export async function calculateRouteWithLegs(
|
||||
legs,
|
||||
...(route.viaPoints.length ? { vias: route.viaPoints } : {}),
|
||||
}
|
||||
routeCache.set(cacheKey, result)
|
||||
if (routeCache.size > ROUTE_CACHE_MAX) {
|
||||
const oldest = routeCache.keys().next().value
|
||||
if (oldest !== undefined) routeCache.delete(oldest)
|
||||
}
|
||||
rememberRoute(cacheKey, result)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -638,15 +634,6 @@ async function osrmWithLegs(
|
||||
return { coordinates, distance: route.distance, duration: route.duration, legs, ...(snapped ? { snapped } : {}) }
|
||||
}
|
||||
|
||||
/** Store a route and drop the oldest once the cache is over its cap. */
|
||||
function cacheRoute(key: string, route: RouteWithLegs): void {
|
||||
routeCache.set(key, route)
|
||||
if (routeCache.size > ROUTE_CACHE_MAX) {
|
||||
const oldest = routeCache.keys().next().value
|
||||
if (oldest !== undefined) routeCache.delete(oldest)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A whole chain from the second engine, weighted away from some road classes.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { routeCacheRepo } from '../../repo/routeCacheRepo';
|
||||
import type { RouteWithLegs } from '../../types';
|
||||
|
||||
/**
|
||||
* Route answers by RouteCalculator's cache key (profile, unit, waypoints,
|
||||
* avoided roads). Routes are stable, so this keeps the router from being asked
|
||||
* again on every day switch or reorder: in memory for this visit, and on the
|
||||
* device for the next one, so a trip that shows its whole route opens drawn
|
||||
* instead of routing every leg again, one a second (#2794).
|
||||
*/
|
||||
const inMemory = new Map<string, RouteWithLegs>();
|
||||
const MEMORY_MAX = 200;
|
||||
|
||||
/** Keeps a route for this visit only, dropping the oldest past the limit. */
|
||||
export function rememberRoute(key: string, route: RouteWithLegs): void {
|
||||
inMemory.set(key, route);
|
||||
if (inMemory.size > MEMORY_MAX) {
|
||||
const oldest = inMemory.keys().next().value;
|
||||
if (oldest !== undefined) inMemory.delete(oldest);
|
||||
}
|
||||
}
|
||||
|
||||
/** The route this visit already holds, without asking the device. */
|
||||
export function routeInMemory(key: string): RouteWithLegs | undefined {
|
||||
return inMemory.get(key);
|
||||
}
|
||||
|
||||
/** Keeps a route the router just answered, in memory and on the device. */
|
||||
export function cacheRoute(key: string, route: RouteWithLegs): void {
|
||||
rememberRoute(key, route);
|
||||
void routeCacheRepo.put(key, route);
|
||||
}
|
||||
|
||||
/** The route kept in memory, else the one kept on the device, which then stays in memory too. */
|
||||
export async function storedRoute(key: string): Promise<RouteWithLegs | null> {
|
||||
const held = inMemory.get(key);
|
||||
if (held) return held;
|
||||
const kept = await routeCacheRepo.get<RouteWithLegs>(key);
|
||||
if (kept) rememberRoute(key, kept);
|
||||
return kept;
|
||||
}
|
||||
@@ -70,6 +70,19 @@ interface CachedTourDraft {
|
||||
draft: TourDraft;
|
||||
}
|
||||
|
||||
/**
|
||||
* A road route as the router answered it, kept on the device so a trip opens
|
||||
* with its roads drawn instead of asking the router for every leg again (#2794).
|
||||
* `key` is RouteCalculator's cache key (profile, unit, waypoints, avoided
|
||||
* roads), `route` its answer as it came, `savedAt` when it came: an old answer
|
||||
* is asked again, and the oldest give way first.
|
||||
*/
|
||||
interface CachedRoute {
|
||||
key: string;
|
||||
route: unknown;
|
||||
savedAt: number;
|
||||
}
|
||||
|
||||
// ── Queue + sync types ────────────────────────────────────────────────────────
|
||||
|
||||
// 'conflict' is terminal-until-resolved: the server rejected the replay because
|
||||
@@ -279,6 +292,7 @@ export class TrekOfflineDb extends Dexie {
|
||||
ferrataTiles!: Table<CachedFerrataTile, [number, string]>;
|
||||
tourStationVisits!: Table<CachedTourStationVisit, [number, string]>;
|
||||
tourDrafts!: Table<CachedTourDraft, number>;
|
||||
routeCache!: Table<CachedRoute, string>;
|
||||
|
||||
constructor(name: string = ANON_DB_NAME) {
|
||||
super(name);
|
||||
@@ -366,6 +380,11 @@ export class TrekOfflineDb extends Dexie {
|
||||
// v13: the Tours planner's unsaved draft as the account last said it, one
|
||||
// per trip (the database is the signed-in user's own), so it opens offline.
|
||||
this.version(13).stores({ tourDrafts: 'tripId' });
|
||||
|
||||
// v14: road routes the router answered, by their cache key, so a trip with its
|
||||
// whole route shown opens drawn instead of re-routing every leg (#2794). Not per
|
||||
// trip: the same waypoints are the same road in any trip; `savedAt` to prune.
|
||||
this.version(14).stores({ routeCache: 'key, savedAt' });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { offlineDb } from '../db/offlineDb';
|
||||
import { routeCacheRepo } from './routeCacheRepo';
|
||||
|
||||
afterEach(async () => {
|
||||
vi.restoreAllMocks();
|
||||
vi.useRealTimers();
|
||||
await offlineDb.routeCache.clear();
|
||||
});
|
||||
|
||||
const ROUTE = { coordinates: [[48.1, 11.5]], distance: 4200, duration: 600, legs: [] };
|
||||
|
||||
describe('road routes kept on the device', () => {
|
||||
it('FE-ROUTE-CACHE-001: a kept route comes back as it went in, a missing one as null', async () => {
|
||||
await routeCacheRepo.put('driving:km:11.5,48.1;11.6,48.2', ROUTE);
|
||||
await expect(routeCacheRepo.get('driving:km:11.5,48.1;11.6,48.2')).resolves.toEqual(ROUTE);
|
||||
await expect(routeCacheRepo.get('walking:km:11.5,48.1;11.6,48.2')).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ROUTE-CACHE-002: a route older than a month is asked again', async () => {
|
||||
vi.useFakeTimers({ toFake: ['Date'] });
|
||||
vi.setSystemTime(new Date('2026-09-01T00:00:00Z'));
|
||||
await routeCacheRepo.put('driving:km:old', ROUTE);
|
||||
vi.setSystemTime(new Date('2026-10-25T00:00:00Z'));
|
||||
await expect(routeCacheRepo.get('driving:km:old')).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ROUTE-CACHE-003: a database that fails is a miss on read and nothing on write', async () => {
|
||||
vi.spyOn(offlineDb.routeCache, 'get').mockRejectedValueOnce(new Error('closed'));
|
||||
await expect(routeCacheRepo.get('driving:km:any')).resolves.toBeNull();
|
||||
vi.spyOn(offlineDb.routeCache, 'put').mockRejectedValueOnce(new Error('quota'));
|
||||
await expect(routeCacheRepo.put('driving:km:any', ROUTE)).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import { offlineDb } from '../db/offlineDb';
|
||||
|
||||
/** Roads change; an answer older than this is asked again. */
|
||||
const MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
|
||||
/** How many routes the device keeps before the oldest give way. */
|
||||
const MAX_ROWS = 3000;
|
||||
/** Pruning counts the table, so it runs every so many writes rather than on each one. */
|
||||
const PRUNE_EVERY = 100;
|
||||
|
||||
let writes = 0;
|
||||
|
||||
async function prune(): Promise<void> {
|
||||
const extra = (await offlineDb.routeCache.count()) - MAX_ROWS;
|
||||
if (extra <= 0) return;
|
||||
const oldest = await offlineDb.routeCache.orderBy('savedAt').limit(extra).primaryKeys();
|
||||
await offlineDb.routeCache.bulkDelete(oldest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Road routes kept on the device, by RouteCalculator's cache key, so a trip that
|
||||
* shows its whole route opens with its roads instead of asking the router for
|
||||
* every leg again, one a second, on every visit (#2794). A convenience only:
|
||||
* anything this store cannot read or write is simply routed again.
|
||||
*/
|
||||
export const routeCacheRepo = {
|
||||
/** The route kept under `key`, unless it is missing or too old. */
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
try {
|
||||
const row = await offlineDb.routeCache.get(key);
|
||||
if (!row || Date.now() - row.savedAt > MAX_AGE_MS) return null;
|
||||
return row.route as T;
|
||||
} catch {
|
||||
// A closed or blocked database: the leg is routed again.
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
/** Keeps a route the router just answered. */
|
||||
async put(key: string, route: unknown): Promise<void> {
|
||||
try {
|
||||
await offlineDb.routeCache.put({ key, route, savedAt: Date.now() });
|
||||
writes += 1;
|
||||
if (writes % PRUNE_EVERY === 0) await prune();
|
||||
} catch {
|
||||
// A full or blocked database: the route still serves this visit from memory.
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -56,6 +56,7 @@ const HISTORY: Record<number, StoreSpec> = {
|
||||
11: { ferrataTiles: '[tripId+tile], tripId, fetchedAt' },
|
||||
12: { tourStationVisits: '[assignment_id+station_key], trip_id, assignment_id' },
|
||||
13: { tourDrafts: 'tripId' },
|
||||
14: { routeCache: 'key, savedAt' },
|
||||
};
|
||||
|
||||
const HISTORY_VERSIONS = Object.keys(HISTORY)
|
||||
@@ -136,6 +137,7 @@ function rowsAt(version: number): Record<string, Record<string, unknown>> {
|
||||
ferrataTiles: { tripId: 1, tile: '930:236', fetchedAt: 11, ferratas: [] },
|
||||
tourStationVisits: { assignment_id: 71, station_key: 'stop-01', status: 'visited', trip_id: 1 },
|
||||
tourDrafts: { tripId: 1, draft: { payload: '{"version":11}', updated_at: '2026-10-22 09:00:00' } },
|
||||
routeCache: { key: 'driving:km:11.5,48.1;11.6,48.2', route: { coordinates: [], legs: [] }, savedAt: 13 },
|
||||
};
|
||||
const tables = schemaAt(version);
|
||||
return Object.fromEntries(Object.entries(all).filter(([table]) => table in tables));
|
||||
@@ -344,6 +346,24 @@ const UPGRADE_CASES: Record<number, UpgradeCase> = {
|
||||
expect((await db.tourDrafts.get(3))?.draft.payload).toBe('{"name":"C"}');
|
||||
},
|
||||
},
|
||||
14: {
|
||||
intent: 'adds the road routes kept on the device and keeps the account drafts of the Tours planner',
|
||||
check: async (db) => {
|
||||
expect((await db.tourDrafts.get(1))?.draft.payload).toBe('{"version":11}');
|
||||
await db.table('routeCache').bulkPut([
|
||||
{ key: 'driving:km:1,2;3,4', route: { legs: [] }, savedAt: 20 },
|
||||
{ key: 'walking:km:1,2;3,4', route: { legs: [] }, savedAt: 10 },
|
||||
]);
|
||||
// One row per key: the same route answered again replaces the old one.
|
||||
await db.table('routeCache').put({ key: 'driving:km:1,2;3,4', route: { legs: [1] }, savedAt: 30 });
|
||||
expect(await db.routeCache.count()).toBe(2);
|
||||
// The oldest come first, which is how the store prunes.
|
||||
expect(await db.routeCache.orderBy('savedAt').primaryKeys()).toEqual([
|
||||
'walking:km:1,2;3,4',
|
||||
'driving:km:1,2;3,4',
|
||||
]);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
describe('offlineDb upgrades', () => {
|
||||
|
||||
Reference in New Issue
Block a user