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