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