CF: include TS sources.
[e-mobility-charging-stations-simulator.git] / src / utils / Statistics.ts
index a2e0d030f009221fe5bf69cef6a8aee51353c75e..6adfaef4ee4b07cdae9171dc0c762647a69e810b 100644 (file)
@@ -1,4 +1,5 @@
 import Configuration from './Configuration';
+import Constants from './Constants';
 import Utils from './Utils';
 import logger from './Logger';
 
@@ -22,52 +23,66 @@ export default class Statistics {
     return Statistics.instance;
   }
 
-  addMessage(command, response = false) {
-    if (response) {
-      if (this._statistics[command]) {
-        if (this._statistics[command].countResponse) {
-          this._statistics[command].countResponse++;
+  addMessage(command: string, messageType: number): void {
+    switch (messageType) {
+      case Constants.OCPP_JSON_CALL_MESSAGE:
+        if (this._statistics[command] && this._statistics[command].countRequest) {
+          this._statistics[command].countRequest++;
         } else {
+          this._statistics[command] = {};
+          this._statistics[command].countRequest = 1;
+        }
+        break;
+      case Constants.OCPP_JSON_CALL_RESULT_MESSAGE:
+        if (this._statistics[command]) {
+          if (this._statistics[command].countResponse) {
+            this._statistics[command].countResponse++;
+          } else {
+            this._statistics[command].countResponse = 1;
+          }
+        } else {
+          this._statistics[command] = {};
           this._statistics[command].countResponse = 1;
         }
-      } else {
-        this._statistics[command] = {};
-        this._statistics[command].countResponse = 1;
-      }
-    } else if (this._statistics[command] && this._statistics[command].count) {
-      this._statistics[command].count++;
-    } else {
-      this._statistics[command] = {};
-      this._statistics[command].count = 1;
+        break;
+      case Constants.OCPP_JSON_CALL_ERROR_MESSAGE:
+        if (this._statistics[command]) {
+          if (this._statistics[command].countError) {
+            this._statistics[command].countError++;
+          } else {
+            this._statistics[command].countError = 1;
+          }
+        } else {
+          this._statistics[command] = {};
+          this._statistics[command].countError = 1;
+        }
+        break;
+      default:
+        logger.error(`${this._logPrefix()} Wrong message type ${messageType}`);
+        break;
     }
   }
 
-  addPerformanceTimer(command, duration) {
-    let currentStatistics;
+  addPerformanceTimer(command: string, duration: number): void {
     // Map to proper command name
     const MAPCOMMAND = {
       sendMeterValues: 'MeterValues',
       startTransaction: 'StartTransaction',
       stopTransaction: 'StopTransaction',
     };
-    // Get current command statistics
     if (MAPCOMMAND[command]) {
-      currentStatistics = this._statistics[MAPCOMMAND[command]];
-    } else if (this._statistics[command]) {
-      currentStatistics = this._statistics[command];
-    } else {
-      this._statistics[command] = {};
-      currentStatistics = this._statistics[command];
+      command = MAPCOMMAND[command];
     }
-
-    if (currentStatistics) {
-      // Update current statistics timers
-      currentStatistics.countTime = currentStatistics.countTime ? currentStatistics.countTime + 1 : 1;
-      currentStatistics.minTime = currentStatistics.minTime ? (currentStatistics.minTime > duration ? duration : currentStatistics.minTime) : duration;
-      currentStatistics.maxTime = currentStatistics.maxTime ? (currentStatistics.maxTime < duration ? duration : currentStatistics.maxTime) : duration;
-      currentStatistics.totalTime = currentStatistics.totalTime ? currentStatistics.totalTime + duration : duration;
-      currentStatistics.avgTime = currentStatistics.totalTime / currentStatistics.countTime;
+    // Initialize command statistics
+    if (!this._statistics[command]) {
+      this._statistics[command] = {};
     }
+    // Update current statistics timers
+    this._statistics[command].countTime = this._statistics[command].countTime ? this._statistics[command].countTime + 1 : 1;
+    this._statistics[command].minTime = this._statistics[command].minTime ? (this._statistics[command].minTime > duration ? duration : this._statistics[command].minTime) : duration;
+    this._statistics[command].maxTime = this._statistics[command].maxTime ? (this._statistics[command].maxTime < duration ? duration : this._statistics[command].maxTime) : duration;
+    this._statistics[command].totalTime = this._statistics[command].totalTime ? this._statistics[command].totalTime + duration : duration;
+    this._statistics[command].avgTime = this._statistics[command].totalTime / this._statistics[command].countTime;
   }
 
   logPerformance(entry, className: string): void {