Extract fallback logic to a separate method

This commit is contained in:
2025-11-13 17:19:37 -08:00
parent 42c71815e6
commit 91584fc2ab
2 changed files with 48 additions and 48 deletions

View File

@@ -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<number | undefined> {
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<void> {
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;

View File

@@ -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<number | undefined> {
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;