From: Jérôme Benoit Date: Tue, 8 Aug 2023 23:52:44 +0000 (+0200) Subject: fix: fix performance records duplication X-Git-Tag: v1.2.21~90 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f1c729e06d48e7821b732276343990946fad9f35;p=e-mobility-charging-stations-simulator.git fix: fix performance records duplication Signed-off-by: Jérôme Benoit --- diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index 8bbbd42c..2f6e4987 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -1,6 +1,6 @@ // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import { closeSync, existsSync, mkdirSync, openSync, readFileSync, writeFileSync } from 'node:fs'; +import { closeSync, existsSync, mkdirSync, openSync, writeSync } from 'node:fs'; import { dirname } from 'node:path'; import { Storage } from './Storage'; @@ -16,7 +16,12 @@ import { } from '../../utils'; export class JsonFileStorage extends Storage { - private fd: number | null = null; + private static readonly performanceRecords: Map = new Map< + string, + Statistics + >(); + + private fd?: number; constructor(storageUri: string, logPrefix: string) { super(storageUri, logPrefix); @@ -27,12 +32,13 @@ export class JsonFileStorage extends Storage { this.checkPerformanceRecordsFile(); AsyncLock.acquire(AsyncLockType.performance) .then(() => { - const fileData = readFileSync(this.dbName, 'utf8'); - const performanceRecords: Statistics[] = fileData - ? (JSON.parse(fileData) as Statistics[]) - : []; - performanceRecords.push(performanceStatistics); - writeFileSync(this.dbName, JSONStringifyWithMapSupport(performanceRecords, 2), 'utf8'); + JsonFileStorage.performanceRecords.set(performanceStatistics.id, performanceStatistics); + writeSync( + this.fd!, + JSONStringifyWithMapSupport([...JsonFileStorage.performanceRecords.values()], 2), + 0, + 'utf8', + ); }) .catch((error) => { handleFileException( @@ -53,7 +59,7 @@ export class JsonFileStorage extends Storage { if (!existsSync(dirname(this.dbName))) { mkdirSync(dirname(this.dbName), { recursive: true }); } - this.fd = openSync(this.dbName, 'a+'); + this.fd = openSync(this.dbName, 'w+'); } } catch (error) { handleFileException( @@ -69,7 +75,7 @@ export class JsonFileStorage extends Storage { try { if (this?.fd) { closeSync(this.fd); - this.fd = null; + delete this?.fd; } } catch (error) { handleFileException( diff --git a/src/performance/storage/MikroOrmStorage.ts b/src/performance/storage/MikroOrmStorage.ts index 3c4e7fd3..d1b84b85 100644 --- a/src/performance/storage/MikroOrmStorage.ts +++ b/src/performance/storage/MikroOrmStorage.ts @@ -21,7 +21,7 @@ import { Constants } from '../../utils'; export class MikroOrmStorage extends Storage { private storageType: StorageType; - private orm!: MikroORM | null; + private orm?: MikroORM; constructor(storageUri: string, logPrefix: string, storageType: StorageType) { super(storageUri, logPrefix); @@ -53,7 +53,7 @@ export class MikroOrmStorage extends Storage { try { if (this?.orm) { await this.orm.close(); - this.orm = null; + delete this?.orm; } } catch (error) { this.handleDBError(this.storageType, error as Error); diff --git a/src/performance/storage/MongoDBStorage.ts b/src/performance/storage/MongoDBStorage.ts index 3e1abd2b..568f7095 100644 --- a/src/performance/storage/MongoDBStorage.ts +++ b/src/performance/storage/MongoDBStorage.ts @@ -8,7 +8,7 @@ import { type Statistics, StorageType } from '../../types'; import { Constants } from '../../utils'; export class MongoDBStorage extends Storage { - private readonly client: MongoClient | null; + private readonly client?: MongoClient; private connected: boolean; constructor(storageUri: string, logPrefix: string) { @@ -26,7 +26,7 @@ export class MongoDBStorage extends Storage { await this.client ?.db(this.dbName) .collection(Constants.PERFORMANCE_RECORDS_TABLE) - .insertOne(performanceStatistics); + .replaceOne({ id: performanceStatistics.id }, performanceStatistics, { upsert: true }); } catch (error) { this.handleDBError(StorageType.MONGO_DB, error as Error, Constants.PERFORMANCE_RECORDS_TABLE); }