Recommend imports sorting code extension.
[e-mobility-charging-stations-simulator.git] / src / utils / Statistics.ts
1 import Configuration from './Configuration';
2 import Constants from './Constants';
3 import Utils from './Utils';
4 import logger from './Logger';
5
6 export default class Statistics {
7 private static instance: Statistics;
8 private _statistics;
9 private _objName: string;
10
11 private constructor() {
12 this._statistics = {};
13 }
14
15 set objName(objName: string) {
16 this._objName = objName;
17 }
18
19 static getInstance(): Statistics {
20 if (!Statistics.instance) {
21 Statistics.instance = new Statistics();
22 }
23 return Statistics.instance;
24 }
25
26 addMessage(command: string, messageType: number): void {
27 switch (messageType) {
28 case Constants.OCPP_JSON_CALL_MESSAGE:
29 if (this._statistics[command] && this._statistics[command].count) {
30 this._statistics[command].countRequest++;
31 } else {
32 this._statistics[command] = {};
33 this._statistics[command].countRequest = 1;
34 }
35 break;
36 case Constants.OCPP_JSON_CALL_RESULT_MESSAGE:
37 if (this._statistics[command]) {
38 if (this._statistics[command].countResponse) {
39 this._statistics[command].countResponse++;
40 } else {
41 this._statistics[command].countResponse = 1;
42 }
43 } else {
44 this._statistics[command] = {};
45 this._statistics[command].countResponse = 1;
46 }
47 break;
48 case Constants.OCPP_JSON_CALL_ERROR_MESSAGE:
49 if (this._statistics[command]) {
50 if (this._statistics[command].countError) {
51 this._statistics[command].countError++;
52 } else {
53 this._statistics[command].countError = 1;
54 }
55 } else {
56 this._statistics[command] = {};
57 this._statistics[command].countError = 1;
58 }
59 break;
60 default:
61 logger.error(`${this._logPrefix()} Wrong message type ${messageType}`);
62 break;
63 }
64 }
65
66 addPerformanceTimer(command, duration) {
67 let currentStatistics;
68 // Map to proper command name
69 const MAPCOMMAND = {
70 sendMeterValues: 'MeterValues',
71 startTransaction: 'StartTransaction',
72 stopTransaction: 'StopTransaction',
73 };
74 // Get current command statistics
75 if (MAPCOMMAND[command]) {
76 currentStatistics = this._statistics[MAPCOMMAND[command]];
77 } else if (this._statistics[command]) {
78 currentStatistics = this._statistics[command];
79 } else {
80 this._statistics[command] = {};
81 currentStatistics = this._statistics[command];
82 }
83
84 if (currentStatistics) {
85 // Update current statistics timers
86 currentStatistics.countTime = currentStatistics.countTime ? currentStatistics.countTime + 1 : 1;
87 currentStatistics.minTime = currentStatistics.minTime ? (currentStatistics.minTime > duration ? duration : currentStatistics.minTime) : duration;
88 currentStatistics.maxTime = currentStatistics.maxTime ? (currentStatistics.maxTime < duration ? duration : currentStatistics.maxTime) : duration;
89 currentStatistics.totalTime = currentStatistics.totalTime ? currentStatistics.totalTime + duration : duration;
90 currentStatistics.avgTime = currentStatistics.totalTime / currentStatistics.countTime;
91 }
92 }
93
94 logPerformance(entry, className: string): void {
95 this.addPerformanceTimer(entry.name, entry.duration);
96 logger.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`);
97 }
98
99 _display(): void {
100 logger.info(this._logPrefix() + ' %j', this._statistics);
101 }
102
103 _displayInterval(): void {
104 if (Configuration.getStatisticsDisplayInterval() > 0) {
105 setInterval(() => {
106 this._display();
107 }, Configuration.getStatisticsDisplayInterval() * 1000);
108 logger.info(this._logPrefix() + ' displayed every ' + Configuration.getStatisticsDisplayInterval() + 's');
109 }
110 }
111
112 start(): void {
113 this._displayInterval();
114 }
115
116 private _logPrefix(): string {
117 return Utils.logPrefix(` ${this._objName} Statistics:`);
118 }
119 }