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 | ||
e118beaa | 70 | logPerformance(entry: PerformanceEntry, className: string): void { |
7dde0b73 | 71 | this.addPerformanceTimer(entry.name, entry.duration); |
ead548f2 | 72 | logger.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`); |
7dde0b73 JB |
73 | } |
74 | ||
6af9012e | 75 | _display(): void { |
e118beaa | 76 | logger.info(this._logPrefix() + ' %j', this._commandsStatistics); |
7dde0b73 JB |
77 | } |
78 | ||
6af9012e | 79 | _displayInterval(): void { |
c6b89400 | 80 | if (Configuration.getStatisticsDisplayInterval() > 0) { |
7dde0b73 JB |
81 | setInterval(() => { |
82 | this._display(); | |
83 | }, Configuration.getStatisticsDisplayInterval() * 1000); | |
7ec46a9a | 84 | logger.info(this._logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval())); |
7dde0b73 JB |
85 | } |
86 | } | |
87 | ||
6af9012e | 88 | start(): void { |
7dde0b73 JB |
89 | this._displayInterval(); |
90 | } | |
6af9012e | 91 | |
7ec46a9a JB |
92 | private addPerformanceTimer(command: string, duration: number): void { |
93 | // Map to proper command name | |
94 | const MAPCOMMAND = { | |
95 | sendMeterValues: 'MeterValues', | |
96 | startTransaction: 'StartTransaction', | |
97 | stopTransaction: 'StopTransaction', | |
98 | }; | |
99 | if (MAPCOMMAND[command]) { | |
100 | command = MAPCOMMAND[command] as string; | |
101 | } | |
102 | // Initialize command statistics | |
103 | if (!this._commandsStatistics[command]) { | |
104 | this._commandsStatistics[command] = {} as CommandStatisticsData; | |
105 | } | |
106 | // Update current statistics timers | |
107 | this._commandsStatistics[command].countTime = this._commandsStatistics[command].countTime ? this._commandsStatistics[command].countTime + 1 : 1; | |
108 | this._commandsStatistics[command].minTime = this._commandsStatistics[command].minTime ? (this._commandsStatistics[command].minTime > duration ? duration : this._commandsStatistics[command].minTime) : duration; | |
109 | this._commandsStatistics[command].maxTime = this._commandsStatistics[command].maxTime ? (this._commandsStatistics[command].maxTime < duration ? duration : this._commandsStatistics[command].maxTime) : duration; | |
110 | this._commandsStatistics[command].totalTime = this._commandsStatistics[command].totalTime ? this._commandsStatistics[command].totalTime + duration : duration; | |
111 | this._commandsStatistics[command].avgTime = this._commandsStatistics[command].totalTime / this._commandsStatistics[command].countTime; | |
112 | } | |
113 | ||
6af9012e JB |
114 | private _logPrefix(): string { |
115 | return Utils.logPrefix(` ${this._objName} Statistics:`); | |
116 | } | |
7dde0b73 | 117 | } |