mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
fix(maps): say which engine drew a day and keep Valhalla days found while the relay rests
The route cache key followed the relay's rest, so after one failed relay request (always the case offline) every day the device held only from Valhalla missed the cache for ten minutes. The key now follows the configured provider, and a resting relay only stops the question from going out. A route now names the engine that drew it. A day OSRM drew because Valhalla could not answer is OSRM's on the rail, and its other ways are asked of OSRM, instead of being labelled Valhalla's and compared with Valhalla offers. An own Valhalla gets the relay's twelve second deadline, and a day that runs it out goes to OSRM without a second wait. A day that avoids something keeps its old cache key, so the days a device already holds stay found. The leg segment and the offer types move to modules both engines import.
This commit is contained in:
@@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest'
|
||||
import { alternativeSubline, alternativesBusy, alternativesPhase, buildAlternativeOverlays, otherEngineNote, type AlternativeOverlay } from './alternativeOverlays'
|
||||
import { formatDurationShort } from './roadtripModel'
|
||||
import { ALT_PRIMARY, ALT_SECONDARY, ALT_LABEL_PRIMARY_BG, ALT_LABEL_SECONDARY_BG } from './alternativeColors'
|
||||
import type { RouteAlternative } from '../Map/RouteCalculator'
|
||||
import type { RouteAlternative } from '../Map/routeTypes'
|
||||
|
||||
/**
|
||||
* FE-ALTOVL-001..023: turning the router's answers into something drawable.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { formatDurationShort } from './roadtripModel'
|
||||
import { ALT_PRIMARY, ALT_SECONDARY, ALT_LABEL_PRIMARY_BG, ALT_LABEL_SECONDARY_BG } from './alternativeColors'
|
||||
import type { RouteAlternative, RouteEngine } from '../Map/RouteCalculator'
|
||||
import type { RouteAlternative } from '../Map/routeTypes'
|
||||
import type { RouteEngine } from '../../types'
|
||||
import type { LegAlternatives } from './useRouteAlternatives'
|
||||
|
||||
/** One offered route, ready to draw: the line, its colour, and where its label sits. */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { MAX_WAY_PINS, pinWay, type PinnedWay, type WayPin } from '@trek/shared'
|
||||
import type { RoadLine } from '../Map/RouteCalculator'
|
||||
import type { RoadLine } from '../Map/routeTypes'
|
||||
import { isStoredStop, type RouteSegment } from '@trek/shared/roadtrip'
|
||||
import type { RailLegRoute, RailLegRouter, RoadtripDay, RoadtripStop } from './useRoadtripRoutes'
|
||||
import type { OfferedRoute, RailDrive, ViaAnchor } from './useRouteAlternatives'
|
||||
|
||||
@@ -1176,6 +1176,19 @@ describe('a leg asked for again on demand', () => {
|
||||
.toMatchObject({ engine: 'plugin' })
|
||||
})
|
||||
|
||||
it('FE-ROADTRIP-ROUTES-047c: a day OSRM drew where Valhalla routes the days is OSRM\'s leg, not a stand-in', async () => {
|
||||
act(() => { useSettingsStore.setState(s => ({ settings: { ...s.settings, routing_valhalla_provider: 'trek' } })) })
|
||||
calculateRouteWithLegs.mockResolvedValue({ ...routed(1), engine: 'osrm' })
|
||||
const { result } = renderHook(() => useRoadtripRoutes(7, [day(1, 1)], map(1, [{ id: 1, at: HAMBURG }, { id: 2, at: BERLIN }])))
|
||||
await waitFor(() => expect(result.current.loading).toBe(false))
|
||||
const [from, to] = result.current.days[0].stops
|
||||
expect(result.current.legRouter!(from, to, 1)).toMatchObject({ engine: 'osrm', standIn: false })
|
||||
|
||||
calculateRouteWithLegs.mockResolvedValue({ ...routed(1), engine: 'valhalla' })
|
||||
act(() => { result.current.reroute!() })
|
||||
await waitFor(() => expect(result.current.legRouter!(from, to, 1).engine).toBe('valhalla'))
|
||||
})
|
||||
|
||||
it('FE-ROADTRIP-ROUTES-048: a day the stand-in engine drove while avoiding says the avoidance did not happen', async () => {
|
||||
act(() => { useSettingsStore.setState(s => ({ settings: { ...s.settings, roadtrip_avoid: 'toll,ferry' } })) })
|
||||
calculateRouteWithLegs.mockResolvedValue({ ...routed(1), avoidance: { asked: ['ferry', 'toll'], achieved: [], fellBack: true } })
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { useRoadtripSettings } from '../../hooks/useRoadtripSettings'
|
||||
import { assembleRoadtrip, bookendStaysOf, carrierLegsFor, carrierSeam, foldRouteRun, hotelBookendsOn, isCarrierMode, isStationaryJoin, mergeRouteSegments, oneNightPerStay, seatCarrierStops, seatNightBookends, standsAsDay, terminalAssignmentId, viasOnLeg, type CarrierSeam, type RoadtripStop, type RoadtripRoutes, type PlanDay, type QuietDay, type RoutedLeg } from '@trek/shared/roadtrip'
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { avoidedClasses, calculateRouteWithLegs, routeEngineFor, RoutingRefusedError, type RouteEngine } from '../Map/RouteCalculator'
|
||||
import { avoidedClasses, calculateRouteWithLegs, routeEngineFor, RoutingRefusedError } from '../Map/RouteCalculator'
|
||||
import { resolveLegMode } from '../Planner/legMode'
|
||||
import { splitIntoRuns, parseAvoid, type DriveLimits } from './roadtripModel'
|
||||
import { spillChains } from './nightSpill'
|
||||
import { useSettingsStore } from '../../store/settingsStore'
|
||||
import { useTripStore } from '../../store/tripStore'
|
||||
import { useVehicleRange } from './useVehicleRange'
|
||||
import type { Assignment, AssignmentsMap, Accommodation, Day, Reservation, RouteAvoidClass, SnappedWaypoint } from '../../types'
|
||||
import type { Assignment, AssignmentsMap, Accommodation, Day, Reservation, RouteAvoidClass, RouteEngine, RouteWithLegs, SnappedWaypoint } from '../../types'
|
||||
import type { RoadtripVia, RoadtripDayBoundary } from '@trek/shared'
|
||||
import { dayWindow } from './dayWindow'
|
||||
import { useTranslation } from '../../i18n/TranslationContext'
|
||||
@@ -82,10 +82,32 @@ const seamShape = (from: RoadtripStop, to: RoadtripStop, viasByDay: Record<numbe
|
||||
|
||||
/**
|
||||
* A drive between two days as it was answered: the leg, the shape it was asked for
|
||||
* (`seamShape`), whether OSRM answered in place of the engine asked (`standIn`), and the
|
||||
* classes the answer could not avoid.
|
||||
* (`seamShape`), whether OSRM answered in place of the engine asked (`standIn`), the
|
||||
* engine the leg is planned with (`plannedEngine`), and the classes the answer could not
|
||||
* avoid.
|
||||
*/
|
||||
type SeamLeg = RoutedLeg & { shape: string; standIn?: boolean; missed?: RouteAvoidClass[] }
|
||||
type SeamLeg = RoutedLeg & { shape: string; standIn?: boolean; engine?: RouteEngine; missed?: RouteAvoidClass[] }
|
||||
|
||||
/**
|
||||
* The engine a leg is planned with, read off its answer: the one that drew it. A day that
|
||||
* avoids nothing and that Valhalla could not answer went to OSRM without a word, so it is
|
||||
* OSRM's, and the ways offered beside it are OSRM's too. A day that avoids something stays
|
||||
* Valhalla's while OSRM stands in for it (`standIn`), since only Valhalla can weigh it.
|
||||
*/
|
||||
const plannedEngine = (r: RouteWithLegs): RouteEngine | undefined => (r.avoidance?.fellBack ? 'valhalla' : r.engine)
|
||||
|
||||
/**
|
||||
* Remembers, per leg of a run, the engine it is planned with and whether OSRM stood in for
|
||||
* it, so a picker opened on one of them knows the line on the map is not its engine's and
|
||||
* does not read it as the road already taken.
|
||||
*/
|
||||
function noteEngines(keys: string[], r: RouteWithLegs, engines: Record<string, RouteEngine>, standIns: Set<string>): void {
|
||||
const engine = plannedEngine(r)
|
||||
for (const key of keys) {
|
||||
if (engine) engines[key] = engine
|
||||
if (r.avoidance?.fellBack) standIns.add(key)
|
||||
}
|
||||
}
|
||||
|
||||
/** The drive from one stop to the next, identified the same way `planKey` identifies them. */
|
||||
const legKey = (from: RoadtripStop, to: RoadtripStop): string => `${stopKey(from)}>${stopKey(to)}`
|
||||
@@ -93,6 +115,7 @@ const legKey = (from: RoadtripStop, to: RoadtripStop): string => `${stopKey(from
|
||||
const EMPTY_ACCOMMODATIONS: Accommodation[] = []
|
||||
const EMPTY_RESERVATIONS: Reservation[] = []
|
||||
const NO_STAND_INS: ReadonlySet<string> = new Set()
|
||||
const NO_ENGINES: Readonly<Record<string, RouteEngine>> = {}
|
||||
|
||||
/** One leg as the rail's own router answered it, for a surface checking a choice against it. */
|
||||
export interface RailLegRoute {
|
||||
@@ -120,7 +143,7 @@ export interface RailLegRouter {
|
||||
mode: string
|
||||
/** The classes the rail weighs away on this leg: none unless the leg is driven by Valhalla. */
|
||||
avoid: RouteAvoidClass[]
|
||||
/** Which engine prices this leg on the rail. */
|
||||
/** Which engine prices this leg on the rail, read off its answer once there is one (`plannedEngine`). */
|
||||
engine: RouteEngine
|
||||
/**
|
||||
* True when the line the rail has for this leg came from OSRM standing in for `engine`,
|
||||
@@ -249,6 +272,8 @@ export function useRoadtripRoutes(
|
||||
const [seamLegs, setSeamLegs] = useState<Record<string, SeamLeg>>({})
|
||||
/** The legs of the day runs that OSRM drew while the engine asked did not answer. */
|
||||
const [standInLegs, setStandInLegs] = useState<ReadonlySet<string>>(NO_STAND_INS)
|
||||
/** The engine each leg of the day runs is planned with, as its answer said (`plannedEngine`). */
|
||||
const [runEngines, setRunEngines] = useState<Readonly<Record<string, RouteEngine>>>(NO_ENGINES)
|
||||
/** Bumped to route everything again (`reroute`): every answer already cached comes back at once. */
|
||||
const [rerouteRound, setRerouteRound] = useState(0)
|
||||
const [loading, setLoading] = useState(false)
|
||||
@@ -417,6 +442,7 @@ export function useRoadtripRoutes(
|
||||
setSnapByDay({})
|
||||
setMissedByDay({})
|
||||
setStandInLegs(NO_STAND_INS)
|
||||
setRunEngines(NO_ENGINES)
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
@@ -428,6 +454,7 @@ export function useRoadtripRoutes(
|
||||
const collectedSnaps: Record<number, Record<string, SnappedWaypoint>> = {}
|
||||
const collectedMisses: Record<number, RouteAvoidClass[]> = {}
|
||||
const collectedStandIns = new Set<string>()
|
||||
const collectedEngines: Record<string, RouteEngine> = {}
|
||||
const tasks: (() => Promise<void>)[] = []
|
||||
|
||||
/**
|
||||
@@ -518,9 +545,7 @@ export function useRoadtripRoutes(
|
||||
}
|
||||
}
|
||||
const folded = foldRouteRun(run, stopAt, r, mode, distanceUnit)
|
||||
// Remembered per leg, so a picker opened on one of them knows the line on the
|
||||
// map is not its engine's and does not read it as the road already taken.
|
||||
if (r.avoidance?.fellBack) for (const key of Object.keys(folded)) collectedStandIns.add(key)
|
||||
noteEngines(Object.keys(folded), r, collectedEngines, collectedStandIns)
|
||||
Object.assign(dayLegs, folded)
|
||||
return
|
||||
} catch (err) {
|
||||
@@ -565,6 +590,7 @@ export function useRoadtripRoutes(
|
||||
setSnapByDay({ ...collectedSnaps })
|
||||
setMissedByDay({ ...collectedMisses })
|
||||
setStandInLegs(new Set(collectedStandIns))
|
||||
setRunEngines({ ...collectedEngines })
|
||||
}
|
||||
void (async () => {
|
||||
for (let i = 0; i < tasks.length; i++) {
|
||||
@@ -770,6 +796,7 @@ export function useRoadtripRoutes(
|
||||
vias: [],
|
||||
shape,
|
||||
standIn: r.avoidance?.fellBack === true,
|
||||
engine: plannedEngine(r),
|
||||
missed: r.avoidance ? r.avoidance.asked.filter(cls => !r.avoidance!.achieved.includes(cls)) : [],
|
||||
},
|
||||
}))
|
||||
@@ -799,7 +826,7 @@ export function useRoadtripRoutes(
|
||||
return {
|
||||
mode,
|
||||
avoid: avoidedClasses(mode, avoid),
|
||||
engine: routeEngineFor(mode, avoid),
|
||||
engine: (byRun ? runEngines[key] : seamLegs[key]?.engine) ?? routeEngineFor(mode, avoid),
|
||||
standIn: byRun ? standInLegs.has(key) : seamLegs[key]?.standIn === true,
|
||||
route: async (pins, signal) => {
|
||||
await spacedRequest(signal)
|
||||
@@ -816,7 +843,7 @@ export function useRoadtripRoutes(
|
||||
}
|
||||
},
|
||||
}
|
||||
}, [legModeOf, avoid, spacedRequest, tripId, plan, legsByDay, standInLegs, seamLegs])
|
||||
}, [legModeOf, avoid, spacedRequest, tripId, plan, legsByDay, standInLegs, runEngines, seamLegs])
|
||||
|
||||
const reroute = useCallback(() => setRerouteRound(round => round + 1), [])
|
||||
|
||||
|
||||
@@ -88,12 +88,23 @@ describe('useRouteAlternatives', () => {
|
||||
act(() => { result.current.ask(request({ avoid: ['toll'], engine: 'valhalla' })) })
|
||||
|
||||
expect(calculateAlternatives).toHaveBeenCalledTimes(1)
|
||||
const [from, to, profile, options] = calculateAlternatives.mock.calls[0] as [unknown, unknown, string, { avoid: string[]; signal: AbortSignal }]
|
||||
const [from, to, profile, options] = calculateAlternatives.mock.calls[0] as [unknown, unknown, string, { avoid: string[]; signal: AbortSignal; engine: string }]
|
||||
expect([from, to, profile]).toEqual([{ lat: 53.55, lng: 9.99 }, { lat: 52.52, lng: 13.4 }, 'driving'])
|
||||
expect(options.avoid).toEqual(['toll'])
|
||||
expect(options.engine).toBe('valhalla')
|
||||
expect(options.signal).toBeInstanceOf(AbortSignal)
|
||||
})
|
||||
|
||||
it('FE-ALTHOOK-003b: a leg OSRM drew where Valhalla routes the days is offered OSRM\'s ways', () => {
|
||||
const { result } = renderHook(() => useRouteAlternatives())
|
||||
|
||||
act(() => { result.current.ask(request({ engine: 'osrm' })) })
|
||||
|
||||
const options = calculateAlternatives.mock.calls[0][3] as { engine: string }
|
||||
expect(options.engine).toBe('osrm')
|
||||
expect(result.current.open?.engine).toBe('osrm')
|
||||
})
|
||||
|
||||
it('FE-ALTHOOK-004: Current is the rail\'s own leg, priced by the rail\'s engine, and never asked for again', async () => {
|
||||
// A trip that avoids a class is driven by the second engine; its current road carries
|
||||
// that engine, so the overlays read every offer against it and not against OSRM.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { useCallback, useMemo, useRef, useState } from 'react'
|
||||
import { sameRoad } from '@trek/shared'
|
||||
import { calculateAlternatives, type RouteAlternative, type RouteEngine } from '../Map/RouteCalculator'
|
||||
import { calculateAlternatives } from '../Map/RouteCalculator'
|
||||
import type { RouteAlternative } from '../Map/routeTypes'
|
||||
import type { RouteEngine } from '../../types'
|
||||
import type { RailLegRoute, RailLegRouter } from './useRoadtripRoutes'
|
||||
|
||||
/** One offered way of driving a leg, with what it means for this trip. */
|
||||
@@ -164,7 +166,9 @@ export function useRouteAlternatives(): RouteAlternativesState {
|
||||
const profile: 'driving' | 'walking' | 'cycling' =
|
||||
router.mode === 'walking' ? 'walking' : router.mode === 'cycling' ? 'cycling' : 'driving'
|
||||
|
||||
void calculateAlternatives(from, to, profile, { signal: controller.signal, avoid: router.avoid })
|
||||
// Asked of the engine the leg is planned with, so a leg OSRM drew in a day Valhalla
|
||||
// could not answer is offered OSRM's ways, timed alike.
|
||||
void calculateAlternatives(from, to, profile, { signal: controller.signal, avoid: router.avoid, engine: router.engine })
|
||||
.then(alternatives => {
|
||||
if (controller.signal.aborted) return
|
||||
const routes: OfferedRoute[] = [current]
|
||||
|
||||
Reference in New Issue
Block a user