From 321631e6c9b0636a02ae2a485ad17e4bee2a5cc1 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 10 Nov 2025 14:49:41 -0800 Subject: [PATCH] Add partial implementation and scaffolding of historical ETA update --- .../shuttle/RedisShuttleRepository.ts | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index cf6e063..d6e03d6 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -1,7 +1,7 @@ import EventEmitter from "node:events"; import { createClient } from 'redis'; import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository"; -import { IEta, IOrderedStop, IRoute, IShuttle, IStop } from "../../entities/ShuttleRepositoryEntities"; +import { IEta, IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities"; import { REDIS_RECONNECT_INTERVAL } from "../../environment"; import { ShuttleRepositoryEvent, @@ -10,6 +10,11 @@ import { ShuttleRepositoryEventPayloads, } from "./ShuttleGetterRepository"; +export interface ShuttleStopArrival { + stopId: string; + timestamp: Date; +} + export class RedisShuttleRepository extends EventEmitter implements ShuttleGetterSetterRepository { protected redisClient; @@ -405,9 +410,48 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette public async addOrUpdateShuttle(shuttle: IShuttle): Promise { const key = this.createShuttleKey(shuttle.id); await this.redisClient.hSet(key, this.createRedisHashFromShuttle(shuttle)); + + await this.updateHistoricalEtasForShuttle(shuttle); } - public async addOrUpdateStop(stop: IStop): Promise { + private async updateHistoricalEtasForShuttle(shuttle: IShuttle) { + const arrivedStop = await this.getArrivedStopIfExists(shuttle); + + if (arrivedStop != undefined) { + const lastStopTimestamp = await this.getShuttleLastStopTimestamp(shuttle) + if (lastStopTimestamp != undefined) { + const now = Date(); + const routeId = shuttle.routeId + const fromStopId = lastStopTimestamp.stopId; + const toStopId = arrivedStop.id; + + // Create an entry in Redis time series + // Key: routeId:fromStopId:toStopId: + // Value: seconds it took to get from lastStopTimestamp.stopId to arrivedStop.id + } + + // TODO: Update the "last stop timestamp" + } + } + + public async getArrivedStopIfExists(shuttle: IShuttle): Promise { + const orderedStops = await this.getOrderedStopsByRouteId(shuttle.routeId); + + for (const orderedStop of orderedStops) { + const stop = await this.getStopById(orderedStop.stopId); + if (stop != null && shuttleHasArrivedAtStop(shuttle, stop)) { + return stop; + } + return undefined; + } + } + + public async getShuttleLastStopTimestamp(shuttle: IShuttle): Promise { + throw new Error("Method not implemented."); + } + + + public async addOrUpdateStop(stop: IStop): Promise { const key = this.createStopKey(stop.id); await this.redisClient.hSet(key, this.createRedisHashFromStop(stop)); }