Implement getHistoricalAveragesOfParkingStructureCounts for InMemoryParkingRepository.ts, and add tests

This commit is contained in:
2025-07-02 19:10:16 -04:00
parent 8b5d80a9d6
commit ca2a66509b
3 changed files with 167 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ import {
IParkingStructure,
IParkingStructureTimestampRecord
} from "../entities/ParkingRepositoryEntities";
import { HistoricalParkingAverageQueryResult } from "./ParkingGetterRepository";
import { HistoricalParkingAverageQueryResult, ParkingStructureCountOptions } from "./ParkingGetterRepository";
import { CircularQueue } from "../types/CircularQueue";
export type ParkingStructureID = string;
@@ -74,7 +74,61 @@ export class InMemoryParkingRepository implements ParkingGetterSetterRepository
return null;
};
getHistoricalAveragesOfParkingStructureCounts = async (id: string): Promise<HistoricalParkingAverageQueryResult[]> => {
return [];
getHistoricalAveragesOfParkingStructureCounts = async (id: string, options: ParkingStructureCountOptions): Promise<HistoricalParkingAverageQueryResult[]> => {
const queue = this.historicalData.get(id);
if (!queue || queue.size() === 0) {
return [];
}
const results: HistoricalParkingAverageQueryResult[] = [];
const { startUnixEpochMs, endUnixEpochMs, intervalMs } = options;
let currentIntervalStart = startUnixEpochMs;
while (currentIntervalStart < endUnixEpochMs) {
const currentIntervalEnd = Math.min(currentIntervalStart + intervalMs, endUnixEpochMs);
const recordsInInterval = this.getRecordsInTimeRange(queue, currentIntervalStart, currentIntervalEnd);
if (recordsInInterval.length > 0) {
const averageResult = this.calculateAverageForInterval(currentIntervalStart, currentIntervalEnd, recordsInInterval);
results.push(averageResult);
}
currentIntervalStart = currentIntervalEnd;
}
return results;
};
private getRecordsInTimeRange = (
queue: CircularQueue<IParkingStructureTimestampRecord>,
startMs: number,
endMs: number
): IParkingStructureTimestampRecord[] => {
const recordsInInterval: IParkingStructureTimestampRecord[] = [];
for (let i = 0; i < queue.size(); i++) {
const record = queue.get(i);
if (record && record.timestampMs >= startMs && record.timestampMs < endMs) {
recordsInInterval.push(record);
}
}
return recordsInInterval;
};
private calculateAverageForInterval = (
fromMs: number,
toMs: number,
records: IParkingStructureTimestampRecord[]
): HistoricalParkingAverageQueryResult => {
const totalSpotsAvailable = records.reduce((sum, record) => sum + record.spotsAvailable, 0);
const averageSpotsAvailable = totalSpotsAvailable / records.length;
return {
fromUnixEpochMs: fromMs,
toUnixEpochMs: toMs,
averageSpotsAvailable
};
};
}

View File

@@ -9,7 +9,7 @@ export interface ParkingStructureCountOptions {
export interface HistoricalParkingAverageQueryResult {
fromUnixEpochMs: number;
toUnixEpochMs: number;
averageSpotsTaken: number;
averageSpotsAvailable: number;
}