mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Add partial implementation and scaffolding of historical ETA update
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import EventEmitter from "node:events";
|
||||
import { createClient } from 'redis';
|
||||
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 {
|
||||
ShuttleRepositoryEvent,
|
||||
@@ -10,6 +10,11 @@ import {
|
||||
ShuttleRepositoryEventPayloads,
|
||||
} from "./ShuttleGetterRepository";
|
||||
|
||||
export interface ShuttleStopArrival {
|
||||
stopId: string;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export class RedisShuttleRepository extends EventEmitter implements ShuttleGetterSetterRepository {
|
||||
protected redisClient;
|
||||
|
||||
@@ -405,9 +410,48 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
|
||||
public async addOrUpdateShuttle(shuttle: IShuttle): Promise<void> {
|
||||
const key = this.createShuttleKey(shuttle.id);
|
||||
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);
|
||||
await this.redisClient.hSet(key, this.createRedisHashFromStop(stop));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user