+ private median(dataSet: number[]): number {
+ if (Array.isArray(dataSet) && dataSet.length === 1) {
+ return dataSet[0];
+ }
+ const sortedDataSet = dataSet.slice().sort();
+ const middleIndex = Math.floor(sortedDataSet.length / 2);
+ if (sortedDataSet.length % 2) {
+ return sortedDataSet[middleIndex / 2];
+ }
+ return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2;
+ }
+
+ private addPerformanceTimer(command: RequestCommand | IncomingRequestCommand, duration: number): void {
+ // Map to proper command name
+ const MAPCOMMAND = {
+ sendMeterValues: 'MeterValues',
+ startTransaction: 'StartTransaction',
+ stopTransaction: 'StopTransaction',
+ };
+ if (MAPCOMMAND[command]) {
+ command = MAPCOMMAND[command] as RequestCommand | IncomingRequestCommand;
+ }
+ // Initialize command statistics
+ if (!this.commandsStatistics.commandsStatisticsData[command]) {
+ this.commandsStatistics.commandsStatisticsData[command] = {} as CommandStatisticsData;
+ }
+ // Update current statistics timers
+ this.commandsStatistics.commandsStatisticsData[command].countTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].countTimeMeasurement ? this.commandsStatistics.commandsStatisticsData[command].countTimeMeasurement + 1 : 1;
+ this.commandsStatistics.commandsStatisticsData[command].currentTimeMeasurement = duration;
+ this.commandsStatistics.commandsStatisticsData[command].minTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].minTimeMeasurement ? (this.commandsStatistics.commandsStatisticsData[command].minTimeMeasurement > duration ? duration : this.commandsStatistics.commandsStatisticsData[command].minTimeMeasurement) : duration;
+ this.commandsStatistics.commandsStatisticsData[command].maxTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].maxTimeMeasurement ? (this.commandsStatistics.commandsStatisticsData[command].maxTimeMeasurement < duration ? duration : this.commandsStatistics.commandsStatisticsData[command].maxTimeMeasurement) : duration;
+ this.commandsStatistics.commandsStatisticsData[command].totalTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].totalTimeMeasurement ? this.commandsStatistics.commandsStatisticsData[command].totalTimeMeasurement + duration : duration;
+ this.commandsStatistics.commandsStatisticsData[command].avgTimeMeasurement = this.commandsStatistics.commandsStatisticsData[command].totalTimeMeasurement / this.commandsStatistics.commandsStatisticsData[command].countTimeMeasurement;
+ Array.isArray(this.commandsStatistics.commandsStatisticsData[command].timeMeasurementSeries) ? this.commandsStatistics.commandsStatisticsData[command].timeMeasurementSeries.push(duration) : this.commandsStatistics.commandsStatisticsData[command].timeMeasurementSeries = [duration] as CircularArray<number>;
+ this.commandsStatistics.commandsStatisticsData[command].medTimeMeasurement = this.median(this.commandsStatistics.commandsStatisticsData[command].timeMeasurementSeries);