diff --git a/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts b/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts index 50af4a3..8a62beb 100644 --- a/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts +++ b/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts @@ -90,8 +90,10 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple 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, @@ -99,6 +101,30 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple 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();