Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(tours): paddle notes under the route, and a lost water network heals itself

Maurice66525c66b752
diff --git a/client/src/components/Tours/paddle/PaddleLayerLeaflet.tsx b/client/src/components/Tours/paddle/PaddleLayerLeaflet.tsx
index 9179c89c2..92a64f0ce 100644
--- a/client/src/components/Tours/paddle/PaddleLayerLeaflet.tsx
+++ b/client/src/components/Tours/paddle/PaddleLayerLeaflet.tsx
@@ -1,5 +1,5 @@
import L from 'leaflet';
-import { useMemo, useState } from 'react';
+import { useEffect, useMemo, useState } from 'react';
import { Marker, Polyline, Tooltip, useMap, useMapEvents } from 'react-leaflet';
import { useTranslation } from '../../../i18n';
import {
@@ -12,6 +12,33 @@ import {
} from './paddleMapLayer';
const LINE_WEIGHT = { on_route: 4, near: 3, area: 2 } as const;
+
+/**
+ * The pane the paddle lines are drawn in: under the overlay pane the tour's
+ * route is in, so a rule that covers a whole river never hides the line
+ * planned along it. The markers stay in the marker pane above, where they are
+ * picked.
+ */
+const LINE_PANE = 'trek-paddle-lines';
+
+/** The line pane, made once per map; undefined where the renderer has no panes, which then stacks by order. */
+function useLinePane(): string | undefined {
+ const map = useMap();
+ const [ready, setReady] = useState(false);
+ useEffect(() => {
+ if (typeof map.getPane !== 'function' || typeof map.createPane !== 'function') return;
+ if (!map.getPane(LINE_PANE)) {
+ const pane = map.createPane(LINE_PANE);
+ pane.style.zIndex = '397';
+ // The app's stylesheet levels every pane (`.leaflet-pane { z-index: 0 !important }`), so
+ // the order in the map pane decides: right before the overlay pane, under the route.
+ const overlay = map.getPane('overlayPane');
+ if (overlay?.parentElement) overlay.parentElement.insertBefore(pane, overlay);
+ }
+ setReady(true);
+ }, [map]);
+ return ready ? LINE_PANE : undefined;
+}
const LINE_OPACITY = { on_route: 1, near: 0.85, area: 0.5 } as const;
function iconOf(item: PaddleMapItem, selected: boolean): L.DivIcon {
@@ -36,6 +63,7 @@ function useLabel() {
/**
* The paddle obstacles on the Leaflet map (PAD-05B): each line on a white
* casing in its category's colour, dashed while it is only near the route,
+ * and under the tour's route so it never hides it,
* and a round marker with its glyph; the quieter, the less it bears on the
* route, and the objects only in the area wait for a closer zoom. A click
* picks the object and goes no further, so it never also sets a waypoint
@@ -44,6 +72,7 @@ function useLabel() {
export function PaddleLayerLeaflet({ layer }: { layer?: PaddleMapLayer }) {
const label = useLabel();
const map = useMap();
+ const linePane = useLinePane();
const [zoom, setZoom] = useState(() => map.getZoom());
useMapEvents({ zoomend: () => setZoom(map.getZoom()) });
const selectedRef = layer?.selectedRef ?? null;
@@ -75,6 +104,7 @@ export function PaddleLayerLeaflet({ layer }: { layer?: PaddleMapLayer }) {
<Polyline
key={`${item.feature.ref}-casing`}
positions={item.line}
+ pane={linePane}
pathOptions={{
color: item.disc,
weight: LINE_WEIGHT[item.relation] + 3,
@@ -89,6 +119,7 @@ export function PaddleLayerLeaflet({ layer }: { layer?: PaddleMapLayer }) {
<Polyline
key={`${item.feature.ref}-line`}
positions={item.line}
+ pane={linePane}
bubblingMouseEvents={false}
pathOptions={{
color: item.color,
diff --git a/client/src/components/Tours/paddle/usePaddleLayerGL.ts b/client/src/components/Tours/paddle/usePaddleLayerGL.ts
index 7a22b13b8..689061879 100644
--- a/client/src/components/Tours/paddle/usePaddleLayerGL.ts
+++ b/client/src/components/Tours/paddle/usePaddleLayerGL.ts
@@ -15,6 +15,8 @@ import {
export type PaddleGlMap = Map;
const SOURCE = 'trek-paddle';
+/** The casing of the route the map draws (MapViewGL), which the paddle lines go under. */
+const ROUTE_CASING = 'trip-route-casing';
const CASING = 'trek-paddle-casing';
const CASING_AREA = 'trek-paddle-casing-area';
const LINE = 'trek-paddle-line';
@@ -116,7 +118,11 @@ function addLayers(map: PaddleGlMap, data: GeoJSON.FeatureCollection): void {
};
// The objects only in the area wait for PADDLE_AREA_MIN_ZOOM; a hidden layer is also no click target.
const area = { minzoom: PADDLE_AREA_MIN_ZOOM };
- const add = map.addLayer.bind(map);
+ // The lines go under the tour's own route, so a rule that covers a whole river never hides
+ // the line planned along it; the pins stay on top, where they are picked.
+ const underRoute = map.getLayer?.(ROUTE_CASING) ? ROUTE_CASING : undefined;
+ const add = (layer: Parameters<PaddleGlMap['addLayer']>[0]) =>
+ map.addLayer(layer, layer.type === 'line' ? underRoute : undefined);
add({
id: CASING_AREA,
type: 'line',
diff --git a/server/src/db/repositories/PaddleWaterGraphs.repository.ts b/server/src/db/repositories/PaddleWaterGraphs.repository.ts
index 0fb22d99c..cd68a4bf5 100644
--- a/server/src/db/repositories/PaddleWaterGraphs.repository.ts
+++ b/server/src/db/repositories/PaddleWaterGraphs.repository.ts
@@ -83,4 +83,18 @@ export class PaddleWaterGraphsRepository extends TrekRepository<PaddleWaterGraph
)
.execute();
}
+
+ /**
+ * Forgets a region's network when its file is gone, but only that file: a
+ * network built again meanwhile under another name stays. Answers whether
+ * one was forgotten.
+ */
+ async forgetMissing(regionId: string, file: string): Promise<boolean> {
+ const result = await this.db()
+ .deleteFrom('paddle_water_graphs')
+ .where('region_id', '=', regionId)
+ .where('file', '=', file)
+ .executeTakeFirst();
+ return Number(result.numDeletedRows ?? 0) > 0;
+ }
}
diff --git a/server/src/nest/paddle-routing/paddle-routing.service.ts b/server/src/nest/paddle-routing/paddle-routing.service.ts
index d5b326c63..7c05d951c 100644
--- a/server/src/nest/paddle-routing/paddle-routing.service.ts
+++ b/server/src/nest/paddle-routing/paddle-routing.service.ts
@@ -22,6 +22,12 @@ import { join } from 'node:path';
const GRAPH_SUFFIX = '.wgraph';
+/** Whether the router failed because the network's file is not there. */
+function isMissingFile(err: unknown): boolean {
+ if (typeof err === 'object' && err !== null && (err as { code?: unknown }).code === 'ENOENT') return true;
+ return err instanceof Error && err.message.startsWith('ENOENT');
+}
+
type Network = NonNullable<PaddleRegion['network']>;
/**
@@ -62,7 +68,12 @@ export class PaddleRoutingService implements OnApplicationBootstrap, OnApplicati
}
async sweep(): Promise<void> {
- const kept = new Set((await this.graphs.list()).map((row) => row.file));
+ const rows = await this.graphs.list();
+ // A database that knows no network at all did not write these files: a test's, or a fresh
+ // one restored without the data folder. Clearing them would only throw away networks
+ // another database still names; an empty one leaves them be.
+ if (rows.length === 0) return;
+ const kept = new Set(rows.map((row) => row.file));
let entries: string[];
try {
entries = await readdir(this.dir());
@@ -194,7 +205,11 @@ export class PaddleRoutingService implements OnApplicationBootstrap, OnApplicati
);
}
- /** One region's legs planned in the router's thread; a thread that fails or hangs fails them all, as busy or timeout. */
+ /**
+ * One region's legs planned in the router's thread; a thread that fails or hangs fails them all, as busy or timeout.
+ *
+ * @txStandalone forgetting a network whose file is gone repairs the record on its own; nothing else is written with it.
+ */
private async planOn(
row: PaddleWaterGraphRow,
legs: PaddleRouteRequest['legs'],
@@ -205,6 +220,13 @@ export class PaddleRoutingService implements OnApplicationBootstrap, OnApplicati
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
logInfo(`[Paddle] routing on ${row.region_id}: ${message}`);
+ // The network's file is gone (a data folder moved or restored without it): the region
+ // has no network any more rather than a busy router. Forgotten, its legs say so, and
+ // loading the region (PAD-11C) builds the network again.
+ if (isMissingFile(err) && (await this.graphs.forgetMissing(row.region_id, row.file))) {
+ this.runner.forget(this.pathOf(row.file));
+ return legs.map((leg): PaddleRouteLeg => ({ status: 'failed', code: 'no_region', at: leg.from }));
+ }
const code = message === 'timeout' ? 'timeout' : 'busy';
return legs.map((): PaddleRouteLeg => ({ status: 'failed', code, at: null }));
}
diff --git a/server/tests/unit/nest/paddle-routing/paddle-routing.service.test.ts b/server/tests/unit/nest/paddle-routing/paddle-routing.service.test.ts
index 50c694916..1d912bcff 100644
--- a/server/tests/unit/nest/paddle-routing/paddle-routing.service.test.ts
+++ b/server/tests/unit/nest/paddle-routing/paddle-routing.service.test.ts
@@ -6,6 +6,7 @@
*/
import { PaddleRegions } from '../../../../src/db/entities/PaddleRegions.entity';
import { PaddleWaterGraphs } from '../../../../src/db/entities/PaddleWaterGraphs.entity';
+import { PaddleWaterGraphsRepository } from '../../../../src/db/repositories/PaddleWaterGraphs.repository';
import { AppConfigModule } from '../../../../src/nest/app-config/app-config.module';
import { DataPathsService } from '../../../../src/nest/app-config/data-paths.service';
import { withRequestContext } from '../../../../src/nest/database/request-context';
@@ -229,4 +230,41 @@ describe('PaddleRoutingService', () => {
await service.onApplicationBootstrap();
expect(log.logError).toHaveBeenCalledWith(expect.stringContaining('disk gone'));
});
+
+ it('PADROUTE-SVC-009: a sweep over a database that knows no network leaves every file on disk', async () => {
+ await mkdir(graphsDir(), { recursive: true });
+ const elsewhere = await service.writeGraphFile('oberpfalz', westGraph);
+ await inContext(() => service.sweep());
+ expect(await readdir(graphsDir())).toEqual([elsewhere]);
+ });
+
+ it('PADROUTE-SVC-010: a network whose file is gone is forgotten and its legs say no region, so loading the region builds it again', async () => {
+ const file = await install('oberpfalz', westGraph);
+ await rm(join(graphsDir(), file));
+ const leg = { from: west.at(0, 5000), to: west.at(0, 1000) };
+ expect((await plan({ legs: [leg] })).legs).toEqual([
+ { status: 'failed', code: 'no_region', at: paddleRouteRequestSchema.parse({ legs: [leg] }).legs[0]?.from },
+ ]);
+ expect(await countRows(orm(), PaddleWaterGraphs)).toBe(0);
+ expect((await inContext(() => service.networks())).has('oberpfalz')).toBe(false);
+ // Asked again, there is no network to fail on: the region answers as one without.
+ expect((await plan({ legs: [leg] })).legs[0]).toMatchObject({ status: 'failed', code: 'no_region' });
+ });
+
+ it('PADROUTE-SVC-011: a missing file of a network built anew meanwhile forgets nothing', async () => {
+ const runner: RouterRunner = {
+ run: vi.fn().mockRejectedValue(Object.assign(new Error('ENOENT: no such file'), { code: 'ENOENT' })),
+ forget: vi.fn(),
+ close: vi.fn().mockResolvedValue(undefined),
+ };
+ await boot(runner);
+ await install('oberpfalz', westGraph);
+ await updateRows(orm(), PaddleWaterGraphs, { region_id: 'oberpfalz' }, { file: 'oberpfalz.rebuilt.wgraph' });
+ const leg = { from: west.at(0, 5000), to: west.at(0, 1000) };
+ // The row names another file by now, so the one that failed is not this network's: busy, nothing forgotten.
+ const raced = vi.spyOn(PaddleWaterGraphsRepository.prototype, 'forgetMissing').mockResolvedValueOnce(false);
+ expect((await plan({ legs: [leg] })).legs).toEqual([{ status: 'failed', code: 'busy', at: null }]);
+ expect(raced).toHaveBeenCalled();
+ expect(await countRows(orm(), PaddleWaterGraphs)).toBe(1);
+ });
});