mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
1bc82f97fd
Paddle tours are planned on the water by TREK's own router: a water network per paddle region, built from the same Geofabrik extract as the obstacles and searched in a worker thread of the server (no sidecar). Rivers, canals and lakes (direct or along the shore), portages round weirs, locks by policy, whitewater by boat, gaps marked. Found portages and locks become points of their own; an unplannable leg says why and offers a way out. - Shaping points: a click on the line sets a point that steers the route without being a stop (hikes and paddle tours); recompute a single leg. - Lake style direct/shore, suggested from the wind forecast. - Regions load by themselves where a paddler plans (admin setting: automatic, on request, off; size cap and a daily count), with requests for the admins, a notification, and the load followed on the leg. - Portage from the last point, portages follow footways via the walking router (or stay straight), "put in here" on slipways and put-ins. - MCP plan_paddle_route with the REST route's rules.
75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
import { WaterRouter, type RouteProfile } from './engine/router';
|
|
import { decodeWaterGraph } from './water-graph';
|
|
import type { PaddleRouteLeg, PaddleRoutePoint } from '@trek/shared';
|
|
|
|
import { readFile } from 'node:fs/promises';
|
|
|
|
/** One region's legs to plan, read from the graph file at `file`. */
|
|
export interface RouteJob {
|
|
file: string;
|
|
legs: Array<{ from: PaddleRoutePoint; to: PaddleRoutePoint }>;
|
|
profile: RouteProfile;
|
|
}
|
|
|
|
/** Plans legs on a region's graph: in a worker thread in production, on the calling thread in tests. */
|
|
export interface RouterRunner {
|
|
run(job: RouteJob): Promise<PaddleRouteLeg[]>;
|
|
/** Drops a graph from memory, after a region was built again or removed. */
|
|
forget(file: string): void;
|
|
close(): Promise<void>;
|
|
}
|
|
|
|
export const PADDLE_ROUTER_RUNNER = Symbol('PADDLE_ROUTER_RUNNER');
|
|
|
|
/** How many regions' graphs stay loaded; the least recently used one goes first. */
|
|
const LOADED_REGIONS = 4;
|
|
|
|
/** The loaded graphs of a thread, newest use last. */
|
|
export class GraphCache {
|
|
private readonly routers = new Map<string, Promise<WaterRouter>>();
|
|
|
|
get(file: string): Promise<WaterRouter> {
|
|
let router = this.routers.get(file);
|
|
if (router !== undefined) {
|
|
this.routers.delete(file);
|
|
} else {
|
|
router = readFile(file).then((bytes) => new WaterRouter(decodeWaterGraph(bytes)));
|
|
router.catch(() => this.routers.delete(file));
|
|
}
|
|
this.routers.set(file, router);
|
|
while (this.routers.size > LOADED_REGIONS) this.routers.delete(this.routers.keys().next().value as string);
|
|
return router;
|
|
}
|
|
|
|
forget(file: string): void {
|
|
this.routers.delete(file);
|
|
}
|
|
|
|
clear(): void {
|
|
this.routers.clear();
|
|
}
|
|
}
|
|
|
|
export async function runJob(cache: GraphCache, job: RouteJob): Promise<PaddleRouteLeg[]> {
|
|
const router = await cache.get(job.file);
|
|
return job.legs.map(({ from, to }) => router.route(from, to, job.profile));
|
|
}
|
|
|
|
/** The test runner: the same work on the calling thread. */
|
|
export class InlineRouterRunner implements RouterRunner {
|
|
private readonly cache = new GraphCache();
|
|
|
|
run(job: RouteJob): Promise<PaddleRouteLeg[]> {
|
|
return runJob(this.cache, job);
|
|
}
|
|
|
|
forget(file: string): void {
|
|
this.cache.forget(file);
|
|
}
|
|
|
|
close(): Promise<void> {
|
|
this.cache.clear();
|
|
return Promise.resolve();
|
|
}
|
|
}
|