Files
TREK/server/src/db/repositories/PaddleWaterGraphs.repository.ts
T
Maurice 66525c66b7 fix(tours): paddle notes under the route, and a lost water network heals itself
The lines of the paddle notes (a rule over a whole stretch, an obstacle's
extent) were drawn over the tour's own route, so where a rule covered the
river the route under it could not be seen. They now go under it: on
Leaflet in a pane placed right before the overlay pane (the app levels every
pane, so the order decides), on MapLibre/Mapbox before the route's casing.
The markers stay on top, where they are picked.

A region whose network file was gone answered every leg with "The route
planner is busy", which no retry could change. The router now forgets such
a network and its legs say the area has no water data, so loading the
region (PAD-11C) builds the network again and the planner plans on.

The boot sweep left a database without any network alone instead of
clearing every file it did not know: a test's empty database, or one
restored without its data folder, no longer throws away networks another
database still names.
2026-10-19 11:29:29 +02:00

101 lines
3.1 KiB
TypeScript

import type { PaddleWaterGraphs } from '../entities/PaddleWaterGraphs.entity';
import type { DB } from '../kysely/db';
import type { DbTimestamp } from '../types';
import { TrekRepository } from './_shared/trek-repository';
/** A region's water network as stored: where its file is, what it holds (`stats` still JSON text), the box it covers. */
export interface PaddleWaterGraphRow {
region_id: string;
file: string;
rev: string;
bytes: number;
built_at: string;
stats: string;
min_lat: number;
min_lng: number;
max_lat: number;
max_lng: number;
}
const COLUMNS = [
'region_id',
'file',
'rev',
'bytes',
'built_at',
'stats',
'min_lat',
'min_lng',
'max_lat',
'max_lng',
] as const;
/** `paddle_water_graphs`: the water network each paddle region built for routing (PAD-11). */
export class PaddleWaterGraphsRepository extends TrekRepository<PaddleWaterGraphs> {
private db() {
return this.kysely<Pick<DB, 'paddle_water_graphs'>>();
}
async list(): Promise<PaddleWaterGraphRow[]> {
return await this.db().selectFrom('paddle_water_graphs').select(COLUMNS).orderBy('region_id', 'asc').execute();
}
async findByRegion(regionId: string): Promise<PaddleWaterGraphRow | undefined> {
return await this.db()
.selectFrom('paddle_water_graphs')
.select(COLUMNS)
.where('region_id', '=', regionId)
.executeTakeFirst();
}
/** The networks whose box holds the point, smallest box first: the most local data plans the leg. */
async coveringPoint(lat: number, lng: number): Promise<PaddleWaterGraphRow[]> {
const rows = await this.db()
.selectFrom('paddle_water_graphs')
.select(COLUMNS)
.where('min_lat', '<=', lat)
.where('max_lat', '>=', lat)
.where('min_lng', '<=', lng)
.where('max_lng', '>=', lng)
.orderBy('region_id', 'asc')
.execute();
const area = (row: PaddleWaterGraphRow) => (row.max_lat - row.min_lat) * (row.max_lng - row.min_lng);
return rows.sort((a, b) => area(a) - area(b));
}
/** Records a region's new network, replacing the one it had. */
async saveGraph(row: Omit<PaddleWaterGraphRow, 'built_at'> & { built_at: DbTimestamp }): Promise<void> {
await this.db()
.insertInto('paddle_water_graphs')
.values(row)
.onConflict((oc) =>
oc.column('region_id').doUpdateSet({
file: row.file,
rev: row.rev,
bytes: row.bytes,
built_at: row.built_at,
stats: row.stats,
min_lat: row.min_lat,
min_lng: row.min_lng,
max_lat: row.max_lat,
max_lng: row.max_lng,
}),
)
.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;
}
}