mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Implement updateEtasBasedOnHistoricalData and add a test for it
This commit is contained in:
@@ -425,21 +425,48 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
|
||||
public async addOrUpdateShuttle(
|
||||
shuttle: IShuttle,
|
||||
travelTimeTimestamp = Date.now(),
|
||||
referenceCurrentTime = new Date(),
|
||||
): Promise<void> {
|
||||
const key = this.createShuttleKey(shuttle.id);
|
||||
await this.redisClient.hSet(key, this.createRedisHashFromShuttle(shuttle));
|
||||
|
||||
await this.updateHistoricalEtasForShuttle(shuttle, travelTimeTimestamp);
|
||||
await this.updateEtasBasedOnHistoricalData(shuttle);
|
||||
await this.updateEtasBasedOnHistoricalData(shuttle, referenceCurrentTime);
|
||||
}
|
||||
|
||||
public async updateEtasBasedOnHistoricalData(
|
||||
private async updateEtasBasedOnHistoricalData(
|
||||
shuttle: IShuttle,
|
||||
referenceCurrentTime: Date = new Date(),
|
||||
) {
|
||||
// Based on historical data for the key provided by this.createHistoricalEtaTimeSeriesKey(routeId, fromStopId, toStopId);
|
||||
// Call this.addOrUpdateEta with the correct information
|
||||
// "Historical data" being averaged time taken for the current hour of the same day
|
||||
// in the past week, based on the reference time
|
||||
const oneWeekAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 24 * 7 * 1000))
|
||||
|
||||
const lastStopArrival = await this.getShuttleLastStopArrival(shuttle)
|
||||
if (lastStopArrival == undefined) return;
|
||||
|
||||
const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStopArrival.stopId);
|
||||
const nextStop = lastOrderedStop?.nextStop;
|
||||
if (nextStop == null) return;
|
||||
|
||||
const travelTimeSeconds = await this.getAverageTravelTimeSeconds({
|
||||
routeId: shuttle.routeId,
|
||||
fromStopId: lastStopArrival.stopId,
|
||||
toStopId: nextStop.stopId,
|
||||
}, {
|
||||
from: oneWeekAgo,
|
||||
to: new Date(oneWeekAgo.getTime() + (60 * 60 * 1000))
|
||||
});
|
||||
if (travelTimeSeconds == undefined) return;
|
||||
|
||||
const elapsedTimeMs = referenceCurrentTime.getTime() - lastStopArrival.timestamp.getTime();
|
||||
const predictedTimeSeconds = travelTimeSeconds - (elapsedTimeMs / 1000);
|
||||
|
||||
await this.addOrUpdateEta({
|
||||
secondsRemaining: predictedTimeSeconds,
|
||||
shuttleId: shuttle.id,
|
||||
stopId: nextStop.stopId,
|
||||
systemId: nextStop.systemId,
|
||||
updatedTime: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
private async updateHistoricalEtasForShuttle(
|
||||
@@ -469,7 +496,7 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
|
||||
public async getAverageTravelTimeSeconds(
|
||||
{ routeId, fromStopId, toStopId }: ShuttleTravelTimeDataIdentifier,
|
||||
{ from, to }: ShuttleTravelTimeDateFilterArguments,
|
||||
): Promise<number> {
|
||||
): Promise<number | undefined> {
|
||||
const timeSeriesKey = this.createHistoricalEtaTimeSeriesKey(routeId, fromStopId, toStopId);
|
||||
const fromTimestamp = from.getTime();
|
||||
const toTimestamp = to.getTime();
|
||||
@@ -493,7 +520,8 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
|
||||
|
||||
throw new Error(`No historical data found for route ${routeId} from stop ${fromStopId} to stop ${toStopId}`);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to get average travel time: ${error instanceof Error ? error.message : String(error)}`);
|
||||
console.warn(`Failed to get average travel time: ${error instanceof Error ? error.message : String(error)}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user