1 // Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { Connection
, ConnectionOptions
, createConnection
} from
'typeorm';
4 import { PerformanceData
, PerformanceRecords
} from
'../../types/orm/entities/PerformanceRecords';
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';
12 export class TypeORMStorage
extends Storage
{
13 private storageType
: StorageType
;
14 private connection
: Connection
;
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
;
22 public async storePerformanceStatistics(performanceStatistics
: Statistics
): Promise
<void> {
24 const performanceDataArray
: PerformanceData
[] = [];
25 for (const key
of Object.keys(performanceStatistics
.statisticsData
)) {
26 const statisticsData
= performanceStatistics
.statisticsData
[key
];
27 let performanceData
= new PerformanceData();
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
44 await this.connection
.manager
.save(performanceData
);
45 performanceDataArray
.push(performanceData
);
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
55 await this.connection
.manager
.save(performanceRecords
);
57 this.handleDBError(this.storageType
, error
, Constants
.PERFORMANCE_RECORDS_TABLE
);
61 public async open(): Promise
<void> {
63 this.connection
= await createConnection(this.getDBOptions(this.storageType
));
64 await this.connection
.connect();
66 this.handleDBError(this.storageType
, error
);
70 public async close(): Promise
<void> {
72 await this.connection
.close();
74 this.handleDBError(this.storageType
, error
);
78 private getDBOptions(storageType
: StorageType
): ConnectionOptions
{
79 switch (storageType
) {
80 case StorageType
.MYSQL
:
81 case StorageType
.MARIA_DB
:
84 url
: this.storageURI
.toString(),
90 case StorageType
.SQLITE
:
93 database
: path
.join(path
.resolve(__dirname
, '../../../'), this.dbName
),