Make addTravelTimeDataPoint public, add tests, and fix the query interval

This commit is contained in:
2025-11-10 19:05:32 -08:00
parent 7a0937c04e
commit 864097154e
2 changed files with 115 additions and 6 deletions

View File

@@ -117,7 +117,7 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
private createEtaKey = (shuttleId: string, stopId: string) => `shuttle:eta:${shuttleId}:${stopId}`;
private createOrderedStopKey = (routeId: string, stopId: string) => `shuttle:orderedstop:${routeId}:${stopId}`;
private createShuttleLastStopKey = (shuttleId: string) => `shuttle:laststop:${shuttleId}`;
private createHistoricalEtaTimeSeriesKey =(routeId: string, fromStopId: string, toStopId: string) => {
private createHistoricalEtaTimeSeriesKey = (routeId: string, fromStopId: string, toStopId: string) => {
return `shuttle:eta:historical:${routeId}:${fromStopId}:${toStopId}`;
}
@@ -446,7 +446,7 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStopArrival.stopId);
const nextStop = lastOrderedStop?.nextStop;
if (nextStop == null) return;
const travelTimeSeconds = await this.getAverageTravelTimeSeconds({
routeId: shuttle.routeId,
fromStopId: lastStopArrival.stopId,
@@ -500,7 +500,7 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
const timeSeriesKey = this.createHistoricalEtaTimeSeriesKey(routeId, fromStopId, toStopId);
const fromTimestamp = from.getTime();
const toTimestamp = to.getTime();
const intervalMs = toTimestamp - fromTimestamp;
const intervalMs = toTimestamp - fromTimestamp + 1;
try {
const aggregationResult = await this.redisClient.sendCommand([
@@ -518,14 +518,14 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
return parseFloat(averageValue);
}
throw new Error(`No historical data found for route ${routeId} from stop ${fromStopId} to stop ${toStopId}`);
return;
} catch (error) {
console.warn(`Failed to get average travel time: ${error instanceof Error ? error.message : String(error)}`);
return;
}
}
private async addTravelTimeDataPoint(
public async addTravelTimeDataPoint(
{ routeId, fromStopId, toStopId }: ShuttleTravelTimeDataIdentifier,
travelTimeSeconds: number,
timestamp = Date.now(),
@@ -634,7 +634,7 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
});
}
public async addOrUpdateStop(stop: IStop): Promise<void> {
public async addOrUpdateStop(stop: IStop): Promise<void> {
const key = this.createStopKey(stop.id);
await this.redisClient.hSet(key, this.createRedisHashFromStop(stop));
}