Add support for more DB engines via TypeORM
[e-mobility-charging-stations-simulator.git] / src / utils / performance-storage / TypeORMStorage.ts
1 // Copyright Jerome Benoit. 2021. All Rights Reserved.
2
3 import { Connection, ConnectionOptions, createConnection } from 'typeorm';
4 import { PerformanceData, PerformanceRecords } from '../../types/orm/entities/PerformanceRecords';
5
6 import Constants from '../Constants';
7 import Statistics from '../../types/Statistics';
8 import { Storage } from './Storage';
9 import { StorageType } from '../../types/Storage';
10 import path from 'path';
11
12 export class TypeORMStorage extends Storage {
13 private storageType: StorageType;
14 private connection: Connection;
15
16 constructor(storageURI: string, logPrefix: string, storageType: StorageType) {
17 super(storageURI, logPrefix);
18 this.storageType = storageType;
19 this.dbName = this.storageURI.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ?? Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME;
20 }
21
22 public async storePerformanceStatistics(performanceStatistics: Statistics): Promise<void> {
23 try {
24 const performanceDataArray: PerformanceData[] = [];
25 for (const key of Object.keys(performanceStatistics.statisticsData)) {
26 const statisticsData = performanceStatistics.statisticsData[key];
27 let performanceData = new PerformanceData();
28 performanceData = {
29 commandName: key,
30 countRequest: statisticsData.countRequest,
31 countResponse: statisticsData.countResponse,
32 countError: statisticsData.countError,
33 countTimeMeasurement: statisticsData.countTimeMeasurement,
34 timeMeasurementSeries: statisticsData.timeMeasurementSeries,
35 currentTimeMeasurement: statisticsData.currentTimeMeasurement,
36 minTimeMeasurement: statisticsData.minTimeMeasurement,
37 maxTimeMeasurement: statisticsData.maxTimeMeasurement,
38 totalTimeMeasurement: statisticsData.totalTimeMeasurement,
39 avgTimeMeasurement: statisticsData.avgTimeMeasurement,
40 medTimeMeasurement: statisticsData.medTimeMeasurement,
41 ninetyFiveThPercentileTimeMeasurement: statisticsData.ninetyFiveThPercentileTimeMeasurement,
42 stdDevTimeMeasurement: statisticsData.stdDevTimeMeasurement
43 };
44 await this.connection.manager.save(performanceData);
45 performanceDataArray.push(performanceData);
46 }
47 let performanceRecords = new PerformanceRecords();
48 performanceRecords = {
49 id: performanceStatistics.id,
50 URI: performanceStatistics.URI,
51 createdAt: performanceStatistics.createdAt,
52 lastUpdatedAt: performanceStatistics.lastUpdatedAt,
53 performanceData: performanceDataArray
54 };
55 await this.connection.manager.save(performanceRecords);
56 } catch (error) {
57 this.handleDBError(this.storageType, error, Constants.PERFORMANCE_RECORDS_TABLE);
58 }
59 }
60
61 public async open(): Promise<void> {
62 try {
63 this.connection = await createConnection(this.getDBOptions(this.storageType));
64 await this.connection.connect();
65 } catch (error) {
66 this.handleDBError(this.storageType, error);
67 }
68 }
69
70 public async close(): Promise<void> {
71 try {
72 await this.connection.close();
73 } catch (error) {
74 this.handleDBError(this.storageType, error);
75 }
76 }
77
78 private getDBOptions(storageType: StorageType): ConnectionOptions {
79 switch (storageType) {
80 case StorageType.MYSQL:
81 case StorageType.MARIA_DB:
82 return {
83 type: 'mysql',
84 url: this.storageURI.toString(),
85 entities: [
86 PerformanceRecords,
87 PerformanceData
88 ],
89 };
90 case StorageType.SQLITE:
91 return {
92 type: 'sqlite',
93 database: path.join(path.resolve(__dirname, '../../../'), this.dbName),
94 entities: [
95 PerformanceRecords,
96 PerformanceData
97 ],
98 };
99 }
100 }
101 }
102