fb7062674bb2ea14787a2d5eb0ff6646bab75d11
[e-mobility-charging-stations-simulator.git] / src / performance / storage / JsonFileStorage.ts
1 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
2
3 import fs from 'node:fs';
4
5 import { FileType, type Statistics } from '../../types';
6 import { AsyncLock, AsyncLockType, Constants, FileUtils, Utils } from '../../utils';
7 import { Storage } from '../internal';
8
9 export class JsonFileStorage extends Storage {
10 private fd: number | null = null;
11
12 constructor(storageUri: string, logPrefix: string) {
13 super(storageUri, logPrefix);
14 this.dbName = this.storageUri.pathname;
15 }
16
17 public storePerformanceStatistics(performanceStatistics: Statistics): void {
18 this.checkPerformanceRecordsFile();
19 AsyncLock.acquire(AsyncLockType.performance)
20 .then(() => {
21 const fileData = fs.readFileSync(this.dbName, 'utf8');
22 const performanceRecords: Statistics[] = fileData
23 ? (JSON.parse(fileData) as Statistics[])
24 : [];
25 performanceRecords.push(performanceStatistics);
26 fs.writeFileSync(
27 this.dbName,
28 Utils.JSONStringifyWithMapSupport(performanceRecords, 2),
29 'utf8'
30 );
31 })
32 .catch((error) => {
33 FileUtils.handleFileException(
34 this.dbName,
35 FileType.PerformanceRecords,
36 error as NodeJS.ErrnoException,
37 this.logPrefix
38 );
39 })
40 .finally(() => {
41 AsyncLock.release(AsyncLockType.performance).catch(Constants.EMPTY_FUNCTION);
42 });
43 }
44
45 public open(): void {
46 try {
47 if (Utils.isNullOrUndefined(this?.fd)) {
48 this.fd = fs.openSync(this.dbName, 'a+');
49 }
50 } catch (error) {
51 FileUtils.handleFileException(
52 this.dbName,
53 FileType.PerformanceRecords,
54 error as NodeJS.ErrnoException,
55 this.logPrefix
56 );
57 }
58 }
59
60 public close(): void {
61 try {
62 if (this?.fd) {
63 fs.closeSync(this.fd);
64 this.fd = null;
65 }
66 } catch (error) {
67 FileUtils.handleFileException(
68 this.dbName,
69 FileType.PerformanceRecords,
70 error as NodeJS.ErrnoException,
71 this.logPrefix
72 );
73 }
74 }
75
76 private checkPerformanceRecordsFile(): void {
77 if (!this?.fd) {
78 throw new Error(
79 `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`
80 );
81 }
82 }
83 }