mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Add previous reference implementation of getAverageTravelTimeSeconds and method bindings
This commit is contained in:
@@ -19,14 +19,52 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple
|
|||||||
private referenceTime: Date | null = null,
|
private referenceTime: Date | null = null,
|
||||||
) {
|
) {
|
||||||
super(redisClient);
|
super(redisClient);
|
||||||
|
|
||||||
|
this.setReferenceTime = this.setReferenceTime.bind(this);
|
||||||
|
this.getAverageTravelTimeSeconds = this.getAverageTravelTimeSeconds.bind(this);
|
||||||
|
this.startListeningForUpdates = this.startListeningForUpdates.bind(this);
|
||||||
|
this.handleShuttleWillArriveAtStop = this.handleShuttleWillArriveAtStop.bind(this);
|
||||||
|
this.handleShuttleUpdate = this.handleShuttleUpdate.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private createHistoricalEtaTimeSeriesKey = (routeId: string, fromStopId: string, toStopId: string) => {
|
||||||
|
return `shuttle:eta:historical:${routeId}:${fromStopId}:${toStopId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
setReferenceTime(referenceTime: Date) {
|
setReferenceTime(referenceTime: Date) {
|
||||||
this.referenceTime = referenceTime;
|
this.referenceTime = referenceTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
getAverageTravelTimeSeconds(identifier: ShuttleTravelTimeDataIdentifier, dateFilter: ShuttleTravelTimeDateFilterArguments): Promise<number | undefined> {
|
public async getAverageTravelTimeSeconds(
|
||||||
throw new Error("Method not implemented.");
|
{ routeId, fromStopId, toStopId }: ShuttleTravelTimeDataIdentifier,
|
||||||
|
{ from, to }: ShuttleTravelTimeDateFilterArguments
|
||||||
|
): Promise<number | undefined> {
|
||||||
|
const timeSeriesKey = this.createHistoricalEtaTimeSeriesKey(routeId, fromStopId, toStopId);
|
||||||
|
const fromTimestamp = from.getTime();
|
||||||
|
const toTimestamp = to.getTime();
|
||||||
|
const intervalMs = toTimestamp - fromTimestamp + 1;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const aggregationResult = await this.redisClient.sendCommand([
|
||||||
|
'TS.RANGE',
|
||||||
|
timeSeriesKey,
|
||||||
|
fromTimestamp.toString(),
|
||||||
|
toTimestamp.toString(),
|
||||||
|
'AGGREGATION',
|
||||||
|
'AVG',
|
||||||
|
intervalMs.toString()
|
||||||
|
]) as [string, string][];
|
||||||
|
|
||||||
|
if (aggregationResult && aggregationResult.length > 0) {
|
||||||
|
const [, averageValue] = aggregationResult[0];
|
||||||
|
return parseFloat(averageValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`Failed to get average travel time: ${error instanceof Error ? error.message : String(error)}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startListeningForUpdates() {
|
startListeningForUpdates() {
|
||||||
|
|||||||
Reference in New Issue
Block a user