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