Rename index.js to start.js
[e-mobility-charging-stations-simulator.git] / src / utils / Statistics.js
CommitLineData
7dde0b73
JB
1const Configuration = require('./Configuration');
2const logger = require('./Logger');
3const Utils = require('./Utils');
4
5class Statistics {
6 constructor(objName) {
7 this._objName = objName;
8 this._statistics = {};
9 }
10
ead548f2
JB
11 _logPrefix() {
12 return Utils.logPrefix(` ${this._objName} Statistics:`);
7dde0b73
JB
13 }
14
15 addMessage(command, response = false) {
16 if (response) {
17 if (this._statistics[command]) {
18 if (this._statistics[command].countResponse) {
19 this._statistics[command].countResponse++;
20 } else {
21 this._statistics[command].countResponse = 1;
22 }
23 } else {
24 this._statistics[command] = {};
25 this._statistics[command].countResponse = 1;
26 }
27 } else if (this._statistics[command] && this._statistics[command].count) {
28 this._statistics[command].count++;
29 } else {
30 this._statistics[command] = {};
31 this._statistics[command].count = 1;
32 }
33 }
34
35 addPerformanceTimer(command, duration) {
36 let currentStatistics;
37 // Map to proper command name
38 const MAPCOMMAND = {
39 sendMeterValues: 'MeterValues',
40 startTransaction: 'StartTransaction',
41 stopTransaction: 'StopTransaction',
42 };
2e6f5966
JB
43 // Get current command statistics
44 if (MAPCOMMAND[command]) {
7dde0b73
JB
45 currentStatistics = this._statistics[MAPCOMMAND[command]];
46 } else if (this._statistics[command]) {
47 currentStatistics = this._statistics[command];
48 } else {
49 this._statistics[command] = {};
50 currentStatistics = this._statistics[command];
51 }
52
53 if (currentStatistics) {
54 // Update current statistics timers
72766a82
JB
55 currentStatistics.countTime = currentStatistics.countTime ? currentStatistics.countTime + 1 : 1;
56 currentStatistics.minTime = currentStatistics.minTime ? (currentStatistics.minTime > duration ? duration : currentStatistics.minTime) : duration;
57 currentStatistics.maxTime = currentStatistics.maxTime ? (currentStatistics.maxTime < duration ? duration : currentStatistics.maxTime) : duration;
58 currentStatistics.totalTime = currentStatistics.totalTime ? currentStatistics.totalTime + duration : duration;
7dde0b73
JB
59 currentStatistics.avgTime = currentStatistics.totalTime / currentStatistics.countTime;
60 }
61 }
62
63 logPerformance(entry, className) {
64 this.addPerformanceTimer(entry.name, entry.duration);
ead548f2 65 logger.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`);
7dde0b73
JB
66 }
67
68 _display() {
ead548f2 69 logger.info(this._logPrefix() + ' %j', this._statistics);
7dde0b73
JB
70 }
71
72 _displayInterval() {
c6b89400 73 if (Configuration.getStatisticsDisplayInterval() > 0) {
7dde0b73
JB
74 setInterval(() => {
75 this._display();
76 }, Configuration.getStatisticsDisplayInterval() * 1000);
ead548f2 77 logger.info(this._logPrefix() + ' displayed every ' + Configuration.getStatisticsDisplayInterval() + 's');
7dde0b73
JB
78 }
79 }
80
81 async start() {
82 this._displayInterval();
83 }
84}
85
86module.exports = Statistics;