From b9fefcc6a9eb30736b0be8d6c190010cf89d0365 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 11 Nov 2025 19:05:47 -0800 Subject: [PATCH] Implement BaseRedisETARepository based on past implementation --- .../shuttle/eta/BaseRedisETARepository.ts | 62 +++++++++++++++++-- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/src/repositories/shuttle/eta/BaseRedisETARepository.ts b/src/repositories/shuttle/eta/BaseRedisETARepository.ts index 13da39e..7df36f8 100644 --- a/src/repositories/shuttle/eta/BaseRedisETARepository.ts +++ b/src/repositories/shuttle/eta/BaseRedisETARepository.ts @@ -3,16 +3,66 @@ import { BaseRedisRepository } from "../../BaseRedisRepository"; import { ETAGetterRepository, ETARepositoryEventListener, ETARepositoryEventName } from "./ETAGetterRepository"; export abstract class BaseRedisETARepository extends BaseRedisRepository implements ETAGetterRepository { - getEtasForShuttleId(shuttleId: string): Promise { - throw new Error("Method not implemented."); + private static readonly ETA_KEY_PREFIX = 'shuttle:eta:'; + + // Helper methods + private createEtaKey = (shuttleId: string, stopId: string) => + `${BaseRedisETARepository.ETA_KEY_PREFIX}${shuttleId}:${stopId}`; + + createRedisHashFromEta = (eta: IEta): Record => ({ + secondsRemaining: eta.secondsRemaining.toString(), + shuttleId: eta.shuttleId, + stopId: eta.stopId, + systemId: eta.systemId, + updatedTime: eta.updatedTime.toISOString(), + }); + + createEtaFromRedisData = (data: Record): IEta => ({ + secondsRemaining: parseFloat(data.secondsRemaining), + shuttleId: data.shuttleId, + stopId: data.stopId, + systemId: data.systemId, + updatedTime: new Date(data.updatedTime), + }); + + // Getter implementations + async getEtasForShuttleId(shuttleId: string): Promise { + const keys = await this.redisClient.keys(`${BaseRedisETARepository.ETA_KEY_PREFIX}${shuttleId}:*`); + const etas: IEta[] = []; + + for (const key of keys) { + const data = await this.redisClient.hGetAll(key); + if (Object.keys(data).length > 0) { + etas.push(this.createEtaFromRedisData(data)); + } + } + + return etas; } - getEtasForStopId(stopId: string): Promise { - throw new Error("Method not implemented."); + async getEtasForStopId(stopId: string): Promise { + const keys = await this.redisClient.keys(`${BaseRedisETARepository.ETA_KEY_PREFIX}*`); + const etas: IEta[] = []; + + for (const key of keys) { + const data = await this.redisClient.hGetAll(key); + if (Object.keys(data).length > 0 && data.stopId === stopId) { + etas.push(this.createEtaFromRedisData(data)); + } + } + + return etas; } - getEtaForShuttleAndStopId(shuttleId: string, stopId: string): Promise { - throw new Error("Method not implemented."); + async getEtaForShuttleAndStopId(shuttleId: string, stopId: string): Promise { + const key = this.createEtaKey(shuttleId, stopId); + const data = await this.redisClient.hGetAll(key); + + if (Object.keys(data).length === 0) { + return null; + } + + return this.createEtaFromRedisData(data); } // EventEmitter override methods for type safety