From b5690d89837e0b5a71ab7f51b4b9a5d041ee2fd9 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 13 Nov 2025 16:52:39 -0800 Subject: [PATCH] Also implement the fallback logic in the in-memory repository --- .../eta/InMemorySelfUpdatingETARepository.ts | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/repositories/shuttle/eta/InMemorySelfUpdatingETARepository.ts b/src/repositories/shuttle/eta/InMemorySelfUpdatingETARepository.ts index be2cea9..88fe3d9 100644 --- a/src/repositories/shuttle/eta/InMemorySelfUpdatingETARepository.ts +++ b/src/repositories/shuttle/eta/InMemorySelfUpdatingETARepository.ts @@ -67,8 +67,10 @@ export class InMemorySelfUpdatingETARepository extends BaseInMemoryETARepository referenceCurrentTime = this.referenceTime; } const oneWeekAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 24 * 7 * 1000)); + const oneDayAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 24 * 1000)); + const oneHourAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 1000)); - const travelTimeSeconds = await this.getAverageTravelTimeSeconds({ + let travelTimeSeconds = await this.getAverageTravelTimeSeconds({ routeId: shuttle.routeId, fromStopId: lastStop.stopId, toStopId: nextStop.stopId, @@ -76,6 +78,30 @@ export class InMemorySelfUpdatingETARepository extends BaseInMemoryETARepository from: oneWeekAgo, to: new Date(oneWeekAgo.getTime() + (60 * 60 * 1000)) }); + + // Fallback to yesterday at the same time if no data + if (travelTimeSeconds == undefined) { + travelTimeSeconds = await this.getAverageTravelTimeSeconds({ + routeId: shuttle.routeId, + fromStopId: lastStop.stopId, + toStopId: nextStop.stopId, + }, { + from: oneDayAgo, + to: new Date(oneDayAgo.getTime() + (60 * 60 * 1000)) + }); + } + // Fallback to last hour if still no data + if (travelTimeSeconds == undefined) { + travelTimeSeconds = await this.getAverageTravelTimeSeconds({ + routeId: shuttle.routeId, + fromStopId: lastStop.stopId, + toStopId: nextStop.stopId, + }, { + from: oneHourAgo, + to: new Date(), + }); + } + if (travelTimeSeconds == undefined) return; const elapsedTimeMs = referenceCurrentTime.getTime() - lastStop.timestamp.getTime();