diff --git a/src/repositories/InMemoryParkingRepository.ts b/src/repositories/InMemoryParkingRepository.ts index 96488b8..f5c63df 100644 --- a/src/repositories/InMemoryParkingRepository.ts +++ b/src/repositories/InMemoryParkingRepository.ts @@ -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 = new Map(), - private historicalData: Map = new Map(), + private historicalData: Map> = 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(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)); } } diff --git a/test/repositories/InMemoryParkingRepositoryTests.test.ts b/test/repositories/InMemoryParkingRepositoryTests.test.ts index b3de36d..24c292f 100644 --- a/test/repositories/InMemoryParkingRepositoryTests.test.ts +++ b/test/repositories/InMemoryParkingRepositoryTests.test.ts @@ -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> = 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); }); });