diff --git a/src/repositories/shuttle/eta/InMemorySelfUpdatingETARepository.ts b/src/repositories/shuttle/eta/InMemorySelfUpdatingETARepository.ts index 88fe3d9..14a6a1a 100644 --- a/src/repositories/shuttle/eta/InMemorySelfUpdatingETARepository.ts +++ b/src/repositories/shuttle/eta/InMemorySelfUpdatingETARepository.ts @@ -54,6 +54,19 @@ export class InMemorySelfUpdatingETARepository extends BaseInMemoryETARepository this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleWillArriveAtStop); } + private async getAverageTravelTimeSecondsWithFallbacks( + identifier: ShuttleTravelTimeDataIdentifier, + dateFilters: ShuttleTravelTimeDateFilterArguments[] + ): Promise { + for (const dateFilter of dateFilters) { + const result = await this.getAverageTravelTimeSeconds(identifier, dateFilter); + if (result !== undefined) { + return result; + } + } + return undefined; + } + private async handleShuttleUpdate(shuttle: IShuttle): Promise { const lastStop = await this.shuttleRepository.getShuttleLastStopArrival(shuttle.id); if (!lastStop) return; @@ -70,37 +83,24 @@ export class InMemorySelfUpdatingETARepository extends BaseInMemoryETARepository const oneDayAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 24 * 1000)); const oneHourAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 1000)); - let travelTimeSeconds = await this.getAverageTravelTimeSeconds({ + const travelTimeSeconds = await this.getAverageTravelTimeSecondsWithFallbacks({ routeId: shuttle.routeId, fromStopId: lastStop.stopId, toStopId: nextStop.stopId, - }, { - 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: oneWeekAgo, + to: new Date(oneWeekAgo.getTime() + (60 * 60 * 1000)) + }, + { 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; diff --git a/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts b/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts index 8a62beb..76e3474 100644 --- a/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts +++ b/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts @@ -77,6 +77,19 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleWillArriveAtStop); } + private async getAverageTravelTimeSecondsWithFallbacks( + identifier: ShuttleTravelTimeDataIdentifier, + dateFilters: ShuttleTravelTimeDateFilterArguments[] + ): Promise { + for (const dateFilter of dateFilters) { + const result = await this.getAverageTravelTimeSeconds(identifier, dateFilter); + if (result !== undefined) { + return result; + } + } + return undefined; + } + private async handleShuttleUpdate(shuttle: IShuttle) { const lastStop = await this.shuttleRepository.getShuttleLastStopArrival(shuttle.id); if (!lastStop) return; @@ -93,37 +106,24 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple const oneDayAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 24 * 1000)); const oneHourAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 1000)); - let travelTimeSeconds = await this.getAverageTravelTimeSeconds({ + const travelTimeSeconds = await this.getAverageTravelTimeSecondsWithFallbacks({ routeId: shuttle.routeId, fromStopId: lastStop.stopId, toStopId: nextStop.stopId, - }, { - 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: oneWeekAgo, + to: new Date(oneWeekAgo.getTime() + (60 * 60 * 1000)) + }, + { 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;