Fix max number of connectors calculation.
[e-mobility-charging-stations-simulator.git] / src / utils / Statistics.js
index bba3d6b174fd829b8dcc9e714042ae67363c2e1d..d53cbe45f415b447d118bd470cc4aab2b7526f95 100644 (file)
@@ -3,13 +3,25 @@ const logger = require('./Logger');
 const Utils = require('./Utils');
 
 class Statistics {
-  constructor(objName) {
-    this._objName = objName;
+  static instance;
+
+  constructor() {
     this._statistics = {};
   }
 
-  _basicFormatLog() {
-    return Utils.basicFormatLog(` ${this._objName} Statistics:`);
+  set objName(objName) {
+    this._objName = objName;
+  }
+
+  static getInstance() {
+    if (!Statistics.instance) {
+      Statistics.instance = new Statistics();
+    }
+    return Statistics.instance;
+  }
+
+  _logPrefix() {
+    return Utils.logPrefix(` ${this._objName} Statistics:`);
   }
 
   addMessage(command, response = false) {
@@ -40,7 +52,8 @@ class Statistics {
       startTransaction: 'StartTransaction',
       stopTransaction: 'StopTransaction',
     };
-    if (MAPCOMMAND[command]) { // Get current command statistics
+    // Get current command statistics
+    if (MAPCOMMAND[command]) {
       currentStatistics = this._statistics[MAPCOMMAND[command]];
     } else if (this._statistics[command]) {
       currentStatistics = this._statistics[command];
@@ -51,31 +64,29 @@ class Statistics {
 
     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.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;
     }
   }
 
   logPerformance(entry, className) {
     this.addPerformanceTimer(entry.name, entry.duration);
-    logger.info(`${this._basicFormatLog()} class->${className}, method->${entry.name}, duration->${entry.duration}`);
+    logger.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`);
   }
 
   _display() {
-    // logger.info(this._basicFormatLog() + ' STARTING')
-    logger.info(this._basicFormatLog() + ' %j', this._statistics);
-    // logger.info(this._basicFormatLog() + ' ENDING')
+    logger.info(this._logPrefix() + ' %j', this._statistics);
   }
 
   _displayInterval() {
-    if (Configuration.getStatisticsDisplayInterval()) {
-      logger.info(this._basicFormatLog() + ' displayed every ' + Configuration.getStatisticsDisplayInterval() + 's');
+    if (Configuration.getStatisticsDisplayInterval() > 0) {
       setInterval(() => {
         this._display();
       }, Configuration.getStatisticsDisplayInterval() * 1000);
+      logger.info(this._logPrefix() + ' displayed every ' + Configuration.getStatisticsDisplayInterval() + 's');
     }
   }