Add partial implementation and scaffolding of historical ETA update

This commit is contained in:
2025-11-10 14:49:41 -08:00
parent b535868897
commit 321631e6c9

View File

@@ -1,7 +1,7 @@
import EventEmitter from "node:events"; import EventEmitter from "node:events";
import { createClient } from 'redis'; import { createClient } from 'redis';
import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository"; import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository";
import { IEta, IOrderedStop, IRoute, IShuttle, IStop } from "../../entities/ShuttleRepositoryEntities"; import { IEta, IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities";
import { REDIS_RECONNECT_INTERVAL } from "../../environment"; import { REDIS_RECONNECT_INTERVAL } from "../../environment";
import { import {
ShuttleRepositoryEvent, ShuttleRepositoryEvent,
@@ -10,6 +10,11 @@ import {
ShuttleRepositoryEventPayloads, ShuttleRepositoryEventPayloads,
} from "./ShuttleGetterRepository"; } from "./ShuttleGetterRepository";
export interface ShuttleStopArrival {
stopId: string;
timestamp: Date;
}
export class RedisShuttleRepository extends EventEmitter implements ShuttleGetterSetterRepository { export class RedisShuttleRepository extends EventEmitter implements ShuttleGetterSetterRepository {
protected redisClient; protected redisClient;
@@ -405,9 +410,48 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
public async addOrUpdateShuttle(shuttle: IShuttle): Promise<void> { public async addOrUpdateShuttle(shuttle: IShuttle): Promise<void> {
const key = this.createShuttleKey(shuttle.id); const key = this.createShuttleKey(shuttle.id);
await this.redisClient.hSet(key, this.createRedisHashFromShuttle(shuttle)); await this.redisClient.hSet(key, this.createRedisHashFromShuttle(shuttle));
await this.updateHistoricalEtasForShuttle(shuttle);
} }
public async addOrUpdateStop(stop: IStop): Promise<void> { private async updateHistoricalEtasForShuttle(shuttle: IShuttle) {
const arrivedStop = await this.getArrivedStopIfExists(shuttle);
if (arrivedStop != undefined) {
const lastStopTimestamp = await this.getShuttleLastStopTimestamp(shuttle)
if (lastStopTimestamp != undefined) {
const now = Date();
const routeId = shuttle.routeId
const fromStopId = lastStopTimestamp.stopId;
const toStopId = arrivedStop.id;
// Create an entry in Redis time series
// Key: routeId:fromStopId:toStopId:
// Value: seconds it took to get from lastStopTimestamp.stopId to arrivedStop.id
}
// TODO: Update the "last stop timestamp"
}
}
public async getArrivedStopIfExists(shuttle: IShuttle): 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)) {
return stop;
}
return undefined;
}
}
public async getShuttleLastStopTimestamp(shuttle: IShuttle): Promise<ShuttleStopArrival> {
throw new Error("Method not implemented.");
}
public async addOrUpdateStop(stop: IStop): Promise<void> {
const key = this.createStopKey(stop.id); const key = this.createStopKey(stop.id);
await this.redisClient.hSet(key, this.createRedisHashFromStop(stop)); await this.redisClient.hSet(key, this.createRedisHashFromStop(stop));
} }