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