1 import CommandStatistics
, { CommandStatisticsData
} from
'../types/CommandStatistics';
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';
9 export default class Statistics
{
10 private static instance
: Statistics
;
11 private _objName
: string;
12 private _commandsStatistics
: CommandStatistics
;
14 private constructor() {
15 this._commandsStatistics
= {} as CommandStatistics
;
18 set
objName(objName
: string) {
19 this._objName
= objName
;
22 static getInstance(): Statistics
{
23 if (!Statistics
.instance
) {
24 Statistics
.instance
= new Statistics();
26 return Statistics
.instance
;
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
++;
35 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
36 this._commandsStatistics
[command
].countRequest
= 1;
39 case Constants
.OCPP_JSON_CALL_RESULT_MESSAGE
:
40 if (this._commandsStatistics
[command
]) {
41 if (this._commandsStatistics
[command
].countResponse
) {
42 this._commandsStatistics
[command
].countResponse
++;
44 this._commandsStatistics
[command
].countResponse
= 1;
47 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
48 this._commandsStatistics
[command
].countResponse
= 1;
51 case Constants
.OCPP_JSON_CALL_ERROR_MESSAGE
:
52 if (this._commandsStatistics
[command
]) {
53 if (this._commandsStatistics
[command
].countError
) {
54 this._commandsStatistics
[command
].countError
++;
56 this._commandsStatistics
[command
].countError
= 1;
59 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
60 this._commandsStatistics
[command
].countError
= 1;
64 logger
.error(`${this._logPrefix()} Wrong message type ${messageType}`);
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}`);
75 logger
.info(this._logPrefix() + ' %j', this._commandsStatistics
);
78 _displayInterval(): void {
79 if (Configuration
.getStatisticsDisplayInterval() > 0) {
82 }, Configuration
.getStatisticsDisplayInterval() * 1000);
83 logger
.info(this._logPrefix() + ' displayed every ' + Utils
.secondsToHHMMSS(Configuration
.getStatisticsDisplayInterval()));
88 this._displayInterval();
91 private addPerformanceTimer(command
: string, duration
: number): void {
92 // Map to proper command name
94 sendMeterValues
: 'MeterValues',
95 startTransaction
: 'StartTransaction',
96 stopTransaction
: 'StopTransaction',
98 if (MAPCOMMAND
[command
]) {
99 command
= MAPCOMMAND
[command
] as string;
101 // Initialize command statistics
102 if (!this._commandsStatistics
[command
]) {
103 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
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
;
114 private _logPrefix(): string {
115 return Utils
.logPrefix(` ${this._objName} Statistics:`);