mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Unify the new ETA functionality across all shuttle repositories
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
import EventEmitter from "node:events";
|
||||
import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository";
|
||||
import { IEta, IOrderedStop, IRoute, IShuttle, IStop } from "../../entities/ShuttleRepositoryEntities";
|
||||
import { IEta, IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities";
|
||||
import { IEntityWithId } from "../../entities/SharedEntities";
|
||||
import {
|
||||
ShuttleRepositoryEvent,
|
||||
ShuttleRepositoryEventListener,
|
||||
ShuttleRepositoryEventName,
|
||||
ShuttleRepositoryEventPayloads,
|
||||
ShuttleStopArrival,
|
||||
ShuttleTravelTimeDataIdentifier,
|
||||
ShuttleTravelTimeDateFilterArguments,
|
||||
} from "./ShuttleGetterRepository";
|
||||
|
||||
/**
|
||||
@@ -70,6 +73,8 @@ export class UnoptimizedInMemoryShuttleRepository
|
||||
private shuttles: IShuttle[] = [];
|
||||
private etas: IEta[] = [];
|
||||
private orderedStops: IOrderedStop[] = [];
|
||||
private shuttleLastStopArrivals: Map<string, ShuttleStopArrival> = new Map();
|
||||
private travelTimeData: Map<string, Array<{ timestamp: number; seconds: number }>> = new Map();
|
||||
|
||||
public async getStops(): Promise<IStop[]> {
|
||||
return this.stops;
|
||||
@@ -144,13 +149,20 @@ export class UnoptimizedInMemoryShuttleRepository
|
||||
}
|
||||
}
|
||||
|
||||
public async addOrUpdateShuttle(shuttle: IShuttle): Promise<void> {
|
||||
public async addOrUpdateShuttle(
|
||||
shuttle: IShuttle,
|
||||
travelTimeTimestamp = Date.now(),
|
||||
referenceCurrentTime = new Date(),
|
||||
): Promise<void> {
|
||||
const index = this.shuttles.findIndex((s) => s.id === shuttle.id);
|
||||
if (index !== -1) {
|
||||
this.shuttles[index] = shuttle;
|
||||
} else {
|
||||
this.shuttles.push(shuttle);
|
||||
}
|
||||
|
||||
await this.updateLastStopArrivalAndTravelTimeDataPoints(shuttle, travelTimeTimestamp);
|
||||
await this.updateEtasBasedOnHistoricalData(shuttle, referenceCurrentTime);
|
||||
}
|
||||
|
||||
public async addOrUpdateStop(stop: IStop): Promise<void> {
|
||||
@@ -171,7 +183,7 @@ export class UnoptimizedInMemoryShuttleRepository
|
||||
}
|
||||
}
|
||||
|
||||
public async addOrUpdateEta(eta: IEta): Promise<void> {
|
||||
private async addOrUpdateEta(eta: IEta): Promise<void> {
|
||||
const index = this.etas.findIndex((e) => e.stopId === eta.stopId && e.shuttleId === eta.shuttleId);
|
||||
if (index !== -1) {
|
||||
this.etas[index] = eta;
|
||||
@@ -181,6 +193,129 @@ export class UnoptimizedInMemoryShuttleRepository
|
||||
this.emit(ShuttleRepositoryEvent.ETA_UPDATED, eta);
|
||||
}
|
||||
|
||||
public async addOrUpdateEtaFromExternalSource(eta: IEta): Promise<void> {
|
||||
await this.addOrUpdateEta(eta);
|
||||
}
|
||||
|
||||
private async updateEtasBasedOnHistoricalData(
|
||||
shuttle: IShuttle,
|
||||
referenceCurrentTime: Date = new Date(),
|
||||
) {
|
||||
const oneWeekAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 24 * 7 * 1000));
|
||||
|
||||
const lastStopArrival = await this.getShuttleLastStopArrival(shuttle.id);
|
||||
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 updateLastStopArrivalAndTravelTimeDataPoints(
|
||||
shuttle: IShuttle,
|
||||
travelTimeTimestamp = Date.now(),
|
||||
) {
|
||||
const arrivedStop = await this.getArrivedStopIfExists(shuttle);
|
||||
|
||||
if (arrivedStop != undefined) {
|
||||
const lastStopTimestamp = await this.getShuttleLastStopArrival(shuttle.id);
|
||||
if (lastStopTimestamp != undefined) {
|
||||
const routeId = shuttle.routeId;
|
||||
const fromStopId = lastStopTimestamp.stopId;
|
||||
const toStopId = arrivedStop.id;
|
||||
|
||||
const travelTimeSeconds = (travelTimeTimestamp - lastStopTimestamp.timestamp.getTime()) / 1000;
|
||||
await this.addTravelTimeDataPoint({ routeId, fromStopId, toStopId }, travelTimeSeconds, travelTimeTimestamp);
|
||||
}
|
||||
|
||||
await this.updateShuttleLastStopArrival(shuttle.id, {
|
||||
stopId: arrivedStop.id,
|
||||
timestamp: new Date(travelTimeTimestamp),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async addTravelTimeDataPoint(
|
||||
{ routeId, fromStopId, toStopId }: ShuttleTravelTimeDataIdentifier,
|
||||
travelTimeSeconds: number,
|
||||
timestamp = Date.now(),
|
||||
): Promise<void> {
|
||||
const key = `${routeId}:${fromStopId}:${toStopId}`;
|
||||
const dataPoints = this.travelTimeData.get(key) || [];
|
||||
dataPoints.push({ timestamp, seconds: travelTimeSeconds });
|
||||
this.travelTimeData.set(key, dataPoints);
|
||||
}
|
||||
|
||||
private async updateShuttleLastStopArrival(shuttleId: string, lastStopArrival: ShuttleStopArrival) {
|
||||
this.shuttleLastStopArrivals.set(shuttleId, lastStopArrival);
|
||||
}
|
||||
|
||||
public async getAverageTravelTimeSeconds(
|
||||
{ routeId, fromStopId, toStopId }: ShuttleTravelTimeDataIdentifier,
|
||||
{ from, to }: ShuttleTravelTimeDateFilterArguments,
|
||||
): Promise<number | undefined> {
|
||||
const key = `${routeId}:${fromStopId}:${toStopId}`;
|
||||
const dataPoints = this.travelTimeData.get(key);
|
||||
|
||||
if (!dataPoints || dataPoints.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const fromTimestamp = from.getTime();
|
||||
const toTimestamp = to.getTime();
|
||||
|
||||
const filteredPoints = dataPoints.filter(
|
||||
(point) => point.timestamp >= fromTimestamp && point.timestamp <= toTimestamp
|
||||
);
|
||||
|
||||
if (filteredPoints.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const sum = filteredPoints.reduce((acc, point) => acc + point.seconds, 0);
|
||||
return sum / filteredPoints.length;
|
||||
}
|
||||
|
||||
public async getArrivedStopIfExists(
|
||||
shuttle: IShuttle,
|
||||
delta = 0.001,
|
||||
): Promise<IStop | undefined> {
|
||||
const orderedStops = await this.getOrderedStopsByRouteId(shuttle.routeId);
|
||||
|
||||
for (const orderedStop of orderedStops) {
|
||||
const stop = await this.getStopById(orderedStop.stopId);
|
||||
if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, delta)) {
|
||||
return stop;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public async getShuttleLastStopArrival(shuttleId: string): Promise<ShuttleStopArrival | undefined> {
|
||||
return this.shuttleLastStopArrivals.get(shuttleId);
|
||||
}
|
||||
|
||||
private async removeEntityByMatcherIfExists<T>(callback: (value: T) => boolean, arrayToSearchIn: T[]) {
|
||||
const index = arrayToSearchIn.findIndex(callback);
|
||||
if (index > -1) {
|
||||
|
||||
Reference in New Issue
Block a user