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