mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Extract fallback logic to a separate method
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user