mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-16 23:40:32 +00:00
Add stopgap ETA of 1 second when the shuttle has arrived at a stop
This commit is contained in:
@@ -90,9 +90,25 @@ export class InMemorySelfUpdatingETARepository extends BaseInMemoryETARepository
|
||||
}
|
||||
|
||||
private async handleShuttleUpdate(shuttle: IShuttle): Promise<void> {
|
||||
const isAtStop = await this.shuttleRepository.checkIfShuttleIsAtStop(shuttle.id);
|
||||
const lastStop = await this.shuttleRepository.getShuttleLastStopArrival(shuttle.id);
|
||||
if (!lastStop) return;
|
||||
|
||||
if (isAtStop) {
|
||||
// Update the ETA *to* the stop the shuttle is currently at,
|
||||
// before starting from the current stop as normal.
|
||||
// Account for cases where the shuttle arrived way earlier than
|
||||
// expected based on the calculated ETA.
|
||||
|
||||
await this.addOrUpdateEta({
|
||||
secondsRemaining: 1,
|
||||
shuttleId: shuttle.id,
|
||||
stopId: lastStop.stopId,
|
||||
systemId: shuttle.systemId,
|
||||
updatedTime: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
const lastOrderedStop = await this.shuttleRepository.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId);
|
||||
|
||||
await this.updateCascadingEta({
|
||||
|
||||
@@ -102,9 +102,25 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple
|
||||
}
|
||||
|
||||
private async handleShuttleUpdate(shuttle: IShuttle) {
|
||||
const isAtStop = await this.shuttleRepository.checkIfShuttleIsAtStop(shuttle.id);
|
||||
const lastStop = await this.shuttleRepository.getShuttleLastStopArrival(shuttle.id);
|
||||
if (!lastStop) return;
|
||||
|
||||
if (isAtStop) {
|
||||
// Update the ETA *to* the stop the shuttle is currently at,
|
||||
// before starting from the current stop as normal.
|
||||
// Account for cases where the shuttle arrived way earlier than
|
||||
// expected based on the calculated ETA.
|
||||
|
||||
await this.addOrUpdateEta({
|
||||
secondsRemaining: 1,
|
||||
shuttleId: shuttle.id,
|
||||
stopId: lastStop.stopId,
|
||||
systemId: shuttle.systemId,
|
||||
updatedTime: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
const lastOrderedStop = await this.shuttleRepository.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId);
|
||||
|
||||
await this.updateCascadingEta({
|
||||
|
||||
@@ -194,6 +194,41 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
||||
currentTime, shuttleSecondArrivalTimeAtFirstStop
|
||||
);
|
||||
});
|
||||
|
||||
test("adds a 'stopgap' entry of 1 second when the shuttle arrives at a stop", async () => {
|
||||
const { stop1, stop2, stop3, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
|
||||
|
||||
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 1, 12, 5, 0);
|
||||
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
|
||||
|
||||
// Populating travel time data
|
||||
await populateTravelTimeDataForStops({ currentTime, shuttle, stop1, stop2, stop3 });
|
||||
|
||||
// Populate ETA data
|
||||
// Simulate shuttle running early for second stop
|
||||
shuttle.coordinates = stop1.coordinates;
|
||||
await shuttleRepository.addOrUpdateShuttle(
|
||||
shuttle,
|
||||
shuttleSecondArrivalTimeAtFirstStop.getTime()
|
||||
);
|
||||
|
||||
shuttle.coordinates = stop2.coordinates;
|
||||
// Call twice to get the ETA repository to read the correct flag
|
||||
await shuttleRepository.addOrUpdateShuttle(
|
||||
shuttle,
|
||||
currentTime.getTime(),
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
await shuttleRepository.addOrUpdateShuttle(
|
||||
shuttle,
|
||||
currentTime.getTime(), // ~8 minutes early
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id);
|
||||
expect(eta?.secondsRemaining).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleShuttleWillLeaveStop", () => {
|
||||
|
||||
Reference in New Issue
Block a user