feat(server): route the road trip days through Valhalla on the server as the planner does

Where the TREK services or an own Valhalla route the days, the MCP road trip
router asks Valhalla for a day that avoids nothing, through the relay alone,
and OSRM routes the whole day when it cannot answer. Requests carry twenty
locations unless they may end up at FOSSGIS, which keeps ten.
This commit is contained in:
Maurice
2026-10-29 12:11:06 +01:00
committed by Maurice
parent 8fd025e003
commit 29dc967d29
2 changed files with 219 additions and 22 deletions
@@ -22,7 +22,11 @@ import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
import {
resolveValhallaProvider,
VALHALLA_COSTING,
VALHALLA_RELAY_MAX_LOCATIONS,
valhallaRoutesEveryDay,
type GeometryPoint,
type ValhallaCosting,
type ValhallaProvider,
type ValhallaRelayAction,
} from '@trek/shared';
@@ -169,6 +173,8 @@ async function refusalOf(response: Response): Promise<'no_way' | 'too_long' | nu
/** The public Valhalla FOSSGIS runs, which TREK ships with. */
const FOSSGIS_VALHALLA = 'https://valhalla1.openstreetmap.de';
/** Locations one request to FOSSGIS may carry; the eleventh is refused with error 150. */
const FOSSGIS_MAX_LOCATIONS = 10;
/**
* What the relay says when it cannot answer for the TREK services right now:
@@ -212,11 +218,15 @@ function heightsKey(userId: number, valhalla: string, line: ReadonlyArray<readon
/**
* The routers a user's settings name: their own OSRM (blank for the public
* hosts) and the Valhalla to ask, if any, chosen as the browser chooses it
* (`valhallaBase`). `trekValhalla` is the TREK services' Valhalla while it can
* be asked.
* hosts), the Valhalla to ask, if any, chosen as the browser chooses it
* (`valhallaBase`), and whether that Valhalla routes every day rather than
* only what OSRM cannot (`everyDay`, the TREK services or an own one).
* `trekValhalla` is the TREK services' Valhalla while it can be asked.
*/
function routersOf(settings: Record<string, unknown>, trekValhalla: string | null): { base: string; valhalla: string } {
function routersOf(
settings: Record<string, unknown>,
trekValhalla: string | null,
): { base: string; valhalla: string; everyDay: boolean } {
const base =
typeof settings.routing_base_url === 'string' ? settings.routing_base_url.trim().replace(/\/+$/, '') : '';
const configuredValhalla =
@@ -232,7 +242,18 @@ function routersOf(settings: Record<string, unknown>, trekValhalla: string | nul
trek: trekValhalla ?? FOSSGIS_VALHALLA,
fossgis: FOSSGIS_VALHALLA,
};
return { base, valhalla: provider ? valhallaOf[provider] : '' };
return { base, valhalla: provider ? valhallaOf[provider] : '', everyDay: valhallaRoutesEveryDay(provider) };
}
/** How a question to Valhalla is asked besides its body. */
interface ValhallaAsk {
timeoutMs?: number;
/**
* Whether FOSSGIS answers where the relay cannot, as it does for what only
* Valhalla can answer. A day OSRM would otherwise route is not handed to it:
* the question fails instead, and OSRM routes the day.
*/
publicFallback?: boolean;
}
/** Valhalla's maneuver type for boarding a ferry. */
@@ -358,14 +379,15 @@ export class RoadtripRouterService {
* One question to a Valhalla. The TREK services' one is asked through the
* relay, under the user's allowance there and with the installation key; a
* relay that cannot answer hands the question to FOSSGIS through the paced
* queue. Every other Valhalla is asked through the queue directly.
* queue, unless `publicFallback` is off. Every other Valhalla is asked
* through the queue directly.
*/
private async askValhalla(
userId: number,
valhalla: string,
action: ValhallaRelayAction,
body: object,
timeoutMs?: number,
{ timeoutMs, publicFallback = true }: ValhallaAsk = {},
): Promise<unknown> {
if (!this.relay.serves(valhalla)) return this.request(`${valhalla}/${action}`, body, timeoutMs);
const answer = await this.relay.ask(userId, action, body, timeoutMs).catch((err: unknown) => {
@@ -373,9 +395,20 @@ export class RoadtripRouterService {
throw err;
});
if (answer && !relayCannotAnswer(answer.status)) return payloadOf(answer);
if (!publicFallback) throw new Error('The TREK routing service cannot answer');
return this.request(`${FOSSGIS_VALHALLA}/${action}`, body, timeoutMs);
}
/**
* Locations one route request carries: ten wherever it may end up at
* FOSSGIS, twenty at the TREK Valhalla asked alone and at an own one, as the
* planner splits a day (`locationsPerRequest`).
*/
private locationsPerRequest(valhalla: string, publicFallback: boolean): number {
const mayReachPublic = valhalla === FOSSGIS_VALHALLA || (this.relay.serves(valhalla) && publicFallback);
return mayReachPublic ? FOSSGIS_MAX_LOCATIONS : VALHALLA_RELAY_MAX_LOCATIONS;
}
async route(
userId: number,
tripId: number,
@@ -430,7 +463,7 @@ export class RoadtripRouterService {
parts = route.legs;
} else {
const mode = profile === 'walking' || profile === 'cycling' ? profile : 'driving';
const { base, valhalla } = routersOf(settings, this.relay.trekValhalla());
const { base, valhalla, everyDay } = routersOf(settings, this.relay.trekValhalla());
let weighted = false;
if (hiking) {
const cycling = mode === 'cycling';
@@ -444,15 +477,16 @@ export class RoadtripRouterService {
weighted = true;
} else if (footOnly && valhalla) {
try {
const legs = await this.valhallaLegs(userId, valhalla, points, 'pedestrian', { use_ferry: 0 }, true);
const ferryless = { use_ferry: 0 };
const offBoats = { offBoatLines: true };
const legs = await this.valhallaLegs(userId, valhalla, points, 'pedestrian', ferryless, offBoats);
({ line, distance, duration, parts, snapped } = routeOfLegs(points, legs));
ferryLegs = legs.flatMap((leg, index) => (crossesByBoat(leg) ? [index] : []));
weighted = true;
} catch {
snapped = undefined;
}
}
if (mode === 'driving' && avoid.length && valhalla) {
} else if (mode === 'driving' && avoid.length && valhalla) {
try {
const options = {
use_tolls: avoid.includes('toll') ? 0 : 1,
@@ -475,6 +509,17 @@ export class RoadtripRouterService {
avoidMissed = [...avoid];
snapped = undefined;
}
} else if (everyDay) {
// Where Valhalla routes the days, it routes this one whole, as the planner
// does (`valhallaDayRun`); where it cannot answer, OSRM routes all of it.
try {
const relayOnly = { publicFallback: false };
const legs = await this.valhallaLegs(userId, valhalla, points, VALHALLA_COSTING[mode], {}, relayOnly);
({ line, distance, duration, parts, snapped } = routeOfLegs(points, legs));
weighted = true;
} catch {
snapped = undefined;
}
}
if (!weighted) {
const path = mode === 'walking' ? 'foot' : mode === 'cycling' ? 'bike' : 'driving';
@@ -556,22 +601,24 @@ export class RoadtripRouterService {
}
/**
* Valhalla's legs through the points, ten locations a request, one after
* another through the paced queue. CW-12: a walk on foot binds no point to
* a ferry's edge (`offBoatLines`).
* Valhalla's legs through the points, as many locations a request as the
* host takes (`locationsPerRequest`), one after another through the paced
* queue, each piece starting on the point the one before ended at. CW-12: a
* walk on foot binds no point to a ferry's edge (`offBoatLines`).
*/
private async valhallaLegs(
userId: number,
valhalla: string,
points: Point[],
costing: 'auto' | 'pedestrian' | 'bicycle',
costing: ValhallaCosting,
options: Record<string, number | string>,
offBoatLines = false,
{ offBoatLines = false, publicFallback = true }: { offBoatLines?: boolean; publicFallback?: boolean } = {},
): Promise<ValhallaLegs> {
const legs: ValhallaLegs = [];
for (let start = 0; start < points.length - 1; start += 9) {
const payload = await this.askValhalla(userId, valhalla, 'route', {
locations: points.slice(start, start + 10).map((p) => ({
const size = this.locationsPerRequest(valhalla, publicFallback);
for (let start = 0; start < points.length - 1; start += size - 1) {
const body = {
locations: points.slice(start, start + size).map((p) => ({
lat: p.lat,
lon: p.lng,
radius: 50,
@@ -580,7 +627,8 @@ export class RoadtripRouterService {
costing,
costing_options: { [costing]: options },
directions_options: { units: 'kilometers' },
});
};
const payload = await this.askValhalla(userId, valhalla, 'route', body, { publicFallback });
legs.push(...valhallaSchema.parse(payload).trip.legs);
}
if (legs.length !== points.length - 1) throw new Error('Routing response has mismatched legs');
@@ -681,7 +729,9 @@ export class RoadtripRouterService {
async wayIds(userId: number, line: ReadonlyArray<readonly [number, number]>): Promise<TracedWay[]> {
const valhalla = await this.valhallaFor(userId);
if (!valhalla) throw new RouteRefusal('no_valhalla', 'No Valhalla is set to describe a line with');
const answer = await this.askValhalla(userId, valhalla, 'trace_attributes', traceBody(line), TRACE_TIMEOUT_MS);
const answer = await this.askValhalla(userId, valhalla, 'trace_attributes', traceBody(line), {
timeoutMs: TRACE_TIMEOUT_MS,
});
return tracedWaysOf(answer);
}
+148 -1
View File
@@ -3,7 +3,7 @@ import { RoadtripRouterService, RouteRefusal } from '../../../src/nest/roadtrip/
import type { ValhallaRelayAnswer } from '../../../src/nest/routing-relay/routing-relay.service';
import { safeFetchAdminConfigured } from '../../../src/utils/ssrfGuard';
import { oneByOne } from '../../helpers/one-by-one';
import { valhallaPolyline } from '../../helpers/valhalla';
import { valhallaPolyline, valhallaRoute } from '../../helpers/valhalla';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -814,3 +814,150 @@ describe('server Roadtrip router through the TREK services (routing relay)', ()
expect(safeFetchAdminConfigured).not.toHaveBeenCalled();
});
});
describe('server Roadtrip router: every day through Valhalla where it routes the days', () => {
type RouteBody = { locations: Array<{ lat: number; lon: number }>; costing: string; costing_options: unknown };
/** Valhalla's answer to a route body: one leg per pair of its locations, a kilometre and a minute each. */
const legsFor = (body: RouteBody) =>
valhallaRoute(
body.locations.slice(1).map((to, index) => ({
line: [
[body.locations[index].lat, body.locations[index].lon],
[to.lat, to.lon],
],
km: 1,
seconds: 60,
})),
);
const answer = (status: number, body: unknown): ValhallaRelayAnswer => ({
status,
contentType: 'application/json',
body: Buffer.from(JSON.stringify(body)),
});
const day = (count: number) => Array.from({ length: count }, (_, index) => ({ lat: 47 + index / 100, lng: 11 }));
const settled = async <T>(pending: Promise<T>): Promise<T> => {
await vi.runAllTimersAsync();
return await pending;
};
const relayBodies = () => relay.ask.mock.calls.map((call) => call[2] as RouteBody);
const bodyOf = (init: RequestInit | undefined) => JSON.parse(String(init?.body)) as RouteBody;
describe('with the TREK services', () => {
beforeEach(() => {
relay.trekValhalla.mockReturnValue(TREK_VALHALLA);
relay.ask.mockImplementation(async (_userId, _action, body) => answer(200, legsFor(body as RouteBody)));
});
it.each([
['driving', 'auto'],
['walking', 'pedestrian'],
['cycling', 'bicycle'],
])(
'ROUTER-DAY-001: routes a %s day that avoids nothing through the relay as %s, OSRM never asked',
async (mode, costing) => {
const route = await settled(setup().router.route(4, 1, 1, points, mode, []));
expect(relayBodies().map((body) => [body.costing, body.costing_options, body.locations.length])).toEqual([
[costing, { [costing]: {} }, 3],
]);
expect(route.parts).toEqual([
{ distance: 1000, duration: 60 },
{ distance: 1000, duration: 60 },
]);
expect(route.avoidMissed).toEqual([]);
expect(safeFetchAdminConfigured).not.toHaveBeenCalled();
},
);
it('ROUTER-DAY-002: asks twenty locations at a time, each piece starting where the one before ended', async () => {
const route = await settled(setup().router.route(4, 1, 1, day(25), 'driving', []));
const asked = relayBodies().map((body) => body.locations);
expect(asked.map((locations) => locations.length)).toEqual([20, 6]);
expect(asked[1][0]).toEqual(asked[0][19]);
expect(route.parts).toHaveLength(24);
});
it('ROUTER-DAY-003: routes the whole day with OSRM where the relay cannot answer, never with FOSSGIS', async () => {
const refusals = [
() => Promise.resolve(answer(401, { error: 'Unauthorized' })),
() => Promise.resolve(answer(403, { error: 'Forbidden' })),
() => Promise.resolve(answer(413, { error: 'Payload too large' })),
() => Promise.resolve(answer(429, { error: 'Too many routing requests' })),
() => Promise.resolve(answer(502, { error: 'The TREK routing service did not answer' })),
() => Promise.resolve(answer(503, { error: 'The TREK routing service is resting' })),
() => Promise.reject(new DomainError(503, 'The TREK routing service is not available on this instance')),
];
await oneByOne([...refusals.entries()], async ([userId, refusal]) => {
vi.mocked(safeFetchAdminConfigured).mockClear();
relay.ask.mockReset().mockImplementationOnce(refusal);
const route = await settled(setup().router.route(userId, 1, 1, points, 'driving', []));
expect(route.parts.map((part) => part.duration)).toEqual([100, 200]);
const asked = vi.mocked(safeFetchAdminConfigured).mock.calls.map(([url]) => String(url));
expect(asked).toHaveLength(1);
expect(asked[0]).toContain('routing.openstreetmap.de/routed-car');
});
});
it('ROUTER-DAY-004: routes the whole day with OSRM where Valhalla finds no way for one of its legs', async () => {
relay.ask.mockResolvedValueOnce(answer(400, { error_code: 442, error: 'No path could be found for input' }));
const route = await settled(setup().router.route(4, 1, 1, points, 'walking', []));
expect(route.parts.map((part) => part.duration)).toEqual([100, 200]);
expect(safeFetchAdminConfigured).toHaveBeenCalledTimes(1);
expect(safeFetchAdminConfigured).toHaveBeenCalledWith(
expect.stringContaining('routed-foot/route/v1/foot/'),
expect.anything(),
);
});
it('ROUTER-DAY-005: a day that avoids something keeps FOSSGIS behind the relay and its ten locations', async () => {
relay.ask.mockReset().mockResolvedValue(answer(503, { error: 'The TREK routing service is resting' }));
vi.mocked(safeFetchAdminConfigured).mockImplementation(
async (_url, init) => new Response(JSON.stringify(legsFor(bodyOf(init)))),
);
const route = await settled(setup().router.route(4, 1, 1, day(12), 'driving', ['toll']));
expect(relayBodies().map((body) => body.locations.length)).toEqual([10, 3]);
expect(vi.mocked(safeFetchAdminConfigured).mock.calls.map(([url]) => url)).toEqual(
Array(2).fill('https://valhalla1.openstreetmap.de/route'),
);
expect(route.parts).toHaveLength(11);
expect(route.avoidMissed).toEqual([]);
});
});
it('ROUTER-DAY-006: an own Valhalla routes every day in pieces of twenty, and OSRM the day it cannot answer', async () => {
const own = (url: unknown) => String(url).startsWith('https://valhalla.internal');
vi.mocked(safeFetchAdminConfigured).mockImplementation(async (url, init) =>
own(url) ? new Response(JSON.stringify(legsFor(bodyOf(init)))) : osrm(),
);
const { router } = setup({ valhalla_base_url: 'https://valhalla.internal' });
const route = await settled(router.route(1, 1, 1, day(21), 'cycling', []));
const asked = vi
.mocked(safeFetchAdminConfigured)
.mock.calls.map(([url, init]) => [url, bodyOf(init).locations.length]);
expect(asked).toEqual([
['https://valhalla.internal/route', 20],
['https://valhalla.internal/route', 2],
]);
expect(route.parts).toHaveLength(20);
vi.mocked(safeFetchAdminConfigured)
.mockClear()
.mockImplementation(async (url) => (own(url) ? new Response('', { status: 502 }) : osrm()));
const fallen = await settled(router.route(2, 1, 1, points, 'driving', []));
expect(fallen.parts.map((part) => part.duration)).toEqual([100, 200]);
const urls = vi.mocked(safeFetchAdminConfigured).mock.calls.map(([url]) => url);
expect(urls).toHaveLength(2);
expect(urls[0]).toBe('https://valhalla.internal/route');
expect(urls[1]).toContain('routing.openstreetmap.de/routed-car');
expect(relay.ask).not.toHaveBeenCalled();
});
it('ROUTER-DAY-007: with FOSSGIS chosen, a day that avoids nothing stays with OSRM as before', async () => {
relay.trekValhalla.mockReturnValue(TREK_VALHALLA);
const { router } = setup({ routing_valhalla_provider: 'fossgis' });
const route = await settled(router.route(1, 1, 1, points, 'driving', []));
expect(route.parts.map((part) => part.duration)).toEqual([100, 200]);
expect(safeFetchAdminConfigured).toHaveBeenCalledTimes(1);
expect(safeFetchAdminConfigured).toHaveBeenCalledWith(expect.stringContaining('routed-car'), expect.anything());
expect(relay.ask).not.toHaveBeenCalled();
});
});