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