Files
TREK/server/src/nest/route-usage/route-usage.job.ts
T
Maurice d30b916f9c feat(maps): count how much routing an instance actually does
Every route is calculated in the browser against the public FOSSGIS hosts, so the
server never sees a routing request and nobody has a number: not how many a real trip
costs, not how big they are, not how much already goes to a self-hosted engine.
Deciding whether TREK could run its own router without that number is guessing at the
bill, and the counters have to be collecting before the question comes up.

Counters, not a log. A row is a day, a profile, a request kind and an engine kind with
totals on it: how many requests, how many waypoints, roughly how far, how many were
refused. No query, no coordinate, no route, no user, no trip. Nothing leaves the
instance: its own database holds them and its own admin reads them at
GET /api/route-usage/summary.

The client tallies in memory and flushes in batches — on a timer, past a threshold and
when the page is hidden — because a counter that cost a request of its own would
measure the load by adding to it. Routing all funnels through one fetch in
RouteCalculator, so that is the only place instrumented.

On by default and switchable off with route_usage_enabled, unlike the shadow log next
door: that one stores what people typed, this one stores four integers about a day.
2026-09-09 14:52:50 +02:00

39 lines
1.3 KiB
TypeScript

import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
import { logError, logInfo } from '../audit/audit-log.logger';
import { CronRegistrarService } from '../scheduling/cron-registrar.service';
import { RouteUsageService } from './route-usage.service';
/**
* Nightly retention for the routing counters.
*
* Runs even when counting is switched off, on purpose: switching it off should also
* let what it already counted age out, rather than freezing the rows in place until
* somebody remembers to press the wipe button.
*
* 3:45 AM, just after the shadow log's own sweep and clear of the 3:00 idempotency
* purge and the backup window.
*/
@Injectable()
export class RouteUsageRetentionJob implements OnApplicationBootstrap {
constructor(
private readonly usage: RouteUsageService,
private readonly registrar: CronRegistrarService,
) {}
onApplicationBootstrap(): void {
if (!this.registrar.isEnabled()) return;
this.registrar.register('route-usage-retention', '45 3 * * *', () => this.tick());
}
tick(): void {
try {
const removed = this.usage.purgeExpired();
if (removed > 0) {
logInfo(`Route usage retention: removed ${removed} expired row(s)`);
}
} catch (err: unknown) {
logError(`Route usage retention: ${err instanceof Error ? err.message : err}`);
}
}
}