Update InMemoryParkingRepository.ts and tests to use the circular queue

This commit is contained in:
2025-07-02 18:48:33 -04:00
parent 220b402b3b
commit 2b04ca01a9
2 changed files with 13 additions and 10 deletions

View File

@@ -4,8 +4,9 @@ import {
IParkingStructureTimestampRecord
} from "../entities/ParkingRepositoryEntities";
import { HistoricalParkingAverageQueryResult } from "./ParkingGetterRepository";
import { CircularQueue } from "../types/CircularQueue";
type ParkingStructureID = string;
export type ParkingStructureID = string;
// Every 10 minutes
// 6x per hour * 24x per day * 7x per week = 1008 entries for one week
@@ -19,7 +20,7 @@ export class InMemoryParkingRepository implements ParkingGetterSetterRepository
constructor(
private structures: Map<ParkingStructureID, IParkingStructure> = new Map(),
private historicalData: Map<ParkingStructureID, IParkingStructureTimestampRecord[]> = new Map(),
private historicalData: Map<ParkingStructureID, CircularQueue<IParkingStructureTimestampRecord>> = new Map(),
) {
}
@@ -44,10 +45,11 @@ export class InMemoryParkingRepository implements ParkingGetterSetterRepository
};
if (!this.historicalData.has(structure.id)) {
this.historicalData.set(structure.id, []);
this.historicalData.set(structure.id, new CircularQueue<IParkingStructureTimestampRecord>(MAX_NUM_ENTRIES));
}
this.historicalData.get(structure.id)?.push(timestampRecord);
const sortingCallback = (a: IParkingStructureTimestampRecord, b: IParkingStructureTimestampRecord) => a.timestampMs - b.timestampMs;
this.historicalData.get(structure.id)?.appendWithSorting(timestampRecord, sortingCallback);
this.dataLastAdded.set(structure.id, new Date(now));
}
}

View File

@@ -1,9 +1,10 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import {
InMemoryParkingRepository,
PARKING_LOGGING_INTERVAL_MS
PARKING_LOGGING_INTERVAL_MS, ParkingStructureID
} from "../../src/repositories/InMemoryParkingRepository";
import { IParkingStructure } from "../../src/entities/ParkingRepositoryEntities";
import { IParkingStructure, IParkingStructureTimestampRecord } from "../../src/entities/ParkingRepositoryEntities";
import { CircularQueue } from "../../src/types/CircularQueue";
describe("InMemoryParkingRepository", () => {
let repository: InMemoryParkingRepository;
@@ -19,7 +20,7 @@ describe("InMemoryParkingRepository", () => {
address: "300 E Walnut Ave, Orange, CA 92867",
updatedTime: new Date(),
};
let historicalData = new Map();
let historicalData: Map<ParkingStructureID, CircularQueue<IParkingStructureTimestampRecord>> = new Map();
beforeEach(() => {
historicalData = new Map();
@@ -57,8 +58,8 @@ describe("InMemoryParkingRepository", () => {
jest.setSystemTime(now + PARKING_LOGGING_INTERVAL_MS + 60);
await repository.addOrUpdateParkingStructure(testStructure);
expect(historicalData.get(testStructure.id)).toContainEqual(expectedTimestampRecordMatcher);
expect(historicalData.get(testStructure.id)).toContainEqual({
expect(historicalData.get(testStructure.id)?.get(0)).toEqual(expectedTimestampRecordMatcher);
expect(historicalData.get(testStructure.id)?.get(1)).toEqual({
...expectedTimestampRecordMatcher,
timestampMs: now + PARKING_LOGGING_INTERVAL_MS + 60,
});
@@ -74,7 +75,7 @@ describe("InMemoryParkingRepository", () => {
jest.setSystemTime(now + 60);
await repository.addOrUpdateParkingStructure(testStructure);
expect(historicalData.get(testStructure.id)).toHaveLength(1);
expect(historicalData.get(testStructure.id)?.size()).toEqual(1);
});
});