c2ad25a9938aeb50e60d548a59176f9bfd50a1ab
[e-mobility-charging-stations-simulator.git] / src / utils / Statistics.ts
1 import CommandStatistics, { CommandStatisticsData } 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 logger.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`);
72 }
73
74 _display(): void {
75 logger.info(this._logPrefix() + ' %j', this._commandsStatistics);
76 }
77
78 _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 start(): void {
88 this._displayInterval();
89 }
90
91 private addPerformanceTimer(command: string, duration: number): void {
92 // Map to proper command name
93 const MAPCOMMAND = {
94 sendMeterValues: 'MeterValues',
95 startTransaction: 'StartTransaction',
96 stopTransaction: 'StopTransaction',
97 };
98 if (MAPCOMMAND[command]) {
99 command = MAPCOMMAND[command] as string;
100 }
101 // Initialize command statistics
102 if (!this._commandsStatistics[command]) {
103 this._commandsStatistics[command] = {} as CommandStatisticsData;
104 }
105 // Update current statistics timers
106 this._commandsStatistics[command].countTimeMeasurement = this._commandsStatistics[command].countTimeMeasurement ? this._commandsStatistics[command].countTimeMeasurement + 1 : 1;
107 this._commandsStatistics[command].currentTime = duration;
108 this._commandsStatistics[command].minTime = this._commandsStatistics[command].minTime ? (this._commandsStatistics[command].minTime > duration ? duration : this._commandsStatistics[command].minTime) : duration;
109 this._commandsStatistics[command].maxTime = this._commandsStatistics[command].maxTime ? (this._commandsStatistics[command].maxTime < duration ? duration : this._commandsStatistics[command].maxTime) : duration;
110 this._commandsStatistics[command].totalTime = this._commandsStatistics[command].totalTime ? this._commandsStatistics[command].totalTime + duration : duration;
111 this._commandsStatistics[command].avgTime = this._commandsStatistics[command].totalTime / this._commandsStatistics[command].countTimeMeasurement;
112 }
113
114 private _logPrefix(): string {
115 return Utils.logPrefix(` ${this._objName} Statistics:`);
116 }
117 }