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