Commit | Line | Data |
---|---|---|
7dde0b73 JB |
1 | const Configuration = require('./Configuration'); |
2 | const logger = require('./Logger'); | |
3 | const Utils = require('./Utils'); | |
4 | ||
5 | class Statistics { | |
560bcf5b JB |
6 | static instance; |
7 | ||
8 | constructor() { | |
7dde0b73 JB |
9 | this._statistics = {}; |
10 | } | |
11 | ||
560bcf5b JB |
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 | ||
ead548f2 JB |
23 | _logPrefix() { |
24 | return Utils.logPrefix(` ${this._objName} Statistics:`); | |
7dde0b73 JB |
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 | }; | |
2e6f5966 JB |
55 | // Get current command statistics |
56 | if (MAPCOMMAND[command]) { | |
7dde0b73 JB |
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 | |
72766a82 JB |
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; | |
7dde0b73 JB |
71 | currentStatistics.avgTime = currentStatistics.totalTime / currentStatistics.countTime; |
72 | } | |
73 | } | |
74 | ||
75 | logPerformance(entry, className) { | |
76 | this.addPerformanceTimer(entry.name, entry.duration); | |
ead548f2 | 77 | logger.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`); |
7dde0b73 JB |
78 | } |
79 | ||
80 | _display() { | |
ead548f2 | 81 | logger.info(this._logPrefix() + ' %j', this._statistics); |
7dde0b73 JB |
82 | } |
83 | ||
84 | _displayInterval() { | |
c6b89400 | 85 | if (Configuration.getStatisticsDisplayInterval() > 0) { |
7dde0b73 JB |
86 | setInterval(() => { |
87 | this._display(); | |
88 | }, Configuration.getStatisticsDisplayInterval() * 1000); | |
ead548f2 | 89 | logger.info(this._logPrefix() + ' displayed every ' + Configuration.getStatisticsDisplayInterval() + 's'); |
7dde0b73 JB |
90 | } |
91 | } | |
92 | ||
93 | async start() { | |
94 | this._displayInterval(); | |
95 | } | |
96 | } | |
97 | ||
98 | module.exports = Statistics; |