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