Also implement the fallback logic in the in-memory repository

This commit is contained in:
2025-11-13 16:52:39 -08:00
parent 076f1fd1e4
commit b5690d8983

View File

@@ -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();