feat: add performance statistics to UI protocol
[e-mobility-charging-stations-simulator.git] / src / performance / storage / MikroOrmStorage.ts
CommitLineData
a19b897d 1// Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
a6b3c6c3 2
4ccf551d
JB
3import { MikroORM as MariaDbORM, type Options as MariaDbOptions } from '@mikro-orm/mariadb'
4import { MikroORM as SqliteORM, type Options as SqliteOptions } from '@mikro-orm/sqlite'
a6b3c6c3 5
66a7748d 6import { Storage } from './Storage.js'
789007bd 7import { type PerformanceRecord, type Statistics, StorageType } from '../../types/index.js'
66a7748d 8import { Constants } from '../../utils/index.js'
a6b3c6c3 9
100a5301 10export class MikroOrmStorage extends Storage {
66a7748d 11 private readonly storageType: StorageType
4ccf551d 12 private orm?: SqliteORM | MariaDbORM
a6b3c6c3 13
66a7748d
JB
14 constructor (storageUri: string, logPrefix: string, storageType: StorageType) {
15 super(storageUri, logPrefix)
16 this.storageType = storageType
17 this.dbName = this.getDBName()
a6b3c6c3
JB
18 }
19
66a7748d 20 public async storePerformanceStatistics (performanceStatistics: Statistics): Promise<void> {
a6b3c6c3 21 try {
a66bbcfe 22 this.setPerformanceStatistics(performanceStatistics)
70b73ed6 23 await this.orm?.em.upsert({
789007bd
JB
24 ...performanceStatistics,
25 statisticsData: Array.from(performanceStatistics.statisticsData, ([name, value]) => ({
26 name,
27 ...value
28 }))
29 } satisfies PerformanceRecord)
a6b3c6c3 30 } catch (error) {
66a7748d 31 this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE)
a6b3c6c3
JB
32 }
33 }
34
66a7748d 35 public async open (): Promise<void> {
a6ceb16a 36 try {
5199f9fd 37 if (this.orm == null) {
4ccf551d
JB
38 switch (this.storageType) {
39 case StorageType.SQLITE:
40 this.orm = await SqliteORM.init(this.getOptions() as SqliteOptions)
41 break
42 case StorageType.MARIA_DB:
43 case StorageType.MYSQL:
44 this.orm = await MariaDbORM.init(this.getOptions() as MariaDbOptions)
45 break
46 }
a6ceb16a
JB
47 }
48 } catch (error) {
66a7748d 49 this.handleDBError(this.storageType, error as Error)
a6ceb16a 50 }
a6b3c6c3
JB
51 }
52
66a7748d 53 public async close (): Promise<void> {
a66bbcfe 54 this.clearPerformanceStatistics()
a6ceb16a 55 try {
5199f9fd 56 if (this.orm != null) {
66a7748d 57 await this.orm.close()
5199f9fd 58 delete this.orm
a6ceb16a
JB
59 }
60 } catch (error) {
66a7748d 61 this.handleDBError(this.storageType, error as Error)
a6ceb16a 62 }
a6b3c6c3
JB
63 }
64
66a7748d 65 private getDBName (): string {
d5603918 66 if (this.storageType === StorageType.SQLITE) {
789007bd 67 return `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`
d5603918 68 }
5199f9fd 69 return this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '')
d5603918
JB
70 }
71
4ccf551d 72 private getOptions (): SqliteOptions | MariaDbOptions {
a6b3c6c3 73 return {
4ccf551d
JB
74 dbName: this.dbName,
75 entities: ['./dist/types/orm/entities/*.js'],
76 entitiesTs: ['./src/types/orm/entities/*.ts'],
66a7748d
JB
77 clientUrl: this.getClientUrl()
78 }
a6b3c6c3 79 }
d5603918 80
66a7748d 81 private getClientUrl (): string | undefined {
d5603918
JB
82 switch (this.storageType) {
83 case StorageType.SQLITE:
d5603918
JB
84 case StorageType.MARIA_DB:
85 case StorageType.MYSQL:
66a7748d 86 return this.storageUri.toString()
d5603918
JB
87 }
88 }
a6b3c6c3 89}