mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
d30b916f9c
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.
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { Body, Controller, Delete, Get, HttpCode, Post, UseGuards } from '@nestjs/common';
|
|
import type { RouteUsageReportResult, RouteUsageSummaryResult } from '@trek/shared';
|
|
import { AdminGuard } from '../auth/admin.guard';
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
import { RouteUsageReportDto } from './route-usage.dto';
|
|
import { RouteUsageService } from './route-usage.service';
|
|
|
|
/**
|
|
* /api/route-usage — how much routing this instance does.
|
|
*
|
|
* Reporting is open to any signed-in user, because every signed-in user is the one
|
|
* generating the load; reading is admin only, because the totals are everybody's
|
|
* routing and the person entitled to see that is the person running the instance.
|
|
* Nothing here leaves the instance on its own.
|
|
*/
|
|
@Controller('api/route-usage')
|
|
@UseGuards(JwtAuthGuard)
|
|
export class RouteUsageController {
|
|
constructor(private readonly usage: RouteUsageService) {}
|
|
|
|
/**
|
|
* 200 with `{ recorded: false }` when counting is off, rather than a 403. The
|
|
* client flushes and moves on; a rejection would be a permanent error in the
|
|
* console of every install that switched the counters off.
|
|
*/
|
|
@Post('report')
|
|
@HttpCode(200)
|
|
report(@Body() body: RouteUsageReportDto): RouteUsageReportResult {
|
|
return { recorded: this.usage.record(body) };
|
|
}
|
|
|
|
@Get('summary')
|
|
@UseGuards(JwtAuthGuard, AdminGuard)
|
|
summary(): RouteUsageSummaryResult {
|
|
return this.usage.summary();
|
|
}
|
|
|
|
@Delete()
|
|
@UseGuards(JwtAuthGuard, AdminGuard)
|
|
clear(): { removed: number } {
|
|
return { removed: this.usage.clear() };
|
|
}
|
|
}
|