mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-19 08:50:29 +00:00
Update InMemoryParkingRepository.ts and tests to use the circular queue
This commit is contained in:
@@ -4,8 +4,9 @@ import {
|
|||||||
IParkingStructureTimestampRecord
|
IParkingStructureTimestampRecord
|
||||||
} from "../entities/ParkingRepositoryEntities";
|
} from "../entities/ParkingRepositoryEntities";
|
||||||
import { HistoricalParkingAverageQueryResult } from "./ParkingGetterRepository";
|
import { HistoricalParkingAverageQueryResult } from "./ParkingGetterRepository";
|
||||||
|
import { CircularQueue } from "../types/CircularQueue";
|
||||||
|
|
||||||
type ParkingStructureID = string;
|
export type ParkingStructureID = string;
|
||||||
|
|
||||||
// Every 10 minutes
|
// Every 10 minutes
|
||||||
// 6x per hour * 24x per day * 7x per week = 1008 entries for one week
|
// 6x per hour * 24x per day * 7x per week = 1008 entries for one week
|
||||||
@@ -19,7 +20,7 @@ export class InMemoryParkingRepository implements ParkingGetterSetterRepository
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private structures: Map<ParkingStructureID, IParkingStructure> = new Map(),
|
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)) {
|
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));
|
this.dataLastAdded.set(structure.id, new Date(now));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
||||||
import {
|
import {
|
||||||
InMemoryParkingRepository,
|
InMemoryParkingRepository,
|
||||||
PARKING_LOGGING_INTERVAL_MS
|
PARKING_LOGGING_INTERVAL_MS, ParkingStructureID
|
||||||
} from "../../src/repositories/InMemoryParkingRepository";
|
} 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", () => {
|
describe("InMemoryParkingRepository", () => {
|
||||||
let repository: InMemoryParkingRepository;
|
let repository: InMemoryParkingRepository;
|
||||||
@@ -19,7 +20,7 @@ describe("InMemoryParkingRepository", () => {
|
|||||||
address: "300 E Walnut Ave, Orange, CA 92867",
|
address: "300 E Walnut Ave, Orange, CA 92867",
|
||||||
updatedTime: new Date(),
|
updatedTime: new Date(),
|
||||||
};
|
};
|
||||||
let historicalData = new Map();
|
let historicalData: Map<ParkingStructureID, CircularQueue<IParkingStructureTimestampRecord>> = new Map();
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
historicalData = new Map();
|
historicalData = new Map();
|
||||||
@@ -57,8 +58,8 @@ describe("InMemoryParkingRepository", () => {
|
|||||||
jest.setSystemTime(now + PARKING_LOGGING_INTERVAL_MS + 60);
|
jest.setSystemTime(now + PARKING_LOGGING_INTERVAL_MS + 60);
|
||||||
await repository.addOrUpdateParkingStructure(testStructure);
|
await repository.addOrUpdateParkingStructure(testStructure);
|
||||||
|
|
||||||
expect(historicalData.get(testStructure.id)).toContainEqual(expectedTimestampRecordMatcher);
|
expect(historicalData.get(testStructure.id)?.get(0)).toEqual(expectedTimestampRecordMatcher);
|
||||||
expect(historicalData.get(testStructure.id)).toContainEqual({
|
expect(historicalData.get(testStructure.id)?.get(1)).toEqual({
|
||||||
...expectedTimestampRecordMatcher,
|
...expectedTimestampRecordMatcher,
|
||||||
timestampMs: now + PARKING_LOGGING_INTERVAL_MS + 60,
|
timestampMs: now + PARKING_LOGGING_INTERVAL_MS + 60,
|
||||||
});
|
});
|
||||||
@@ -74,7 +75,7 @@ describe("InMemoryParkingRepository", () => {
|
|||||||
jest.setSystemTime(now + 60);
|
jest.setSystemTime(now + 60);
|
||||||
await repository.addOrUpdateParkingStructure(testStructure);
|
await repository.addOrUpdateParkingStructure(testStructure);
|
||||||
|
|
||||||
expect(historicalData.get(testStructure.id)).toHaveLength(1);
|
expect(historicalData.get(testStructure.id)?.size()).toEqual(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user