feat: improve get composite schedule
[e-mobility-charging-stations-simulator.git] / src / performance / storage / JsonFileStorage.ts
index e1a9a44ed796e66e5639c45660850cb7b54fdbf9..8bbbd42cea06563d91d7e6d360bf38cc06f92563 100644 (file)
@@ -6,7 +6,14 @@ import { dirname } from 'node:path';
 import { Storage } from './Storage';
 import { BaseError } from '../../exception';
 import { FileType, type Statistics } from '../../types';
-import { AsyncLock, AsyncLockType, Constants, Utils, handleFileException } from '../../utils';
+import {
+  AsyncLock,
+  AsyncLockType,
+  Constants,
+  JSONStringifyWithMapSupport,
+  handleFileException,
+  isNullOrUndefined,
+} from '../../utils';
 
 export class JsonFileStorage extends Storage {
   private fd: number | null = null;
@@ -25,18 +32,14 @@ export class JsonFileStorage extends Storage {
           ? (JSON.parse(fileData) as Statistics[])
           : [];
         performanceRecords.push(performanceStatistics);
-        writeFileSync(
-          this.dbName,
-          Utils.JSONStringifyWithMapSupport(performanceRecords, 2),
-          'utf8'
-        );
+        writeFileSync(this.dbName, JSONStringifyWithMapSupport(performanceRecords, 2), 'utf8');
       })
       .catch((error) => {
         handleFileException(
           this.dbName,
           FileType.PerformanceRecords,
           error as NodeJS.ErrnoException,
-          this.logPrefix
+          this.logPrefix,
         );
       })
       .finally(() => {
@@ -46,7 +49,7 @@ export class JsonFileStorage extends Storage {
 
   public open(): void {
     try {
-      if (Utils.isNullOrUndefined(this?.fd)) {
+      if (isNullOrUndefined(this?.fd)) {
         if (!existsSync(dirname(this.dbName))) {
           mkdirSync(dirname(this.dbName), { recursive: true });
         }
@@ -57,7 +60,7 @@ export class JsonFileStorage extends Storage {
         this.dbName,
         FileType.PerformanceRecords,
         error as NodeJS.ErrnoException,
-        this.logPrefix
+        this.logPrefix,
       );
     }
   }
@@ -73,7 +76,7 @@ export class JsonFileStorage extends Storage {
         this.dbName,
         FileType.PerformanceRecords,
         error as NodeJS.ErrnoException,
-        this.logPrefix
+        this.logPrefix,
       );
     }
   }
@@ -81,7 +84,7 @@ export class JsonFileStorage extends Storage {
   private checkPerformanceRecordsFile(): void {
     if (!this?.fd) {
       throw new BaseError(
-        `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`
+        `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`,
       );
     }
   }