Add a helper method for setting the "reference time" of the self-updating repo

This commit is contained in:
2025-11-11 19:34:05 -08:00
parent e8f4e7b9ee
commit 4b37f3a121
3 changed files with 58 additions and 5 deletions

View File

@@ -16,30 +16,73 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple
reconnectStrategy: REDIS_RECONNECT_INTERVAL,
},
}),
private referenceTime: Date | null = null,
) {
super(redisClient);
}
setReferenceTime(referenceTime: Date) {
this.referenceTime = referenceTime;
}
getAverageTravelTimeSeconds(identifier: ShuttleTravelTimeDataIdentifier, dateFilter: ShuttleTravelTimeDateFilterArguments): Promise<number | undefined> {
throw new Error("Method not implemented.");
}
startListeningForUpdates() {
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate);
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleArriveAtStop)
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleWillArriveAtStop)
}
stopListeningForUpdates() {
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate);
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleArriveAtStop);
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleWillArriveAtStop);
}
private handleShuttleUpdate(shuttle: IShuttle) {
// TODO: handle shuttle update
}
private handleShuttleArriveAtStop(shuttleArrival: ShuttleStopArrival) {
// TODO: handle shuttle arrive at stop
private async handleShuttleWillArriveAtStop(
shuttleArrival: ShuttleStopArrival,
) {
const lastStop = await this.shuttleRepository.getShuttleLastStopArrival(shuttleArrival.shuttleId);
if (lastStop == undefined) return;
const shuttle = await this.shuttleRepository.getShuttleById(shuttleArrival.shuttleId);
if (shuttle == null) return;
let referenceCurrentTime = new Date();
if (this.referenceTime != null) {
referenceCurrentTime = this.referenceTime;
}
const oneWeekAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 24 * 7 * 1000));
const lastOrderedStop = await this.shuttleRepository.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId);
const nextStop = lastOrderedStop?.nextStop;
if (nextStop == null) return;
const travelTimeSeconds = await this.getAverageTravelTimeSeconds({
routeId: shuttle.routeId,
fromStopId: lastStop.stopId,
toStopId: nextStop.stopId,
}, {
from: oneWeekAgo,
to: new Date(oneWeekAgo.getTime() + (60 * 60 * 1000))
});
if (travelTimeSeconds == undefined) return;
const elapsedTimeMs = referenceCurrentTime.getTime() - lastStop.timestamp.getTime();
const predictedTimeSeconds = travelTimeSeconds - (elapsedTimeMs / 1000);
await this.addOrUpdateEta({
secondsRemaining: predictedTimeSeconds,
shuttleId: shuttle.id,
stopId: nextStop.stopId,
systemId: nextStop.systemId,
updatedTime: new Date(),
});
}
}