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