mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Implement getHistoricalAveragesOfParkingStructureCounts for InMemoryParkingRepository.ts, and add tests
This commit is contained in:
@@ -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
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ export interface ParkingStructureCountOptions {
|
||||
export interface HistoricalParkingAverageQueryResult {
|
||||
fromUnixEpochMs: number;
|
||||
toUnixEpochMs: number;
|
||||
averageSpotsTaken: number;
|
||||
averageSpotsAvailable: number;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user