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