X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fperformance%2Fstorage%2FMikroOrmStorage.ts;h=7b4545bdff36ba824597c22549889641c6f60538;hb=8d2a9e1c5a2b21932c77846bdf10cbe726797685;hp=d5e25bb2c6e7e205a98963bd3bd97195d7dc73cb;hpb=4ccf551d961ef001acef6fafe1165ad9b6dac4e3;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/storage/MikroOrmStorage.ts b/src/performance/storage/MikroOrmStorage.ts index d5e25bb2..7b4545bd 100644 --- a/src/performance/storage/MikroOrmStorage.ts +++ b/src/performance/storage/MikroOrmStorage.ts @@ -3,9 +3,9 @@ import { MikroORM as MariaDbORM, type Options as MariaDbOptions } from '@mikro-orm/mariadb' import { MikroORM as SqliteORM, type Options as SqliteOptions } from '@mikro-orm/sqlite' -import { Storage } from './Storage.js' -import { type Statistics, StorageType } from '../../types/index.js' +import { type PerformanceRecord, type Statistics, StorageType } from '../../types/index.js' import { Constants } from '../../utils/index.js' +import { Storage } from './Storage.js' export class MikroOrmStorage extends Storage { private readonly storageType: StorageType @@ -19,8 +19,14 @@ export class MikroOrmStorage extends Storage { public async storePerformanceStatistics (performanceStatistics: Statistics): Promise { try { - // TODO: build PerformanceRecord entity - await this.orm?.em.persistAndFlush({}) + this.setPerformanceStatistics(performanceStatistics) + await this.orm?.em.upsert({ + ...performanceStatistics, + statisticsData: Array.from(performanceStatistics.statisticsData, ([name, value]) => ({ + name, + ...value + })) + } satisfies PerformanceRecord) } catch (error) { this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE) } @@ -45,6 +51,7 @@ export class MikroOrmStorage extends Storage { } public async close (): Promise { + this.clearPerformanceStatistics() try { if (this.orm != null) { await this.orm.close() @@ -57,7 +64,7 @@ export class MikroOrmStorage extends Storage { private getDBName (): string { if (this.storageType === StorageType.SQLITE) { - return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db` + return `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db` } return this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') }