Use generic for worker data type.
[e-mobility-charging-stations-simulator.git] / src / utils / Statistics.ts
index d6174d96dea7a602fa19712816283e42a8a25701..2aa99b0d4c9f317c78d14a052c65da80f4cbac4c 100644 (file)
@@ -9,57 +9,46 @@ import Utils from './Utils';
 import logger from './Logger';
 
 export default class Statistics {
-  private static instance: Statistics;
-  private _objName: string;
-  private _commandsStatistics: CommandStatistics;
+  private objId: string;
+  private commandsStatistics: CommandStatistics;
 
-  private constructor() {
-    this._commandsStatistics = {} as CommandStatistics;
+  public constructor(objName: string) {
+    this.objId = objName;
+    this.commandsStatistics = { id: this.objId ? this.objId : 'Object id not specified', commandsStatisticsData: {} };
   }
 
-  set objName(objName: string) {
-    this._objName = objName;
-  }
-
-  static getInstance(): Statistics {
-    if (!Statistics.instance) {
-      Statistics.instance = new Statistics();
-    }
-    return Statistics.instance;
-  }
-
-  addMessage(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void {
+  public addMessage(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void {
     switch (messageType) {
       case MessageType.CALL_MESSAGE:
-        if (this._commandsStatistics[command] && this._commandsStatistics[command].countRequest) {
-          this._commandsStatistics[command].countRequest++;
+        if (this.commandsStatistics.commandsStatisticsData[command] && this.commandsStatistics.commandsStatisticsData[command].countRequest) {
+          this.commandsStatistics.commandsStatisticsData[command].countRequest++;
         } else {
-          this._commandsStatistics[command] = {} as CommandStatisticsData;
-          this._commandsStatistics[command].countRequest = 1;
+          this.commandsStatistics.commandsStatisticsData[command] = {} as CommandStatisticsData;
+          this.commandsStatistics.commandsStatisticsData[command].countRequest = 1;
         }
         break;
       case MessageType.CALL_RESULT_MESSAGE:
-        if (this._commandsStatistics[command]) {
-          if (this._commandsStatistics[command].countResponse) {
-            this._commandsStatistics[command].countResponse++;
+        if (this.commandsStatistics.commandsStatisticsData[command]) {
+          if (this.commandsStatistics.commandsStatisticsData[command].countResponse) {
+            this.commandsStatistics.commandsStatisticsData[command].countResponse++;
           } else {
-            this._commandsStatistics[command].countResponse = 1;
+            this.commandsStatistics.commandsStatisticsData[command].countResponse = 1;
           }
         } else {
-          this._commandsStatistics[command] = {} as CommandStatisticsData;
-          this._commandsStatistics[command].countResponse = 1;
+          this.commandsStatistics.commandsStatisticsData[command] = {} as CommandStatisticsData;
+          this.commandsStatistics.commandsStatisticsData[command].countResponse = 1;
         }
         break;
       case MessageType.CALL_ERROR_MESSAGE:
-        if (this._commandsStatistics[command]) {
-          if (this._commandsStatistics[command].countError) {
-            this._commandsStatistics[command].countError++;
+        if (this.commandsStatistics.commandsStatisticsData[command]) {
+          if (this.commandsStatistics.commandsStatisticsData[command].countError) {
+            this.commandsStatistics.commandsStatisticsData[command].countError++;
           } else {
-            this._commandsStatistics[command].countError = 1;
+            this.commandsStatistics.commandsStatisticsData[command].countError = 1;
           }
         } else {
-          this._commandsStatistics[command] = {} as CommandStatisticsData;
-          this._commandsStatistics[command].countError = 1;
+          this.commandsStatistics.commandsStatisticsData[command] = {} as CommandStatisticsData;
+          this.commandsStatistics.commandsStatisticsData[command].countError = 1;
         }
         break;
       default:
@@ -68,7 +57,7 @@ export default class Statistics {
     }
   }
 
-  logPerformance(entry: PerformanceEntry, className: string): void {
+  public logPerformance(entry: PerformanceEntry, className: string): void {
     this.addPerformanceTimer(entry.name as RequestCommand | IncomingRequestCommand, entry.duration);
     const perfEntry: PerfEntry = {} as PerfEntry;
     perfEntry.name = entry.name;
@@ -78,12 +67,12 @@ export default class Statistics {
     logger.info(`${this._logPrefix()} object ${className} method(s) performance entry: %j`, perfEntry);
   }
 
-  start(): void {
+  public start(): void {
     this._displayInterval();
   }
 
   private _display(): void {
-    logger.info(this._logPrefix() + ' %j', this._commandsStatistics);
+    logger.info(this._logPrefix() + ' %j', this.commandsStatistics);
   }
 
   private _displayInterval(): void {
@@ -118,21 +107,21 @@ export default class Statistics {
       command = MAPCOMMAND[command] as RequestCommand | IncomingRequestCommand;
     }
     // Initialize command statistics
-    if (!this._commandsStatistics[command]) {
-      this._commandsStatistics[command] = {} as CommandStatisticsData;
+    if (!this.commandsStatistics.commandsStatisticsData[command]) {
+      this.commandsStatistics.commandsStatisticsData[command] = {} as CommandStatisticsData;
     }
     // Update current statistics timers
-    this._commandsStatistics[command].countTimeMeasurement = this._commandsStatistics[command].countTimeMeasurement ? this._commandsStatistics[command].countTimeMeasurement + 1 : 1;
-    this._commandsStatistics[command].currentTimeMeasurement = duration;
-    this._commandsStatistics[command].minTimeMeasurement = this._commandsStatistics[command].minTimeMeasurement ? (this._commandsStatistics[command].minTimeMeasurement > duration ? duration : this._commandsStatistics[command].minTimeMeasurement) : duration;
-    this._commandsStatistics[command].maxTimeMeasurement = this._commandsStatistics[command].maxTimeMeasurement ? (this._commandsStatistics[command].maxTimeMeasurement < duration ? duration : this._commandsStatistics[command].maxTimeMeasurement) : duration;
-    this._commandsStatistics[command].totalTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement ? this._commandsStatistics[command].totalTimeMeasurement + duration : duration;
-    this._commandsStatistics[command].avgTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement / this._commandsStatistics[command].countTimeMeasurement;
-    Array.isArray(this._commandsStatistics[command].timeMeasurementSeries) ? this._commandsStatistics[command].timeMeasurementSeries.push(duration) : this._commandsStatistics[command].timeMeasurementSeries = [duration] as CircularArray<number>;
-    this._commandsStatistics[command].medTimeMeasurement = this.median(this._commandsStatistics[command].timeMeasurementSeries);
+    this.commandsStatistics.commandsStatisticsData[command].countTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].countTimeMeasurement ? this.commandsStatistics.commandsStatisticsData[command].countTimeMeasurement + 1 : 1;
+    this.commandsStatistics.commandsStatisticsData[command].currentTimeMeasurement = duration;
+    this.commandsStatistics.commandsStatisticsData[command].minTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].minTimeMeasurement ? (this.commandsStatistics.commandsStatisticsData[command].minTimeMeasurement > duration ? duration : this.commandsStatistics.commandsStatisticsData[command].minTimeMeasurement) : duration;
+    this.commandsStatistics.commandsStatisticsData[command].maxTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].maxTimeMeasurement ? (this.commandsStatistics.commandsStatisticsData[command].maxTimeMeasurement < duration ? duration : this.commandsStatistics.commandsStatisticsData[command].maxTimeMeasurement) : duration;
+    this.commandsStatistics.commandsStatisticsData[command].totalTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].totalTimeMeasurement ? this.commandsStatistics.commandsStatisticsData[command].totalTimeMeasurement + duration : duration;
+    this.commandsStatistics.commandsStatisticsData[command].avgTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].totalTimeMeasurement / this.commandsStatistics.commandsStatisticsData[command].countTimeMeasurement;
+    Array.isArray(this.commandsStatistics.commandsStatisticsData[command].timeMeasurementSeries) ? this.commandsStatistics.commandsStatisticsData[command].timeMeasurementSeries.push(duration) : this.commandsStatistics.commandsStatisticsData[command].timeMeasurementSeries = [duration] as CircularArray<number>;
+    this.commandsStatistics.commandsStatisticsData[command].medTimeMeasurement = this.median(this.commandsStatistics.commandsStatisticsData[command].timeMeasurementSeries);
   }
 
   private _logPrefix(): string {
-    return Utils.logPrefix(` ${this._objName} Statistics:`);
+    return Utils.logPrefix(` ${this.objId} Statistics:`);
   }
 }