chore: version 1.1.94
[e-mobility-charging-stations-simulator.git] / src / performance / PerformanceStatistics.ts
index f3c4e5e612a6707bf8288adfd071e291b1cd64b0..aa0cc72325d903bf2de6ff0a8a44c8b959207f53 100644 (file)
@@ -1,15 +1,16 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
+import type { URL } from 'node:url';
 import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks';
-import type { URL } from 'url';
 import { parentPort } from 'worker_threads';
 
 import { MessageChannelUtils } from '../charging-station/MessageChannelUtils';
 import { MessageType } from '../types/ocpp/MessageType';
 import type { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests';
 import type { Statistics, TimeSeries } from '../types/Statistics';
-import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from '../utils/CircularArray';
+import { CircularArray } from '../utils/CircularArray';
 import Configuration from '../utils/Configuration';
+import Constants from '../utils/Constants';
 import logger from '../utils/Logger';
 import Utils from '../utils/Utils';
 
@@ -21,9 +22,9 @@ export default class PerformanceStatistics {
 
   private readonly objId: string;
   private readonly objName: string;
-  private performanceObserver: PerformanceObserver;
+  private performanceObserver!: PerformanceObserver;
   private readonly statistics: Statistics;
-  private displayInterval: NodeJS.Timeout;
+  private displayInterval!: NodeJS.Timeout;
 
   private constructor(objId: string, objName: string, uri: URL) {
     this.objId = objId;
@@ -50,7 +51,7 @@ export default class PerformanceStatistics {
   }
 
   public static beginMeasure(id: string): string {
-    const markId = `${id.charAt(0).toUpperCase() + id.slice(1)}~${Utils.generateUUID()}`;
+    const markId = `${id.charAt(0).toUpperCase()}${id.slice(1)}~${Utils.generateUUID()}`;
     performance.mark(markId);
     return markId;
   }
@@ -162,16 +163,13 @@ export default class PerformanceStatistics {
         this.logStatistics();
       }, Configuration.getLogStatisticsInterval() * 1000);
       logger.info(
-        this.logPrefix() +
-          ' logged every ' +
-          Utils.formatDurationSeconds(Configuration.getLogStatisticsInterval())
+        `${this.logPrefix()} logged every ${Utils.formatDurationSeconds(
+          Configuration.getLogStatisticsInterval()
+        )}`
       );
     } else {
       logger.info(
-        this.logPrefix() +
-          ' log interval is set to ' +
-          Configuration.getLogStatisticsInterval().toString() +
-          '. Not logging statistics'
+        `${this.logPrefix()} log interval is set to ${Configuration.getLogStatisticsInterval()?.toString()}. Not logging statistics`
       );
     }
   }
@@ -256,12 +254,12 @@ export default class PerformanceStatistics {
     this.statistics.statisticsData.get(entryName).avgTimeMeasurement =
       this.statistics.statisticsData.get(entryName).totalTimeMeasurement /
       this.statistics.statisticsData.get(entryName).countTimeMeasurement;
-    Array.isArray(this.statistics.statisticsData.get(entryName).timeMeasurementSeries) === true
+    this.statistics.statisticsData.get(entryName)?.timeMeasurementSeries instanceof CircularArray
       ? this.statistics.statisticsData
           .get(entryName)
-          .timeMeasurementSeries.push({ timestamp: entry.startTime, value: entry.duration })
+          ?.timeMeasurementSeries?.push({ timestamp: entry.startTime, value: entry.duration })
       : (this.statistics.statisticsData.get(entryName).timeMeasurementSeries =
-          new CircularArray<TimeSeries>(DEFAULT_CIRCULAR_ARRAY_SIZE, {
+          new CircularArray<TimeSeries>(Constants.DEFAULT_CIRCULAR_BUFFER_CAPACITY, {
             timestamp: entry.startTime,
             value: entry.duration,
           }));
@@ -283,7 +281,7 @@ export default class PerformanceStatistics {
       )
     );
     if (Configuration.getPerformanceStorage().enabled) {
-      parentPort.postMessage(
+      parentPort?.postMessage(
         MessageChannelUtils.buildPerformanceStatisticsMessage(this.statistics)
       );
     }
@@ -293,7 +291,7 @@ export default class PerformanceStatistics {
     return timeSeries.map((timeSeriesItem) => timeSeriesItem.value);
   }
 
-  private logPrefix(): string {
+  private logPrefix = (): string => {
     return Utils.logPrefix(` ${this.objName} | Performance statistics`);
-  }
+  };
 }