Fix log messsage.
[e-mobility-charging-stations-simulator.git] / src / utils / Statistics.ts
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 static instance: Statistics;
13 private _objName: string;
14 private _commandsStatistics: CommandStatistics;
15
16 private constructor() {
17 this._commandsStatistics = {} as CommandStatistics;
18 }
19
20 set objName(objName: string) {
21 this._objName = objName;
22 }
23
24 static getInstance(): Statistics {
25 if (!Statistics.instance) {
26 Statistics.instance = new Statistics();
27 }
28 return Statistics.instance;
29 }
30
31 addMessage(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void {
32 switch (messageType) {
33 case MessageType.CALL_MESSAGE:
34 if (this._commandsStatistics[command] && this._commandsStatistics[command].countRequest) {
35 this._commandsStatistics[command].countRequest++;
36 } else {
37 this._commandsStatistics[command] = {} as CommandStatisticsData;
38 this._commandsStatistics[command].countRequest = 1;
39 }
40 break;
41 case MessageType.CALL_RESULT_MESSAGE:
42 if (this._commandsStatistics[command]) {
43 if (this._commandsStatistics[command].countResponse) {
44 this._commandsStatistics[command].countResponse++;
45 } else {
46 this._commandsStatistics[command].countResponse = 1;
47 }
48 } else {
49 this._commandsStatistics[command] = {} as CommandStatisticsData;
50 this._commandsStatistics[command].countResponse = 1;
51 }
52 break;
53 case MessageType.CALL_ERROR_MESSAGE:
54 if (this._commandsStatistics[command]) {
55 if (this._commandsStatistics[command].countError) {
56 this._commandsStatistics[command].countError++;
57 } else {
58 this._commandsStatistics[command].countError = 1;
59 }
60 } else {
61 this._commandsStatistics[command] = {} as CommandStatisticsData;
62 this._commandsStatistics[command].countError = 1;
63 }
64 break;
65 default:
66 logger.error(`${this._logPrefix()} Wrong message type ${messageType}`);
67 break;
68 }
69 }
70
71 logPerformance(entry: PerformanceEntry, className: string): void {
72 this.addPerformanceTimer(entry.name as RequestCommand | IncomingRequestCommand, entry.duration);
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;
78 logger.info(`${this._logPrefix()} object ${className} method(s) performance entry: %j`, perfEntry);
79 }
80
81 start(): void {
82 this._displayInterval();
83 }
84
85 private _display(): void {
86 logger.info(this._logPrefix() + ' %j', this._commandsStatistics);
87 }
88
89 private _displayInterval(): void {
90 if (Configuration.getStatisticsDisplayInterval() > 0) {
91 setInterval(() => {
92 this._display();
93 }, Configuration.getStatisticsDisplayInterval() * 1000);
94 logger.info(this._logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval()));
95 }
96 }
97
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
110 private addPerformanceTimer(command: RequestCommand | IncomingRequestCommand, duration: number): void {
111 // Map to proper command name
112 const MAPCOMMAND = {
113 sendMeterValues: 'MeterValues',
114 startTransaction: 'StartTransaction',
115 stopTransaction: 'StopTransaction',
116 };
117 if (MAPCOMMAND[command]) {
118 command = MAPCOMMAND[command] as RequestCommand | IncomingRequestCommand;
119 }
120 // Initialize command statistics
121 if (!this._commandsStatistics[command]) {
122 this._commandsStatistics[command] = {} as CommandStatisticsData;
123 }
124 // Update current statistics timers
125 this._commandsStatistics[command].countTimeMeasurement = this._commandsStatistics[command].countTimeMeasurement ? this._commandsStatistics[command].countTimeMeasurement + 1 : 1;
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;
131 Array.isArray(this._commandsStatistics[command].timeMeasurementSeries) ? this._commandsStatistics[command].timeMeasurementSeries.push(duration) : this._commandsStatistics[command].timeMeasurementSeries = [duration] as CircularArray<number>;
132 this._commandsStatistics[command].medTimeMeasurement = this.median(this._commandsStatistics[command].timeMeasurementSeries);
133 }
134
135 private _logPrefix(): string {
136 return Utils.logPrefix(` ${this._objName} Statistics:`);
137 }
138 }