Throw an error in the template does not have default required mesurand
[e-mobility-charging-stations-simulator.git] / src / performance / storage / Storage.ts
1 // Copyright Jerome Benoit. 2021. All Rights Reserved.
2
3 import { DBName, StorageType } from '../../types/Storage';
4
5 import Statistics from '../../types/Statistics';
6 import { URL } from 'url';
7 import Utils from '../../utils/Utils';
8 import logger from '../../utils/Logger';
9
10 export abstract class Storage {
11 protected readonly storageURI: URL;
12 protected readonly logPrefix: string;
13 protected dbName: string;
14
15 constructor(storageURI: string, logPrefix: string) {
16 this.storageURI = new URL(storageURI);
17 this.logPrefix = logPrefix;
18 }
19
20 protected handleDBError(type: StorageType, error: Error, table?: string): void {
21 logger.error(`${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${error.message}'${(!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`}: %j`, error);
22 }
23
24 protected getDBNameFromStorageType(type: StorageType): DBName {
25 switch (type) {
26 case StorageType.MARIA_DB:
27 return DBName.MARIA_DB;
28 case StorageType.MONGO_DB:
29 return DBName.MONGO_DB;
30 case StorageType.MYSQL:
31 return DBName.MYSQL;
32 case StorageType.SQLITE:
33 return DBName.SQLITE;
34 }
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>;
40 }