Commit | Line | Data |
---|---|---|
6bf6769e | 1 | import CommandStatistics, { CommandStatisticsData, PerfEntry } from '../types/CommandStatistics'; |
63b48f77 | 2 | |
6af9012e | 3 | import Configuration from './Configuration'; |
7f134aca | 4 | import Constants from './Constants'; |
e118beaa | 5 | import { PerformanceEntry } from 'perf_hooks'; |
6af9012e JB |
6 | import Utils from './Utils'; |
7 | import logger from './Logger'; | |
7dde0b73 | 8 | |
3f40bc9c | 9 | export default class Statistics { |
6af9012e | 10 | private static instance: Statistics; |
6af9012e | 11 | private _objName: string; |
63b48f77 | 12 | private _commandsStatistics: CommandStatistics; |
560bcf5b | 13 | |
6af9012e | 14 | private constructor() { |
63b48f77 | 15 | this._commandsStatistics = {} as CommandStatistics; |
7dde0b73 JB |
16 | } |
17 | ||
6af9012e | 18 | set objName(objName: string) { |
560bcf5b JB |
19 | this._objName = objName; |
20 | } | |
21 | ||
6af9012e | 22 | static getInstance(): Statistics { |
560bcf5b JB |
23 | if (!Statistics.instance) { |
24 | Statistics.instance = new Statistics(); | |
25 | } | |
26 | return Statistics.instance; | |
27 | } | |
28 | ||
7f134aca JB |
29 | addMessage(command: string, messageType: number): void { |
30 | switch (messageType) { | |
31 | case Constants.OCPP_JSON_CALL_MESSAGE: | |
e118beaa JB |
32 | if (this._commandsStatistics[command] && this._commandsStatistics[command].countRequest) { |
33 | this._commandsStatistics[command].countRequest++; | |
7dde0b73 | 34 | } else { |
e118beaa JB |
35 | this._commandsStatistics[command] = {} as CommandStatisticsData; |
36 | this._commandsStatistics[command].countRequest = 1; | |
7f134aca JB |
37 | } |
38 | break; | |
39 | case Constants.OCPP_JSON_CALL_RESULT_MESSAGE: | |
e118beaa JB |
40 | if (this._commandsStatistics[command]) { |
41 | if (this._commandsStatistics[command].countResponse) { | |
42 | this._commandsStatistics[command].countResponse++; | |
7f134aca | 43 | } else { |
e118beaa | 44 | this._commandsStatistics[command].countResponse = 1; |
7f134aca JB |
45 | } |
46 | } else { | |
e118beaa JB |
47 | this._commandsStatistics[command] = {} as CommandStatisticsData; |
48 | this._commandsStatistics[command].countResponse = 1; | |
7dde0b73 | 49 | } |
7f134aca JB |
50 | break; |
51 | case Constants.OCPP_JSON_CALL_ERROR_MESSAGE: | |
e118beaa JB |
52 | if (this._commandsStatistics[command]) { |
53 | if (this._commandsStatistics[command].countError) { | |
54 | this._commandsStatistics[command].countError++; | |
7f134aca | 55 | } else { |
e118beaa | 56 | this._commandsStatistics[command].countError = 1; |
7f134aca JB |
57 | } |
58 | } else { | |
e118beaa JB |
59 | this._commandsStatistics[command] = {} as CommandStatisticsData; |
60 | this._commandsStatistics[command].countError = 1; | |
7f134aca JB |
61 | } |
62 | break; | |
63 | default: | |
64 | logger.error(`${this._logPrefix()} Wrong message type ${messageType}`); | |
65 | break; | |
7dde0b73 JB |
66 | } |
67 | } | |
68 | ||
e118beaa | 69 | logPerformance(entry: PerformanceEntry, className: string): void { |
7dde0b73 | 70 | this.addPerformanceTimer(entry.name, entry.duration); |
6bf6769e JB |
71 | const perfEntry: PerfEntry = {} as PerfEntry; |
72 | perfEntry.name = entry.name; | |
73 | perfEntry.entryType = entry.entryType; | |
74 | perfEntry.startTime = entry.startTime; | |
75 | perfEntry.duration = entry.duration; | |
76 | logger.info(`${this._logPrefix()} object ${className} method performance entry: %j`, perfEntry); | |
7dde0b73 JB |
77 | } |
78 | ||
6af9012e | 79 | _display(): void { |
e118beaa | 80 | logger.info(this._logPrefix() + ' %j', this._commandsStatistics); |
7dde0b73 JB |
81 | } |
82 | ||
6af9012e | 83 | _displayInterval(): void { |
c6b89400 | 84 | if (Configuration.getStatisticsDisplayInterval() > 0) { |
7dde0b73 JB |
85 | setInterval(() => { |
86 | this._display(); | |
87 | }, Configuration.getStatisticsDisplayInterval() * 1000); | |
7ec46a9a | 88 | logger.info(this._logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval())); |
7dde0b73 JB |
89 | } |
90 | } | |
91 | ||
6af9012e | 92 | start(): void { |
7dde0b73 JB |
93 | this._displayInterval(); |
94 | } | |
6af9012e | 95 | |
6bf6769e JB |
96 | private median(dataSet: number[]): number { |
97 | if (Array.isArray(dataSet) && dataSet.length === 1) { | |
98 | return dataSet[0]; | |
99 | } | |
100 | const sortedDataSet = dataSet.slice().sort(); | |
101 | const middleIndex = Math.floor(sortedDataSet.length / 2); | |
102 | if (sortedDataSet.length % 2) { | |
103 | return sortedDataSet[middleIndex / 2]; | |
104 | } | |
105 | return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2; | |
106 | } | |
107 | ||
7ec46a9a JB |
108 | private addPerformanceTimer(command: string, duration: number): void { |
109 | // Map to proper command name | |
110 | const MAPCOMMAND = { | |
111 | sendMeterValues: 'MeterValues', | |
112 | startTransaction: 'StartTransaction', | |
113 | stopTransaction: 'StopTransaction', | |
114 | }; | |
115 | if (MAPCOMMAND[command]) { | |
116 | command = MAPCOMMAND[command] as string; | |
117 | } | |
118 | // Initialize command statistics | |
119 | if (!this._commandsStatistics[command]) { | |
120 | this._commandsStatistics[command] = {} as CommandStatisticsData; | |
121 | } | |
122 | // Update current statistics timers | |
4a71152b | 123 | this._commandsStatistics[command].countTimeMeasurement = this._commandsStatistics[command].countTimeMeasurement ? this._commandsStatistics[command].countTimeMeasurement + 1 : 1; |
6bf6769e JB |
124 | this._commandsStatistics[command].currentTimeMeasurement = duration; |
125 | this._commandsStatistics[command].minTimeMeasurement = this._commandsStatistics[command].minTimeMeasurement ? (this._commandsStatistics[command].minTimeMeasurement > duration ? duration : this._commandsStatistics[command].minTimeMeasurement) : duration; | |
126 | this._commandsStatistics[command].maxTimeMeasurement = this._commandsStatistics[command].maxTimeMeasurement ? (this._commandsStatistics[command].maxTimeMeasurement < duration ? duration : this._commandsStatistics[command].maxTimeMeasurement) : duration; | |
127 | this._commandsStatistics[command].totalTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement ? this._commandsStatistics[command].totalTimeMeasurement + duration : duration; | |
128 | this._commandsStatistics[command].avgTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement / this._commandsStatistics[command].countTimeMeasurement; | |
129 | Array.isArray(this._commandsStatistics[command].timeMeasurementSeries) ? this._commandsStatistics[command].timeMeasurementSeries.push(duration) : this._commandsStatistics[command].timeMeasurementSeries = [duration]; | |
130 | this._commandsStatistics[command].medTimeMeasurement = this.median(this._commandsStatistics[command].timeMeasurementSeries); | |
7ec46a9a JB |
131 | } |
132 | ||
6af9012e JB |
133 | private _logPrefix(): string { |
134 | return Utils.logPrefix(` ${this._objName} Statistics:`); | |
135 | } | |
7dde0b73 | 136 | } |