Improve OCPP error handling, fix performance storage default file path
[e-mobility-charging-stations-simulator.git] / src / performance / storage / Storage.ts
CommitLineData
c27c3eee
JB
1// Copyright Jerome Benoit. 2021. All Rights Reserved.
2
a6b3c6c3 3import { DBName, StorageType } from '../../types/Storage';
c27c3eee 4
72f041bd
JB
5import Statistics from '../../types/Statistics';
6import { URL } from 'url';
a6b3c6c3
JB
7import Utils from '../../utils/Utils';
8import logger from '../../utils/Logger';
72f041bd
JB
9
10export abstract class Storage {
2a370053
JB
11 protected readonly storageURI: URL;
12 protected readonly logPrefix: string;
13 protected dbName: string;
72f041bd
JB
14
15 constructor(storageURI: string, logPrefix: string) {
16 this.storageURI = new URL(storageURI);
17 this.logPrefix = logPrefix;
18 }
19
c27c3eee 20 protected handleDBError(type: StorageType, error: Error, table?: string): void {
a6b3c6c3 21 logger.error(`${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${error.message}'${(!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`}: %j`, error);
c27c3eee
JB
22 }
23
a6b3c6c3 24 protected getDBNameFromStorageType(type: StorageType): DBName {
c27c3eee
JB
25 switch (type) {
26 case StorageType.MARIA_DB:
a6b3c6c3 27 return DBName.MARIA_DB;
c27c3eee 28 case StorageType.MONGO_DB:
a6b3c6c3 29 return DBName.MONGO_DB;
c27c3eee 30 case StorageType.MYSQL:
a6b3c6c3 31 return DBName.MYSQL;
c27c3eee 32 case StorageType.SQLITE:
a6b3c6c3 33 return DBName.SQLITE;
c27c3eee 34 }
2a370053
JB
35 }
36
37 public abstract open(): void | Promise<void>;
38 public abstract close(): void | Promise<void>;
39 public abstract storePerformanceStatistics(performanceStatistics: Statistics): void | Promise<void>;
72f041bd 40}