refactor(ScanController, ResponseScanIntake, RunnerKV): Rename totalDistance to distance for consistency
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
import type { Request } from "express";
|
||||
import { Authorized, Body, Delete, Get, HttpError, JsonController, OnUndefined, Param, Post, Put, QueryParam, Req, UseBefore } from 'routing-controllers';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { Repository, getConnection, getConnectionManager } from 'typeorm';
|
||||
import { getConnection, getConnectionManager, Repository } from 'typeorm';
|
||||
import { RunnerNotFoundError } from '../errors/RunnerErrors';
|
||||
import { ScanIdsNotMatchingError, ScanNotFoundError } from '../errors/ScanErrors';
|
||||
import { ScanStationNotFoundError } from '../errors/ScanStationErrors';
|
||||
import ScanAuth from '../middlewares/ScanAuth';
|
||||
import { deleteCardEntry, getCardEntry, setCardEntry } from '../nats/CardKV';
|
||||
import { deleteRunnerEntry, getRunnerEntry, RunnerKVEntry, setRunnerEntry, warmRunner } from '../nats/RunnerKV';
|
||||
import { getStationEntryById } from '../nats/StationKV';
|
||||
import { CreateScan } from '../models/actions/create/CreateScan';
|
||||
import { CreateTrackScan } from '../models/actions/create/CreateTrackScan';
|
||||
import { UpdateScan } from '../models/actions/update/UpdateScan';
|
||||
@@ -20,6 +17,9 @@ import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||
import { ResponseScan } from '../models/responses/ResponseScan';
|
||||
import { ResponseScanIntake, ResponseScanIntakeRunner } from '../models/responses/ResponseScanIntake';
|
||||
import { ResponseTrackScan } from '../models/responses/ResponseTrackScan';
|
||||
import { getCardEntry, setCardEntry } from '../nats/CardKV';
|
||||
import { deleteRunnerEntry, getRunnerEntry, RunnerKVEntry, setRunnerEntry, warmRunner } from '../nats/RunnerKV';
|
||||
import { getStationEntryById } from '../nats/StationKV';
|
||||
@JsonController('/scans')
|
||||
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
||||
export class ScanController {
|
||||
@@ -145,12 +145,12 @@ export class ScanController {
|
||||
// Compute
|
||||
const lapTime = entry.latestTimestamp === 0 ? 0 : now - entry.latestTimestamp;
|
||||
const valid = minimumLapTime === 0 || lapTime > minimumLapTime;
|
||||
const newDistance = entry.totalDistance + (valid ? trackDistance : 0);
|
||||
const newDistance = entry.distance + (valid ? trackDistance : 0);
|
||||
const newTimestamp = valid ? now : entry.latestTimestamp;
|
||||
|
||||
const updated: RunnerKVEntry = {
|
||||
displayName: entry.displayName,
|
||||
totalDistance: newDistance,
|
||||
distance: newDistance,
|
||||
latestTimestamp: newTimestamp,
|
||||
};
|
||||
|
||||
@@ -174,7 +174,7 @@ export class ScanController {
|
||||
|
||||
const runnerInfo = new ResponseScanIntakeRunner();
|
||||
runnerInfo.displayName = entry.displayName;
|
||||
runnerInfo.totalDistance = newDistance;
|
||||
runnerInfo.distance = newDistance;
|
||||
|
||||
response = new ResponseScanIntake();
|
||||
response.accepted = true;
|
||||
|
||||
@@ -11,7 +11,7 @@ export class ResponseScanIntakeRunner {
|
||||
displayName: string;
|
||||
|
||||
@IsInt()
|
||||
totalDistance: number;
|
||||
distance: number;
|
||||
}
|
||||
|
||||
export class ResponseScanIntake {
|
||||
|
||||
@@ -14,7 +14,7 @@ export interface RunnerKVEntry {
|
||||
/** "Firstname Lastname" — middlename omitted. */
|
||||
displayName: string;
|
||||
/** Sum of all valid scan distances in metres. */
|
||||
totalDistance: number;
|
||||
distance: number;
|
||||
/** Unix seconds timestamp of the last valid scan. 0 if none. */
|
||||
latestTimestamp: number;
|
||||
}
|
||||
@@ -93,7 +93,7 @@ export async function deleteRunnerEntry(runnerId: number): Promise<void> {
|
||||
* scan timestamp from the database, writes the result to KV, and returns it.
|
||||
*
|
||||
* Called on any KV cache miss during the scan intake flow.
|
||||
* Also handles the first-scan-ever case — latestTimestamp=0, totalDistance=0.
|
||||
* Also handles the first-scan-ever case — latestTimestamp=0, distance=0.
|
||||
*/
|
||||
export async function warmRunner(runnerId: number): Promise<RunnerKVEntry> {
|
||||
const connection = getConnection();
|
||||
@@ -119,7 +119,7 @@ export async function warmRunner(runnerId: number): Promise<RunnerKVEntry> {
|
||||
|
||||
const entry: RunnerKVEntry = {
|
||||
displayName,
|
||||
totalDistance: parseInt(distanceResult?.total ?? '0', 10),
|
||||
distance: parseInt(distanceResult?.total ?? '0', 10),
|
||||
latestTimestamp: latestScan?.timestamp ?? 0,
|
||||
};
|
||||
|
||||
@@ -180,7 +180,7 @@ export async function warmAll(): Promise<void> {
|
||||
const writePromises = runners.map((runner) => {
|
||||
const entry: RunnerKVEntry = {
|
||||
displayName: `${runner.firstname} ${runner.lastname}`,
|
||||
totalDistance: distanceMap.get(runner.id) ?? 0,
|
||||
distance: distanceMap.get(runner.id) ?? 0,
|
||||
latestTimestamp: latestMap.get(runner.id) ?? 0,
|
||||
};
|
||||
return setRunnerEntry(runner.id, entry);
|
||||
|
||||
Reference in New Issue
Block a user