refactor(simulator): switch to named exports
[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 lockfile from 'proper-lockfile';
6
7 import { Storage } from './Storage';
8 import { FileType, type Statistics } from '../../types';
9 import { FileUtils } from '../../utils/FileUtils';
10 import { Utils } from '../../utils/Utils';
11
12 export class JsonFileStorage extends Storage {
13 private fd: number | null = null;
14
15 constructor(storageUri: string, logPrefix: string) {
16 super(storageUri, logPrefix);
17 this.dbName = this.storageUri.pathname;
18 }
19
20 public storePerformanceStatistics(performanceStatistics: Statistics): void {
21 this.checkPerformanceRecordsFile();
22 lockfile
23 .lock(this.dbName, { stale: 5000, retries: 3 })
24 .then(async (release) => {
25 try {
26 const fileData = fs.readFileSync(this.dbName, 'utf8');
27 const performanceRecords: Statistics[] = fileData
28 ? (JSON.parse(fileData) as Statistics[])
29 : [];
30 performanceRecords.push(performanceStatistics);
31 fs.writeFileSync(
32 this.dbName,
33 Utils.JSONStringifyWithMapSupport(performanceRecords, 2),
34 'utf8'
35 );
36 } catch (error) {
37 FileUtils.handleFileException(
38 this.dbName,
39 FileType.PerformanceRecords,
40 error as NodeJS.ErrnoException,
41 this.logPrefix
42 );
43 }
44 await release();
45 })
46 .catch(() => {
47 /* This is intentional */
48 });
49 }
50
51 public open(): void {
52 try {
53 if (this?.fd === undefined || this?.fd === null) {
54 this.fd = fs.openSync(this.dbName, 'a+');
55 }
56 } catch (error) {
57 FileUtils.handleFileException(
58 this.dbName,
59 FileType.PerformanceRecords,
60 error as NodeJS.ErrnoException,
61 this.logPrefix
62 );
63 }
64 }
65
66 public close(): void {
67 try {
68 if (this?.fd) {
69 fs.closeSync(this.fd);
70 this.fd = null;
71 }
72 } catch (error) {
73 FileUtils.handleFileException(
74 this.dbName,
75 FileType.PerformanceRecords,
76 error as NodeJS.ErrnoException,
77 this.logPrefix
78 );
79 }
80 }
81
82 private checkPerformanceRecordsFile(): void {
83 if (!this?.fd) {
84 throw new Error(
85 `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`
86 );
87 }
88 }
89 }