X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FStatistics.ts;h=d6174d96dea7a602fa19712816283e42a8a25701;hb=df6dddca99d23c0458eb4758f651a3be8e5d2d91;hp=3fa4c78321ddafe219ba750aa11b78c971f83b63;hpb=6bf6769ef4ee4fd3935a442243984a8aa2136e53;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Statistics.ts b/src/utils/Statistics.ts index 3fa4c783..d6174d96 100644 --- a/src/utils/Statistics.ts +++ b/src/utils/Statistics.ts @@ -1,7 +1,9 @@ import CommandStatistics, { CommandStatisticsData, PerfEntry } from '../types/CommandStatistics'; +import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/1.6/Requests'; +import CircularArray from './CircularArray'; import Configuration from './Configuration'; -import Constants from './Constants'; +import { MessageType } from '../types/ocpp/MessageType'; import { PerformanceEntry } from 'perf_hooks'; import Utils from './Utils'; import logger from './Logger'; @@ -26,9 +28,9 @@ export default class Statistics { return Statistics.instance; } - addMessage(command: string, messageType: number): void { + addMessage(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void { switch (messageType) { - case Constants.OCPP_JSON_CALL_MESSAGE: + case MessageType.CALL_MESSAGE: if (this._commandsStatistics[command] && this._commandsStatistics[command].countRequest) { this._commandsStatistics[command].countRequest++; } else { @@ -36,7 +38,7 @@ export default class Statistics { this._commandsStatistics[command].countRequest = 1; } break; - case Constants.OCPP_JSON_CALL_RESULT_MESSAGE: + case MessageType.CALL_RESULT_MESSAGE: if (this._commandsStatistics[command]) { if (this._commandsStatistics[command].countResponse) { this._commandsStatistics[command].countResponse++; @@ -48,7 +50,7 @@ export default class Statistics { this._commandsStatistics[command].countResponse = 1; } break; - case Constants.OCPP_JSON_CALL_ERROR_MESSAGE: + case MessageType.CALL_ERROR_MESSAGE: if (this._commandsStatistics[command]) { if (this._commandsStatistics[command].countError) { this._commandsStatistics[command].countError++; @@ -67,20 +69,24 @@ export default class Statistics { } logPerformance(entry: PerformanceEntry, className: string): void { - this.addPerformanceTimer(entry.name, entry.duration); + this.addPerformanceTimer(entry.name as RequestCommand | IncomingRequestCommand, entry.duration); const perfEntry: PerfEntry = {} as PerfEntry; perfEntry.name = entry.name; perfEntry.entryType = entry.entryType; perfEntry.startTime = entry.startTime; perfEntry.duration = entry.duration; - logger.info(`${this._logPrefix()} object ${className} method performance entry: %j`, perfEntry); + logger.info(`${this._logPrefix()} object ${className} method(s) performance entry: %j`, perfEntry); } - _display(): void { + start(): void { + this._displayInterval(); + } + + private _display(): void { logger.info(this._logPrefix() + ' %j', this._commandsStatistics); } - _displayInterval(): void { + private _displayInterval(): void { if (Configuration.getStatisticsDisplayInterval() > 0) { setInterval(() => { this._display(); @@ -89,10 +95,6 @@ export default class Statistics { } } - start(): void { - this._displayInterval(); - } - private median(dataSet: number[]): number { if (Array.isArray(dataSet) && dataSet.length === 1) { return dataSet[0]; @@ -105,7 +107,7 @@ export default class Statistics { return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2; } - private addPerformanceTimer(command: string, duration: number): void { + private addPerformanceTimer(command: RequestCommand | IncomingRequestCommand, duration: number): void { // Map to proper command name const MAPCOMMAND = { sendMeterValues: 'MeterValues', @@ -113,7 +115,7 @@ export default class Statistics { stopTransaction: 'StopTransaction', }; if (MAPCOMMAND[command]) { - command = MAPCOMMAND[command] as string; + command = MAPCOMMAND[command] as RequestCommand | IncomingRequestCommand; } // Initialize command statistics if (!this._commandsStatistics[command]) { @@ -126,7 +128,7 @@ export default class Statistics { this._commandsStatistics[command].maxTimeMeasurement = this._commandsStatistics[command].maxTimeMeasurement ? (this._commandsStatistics[command].maxTimeMeasurement < duration ? duration : this._commandsStatistics[command].maxTimeMeasurement) : duration; this._commandsStatistics[command].totalTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement ? this._commandsStatistics[command].totalTimeMeasurement + duration : duration; this._commandsStatistics[command].avgTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement / this._commandsStatistics[command].countTimeMeasurement; - Array.isArray(this._commandsStatistics[command].timeMeasurementSeries) ? this._commandsStatistics[command].timeMeasurementSeries.push(duration) : this._commandsStatistics[command].timeMeasurementSeries = [duration]; + Array.isArray(this._commandsStatistics[command].timeMeasurementSeries) ? this._commandsStatistics[command].timeMeasurementSeries.push(duration) : this._commandsStatistics[command].timeMeasurementSeries = [duration] as CircularArray; this._commandsStatistics[command].medTimeMeasurement = this.median(this._commandsStatistics[command].timeMeasurementSeries); }