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