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

@@ -8,6 +8,9 @@ export class InMemorySelfUpdatingETARepository extends BaseInMemoryETARepository
) { ) {
super(); super();
} }
setReferenceTime(referenceTime: Date): void {
throw new Error("Method not implemented.");
}
getAverageTravelTimeSeconds(identifier: ShuttleTravelTimeDataIdentifier, dateFilter: ShuttleTravelTimeDateFilterArguments): Promise<number | undefined> { getAverageTravelTimeSeconds(identifier: ShuttleTravelTimeDataIdentifier, dateFilter: ShuttleTravelTimeDateFilterArguments): Promise<number | undefined> {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");

View File

@@ -16,30 +16,73 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple
reconnectStrategy: REDIS_RECONNECT_INTERVAL, reconnectStrategy: REDIS_RECONNECT_INTERVAL,
}, },
}), }),
private referenceTime: Date | null = null,
) { ) {
super(redisClient); super(redisClient);
} }
setReferenceTime(referenceTime: Date) {
this.referenceTime = referenceTime;
}
getAverageTravelTimeSeconds(identifier: ShuttleTravelTimeDataIdentifier, dateFilter: ShuttleTravelTimeDateFilterArguments): Promise<number | undefined> { getAverageTravelTimeSeconds(identifier: ShuttleTravelTimeDataIdentifier, dateFilter: ShuttleTravelTimeDateFilterArguments): Promise<number | undefined> {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
} }
startListeningForUpdates() { startListeningForUpdates() {
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate); 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() { stopListeningForUpdates() {
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate); 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) { private handleShuttleUpdate(shuttle: IShuttle) {
// TODO: handle shuttle update // TODO: handle shuttle update
} }
private handleShuttleArriveAtStop(shuttleArrival: ShuttleStopArrival) { private async handleShuttleWillArriveAtStop(
// TODO: handle shuttle arrive at stop 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(),
});
} }
} }

View File

@@ -18,4 +18,11 @@ export interface SelfUpdatingETARepository extends ETAGetterRepository {
identifier: ShuttleTravelTimeDataIdentifier, identifier: ShuttleTravelTimeDataIdentifier,
dateFilter: ShuttleTravelTimeDateFilterArguments dateFilter: ShuttleTravelTimeDateFilterArguments
): Promise<number | undefined>; ): Promise<number | undefined>;
/**
* Set the "current time" as the class knows it, in order to calculate
* ETAs based on past data.
* @param referenceTime
*/
setReferenceTime(referenceTime: Date): void;
} }