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';
8 export default class Statistics
{
9 private static instance
: Statistics
;
10 private _objName
: string;
11 private _commandsStatistics
: {
12 [command
: string]: CommandStatisticsData
15 private constructor() {
16 this._commandsStatistics
= {};
19 set
objName(objName
: string) {
20 this._objName
= objName
;
23 static getInstance(): Statistics
{
24 if (!Statistics
.instance
) {
25 Statistics
.instance
= new Statistics();
27 return Statistics
.instance
;
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
++;
36 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
37 this._commandsStatistics
[command
].countRequest
= 1;
40 case Constants
.OCPP_JSON_CALL_RESULT_MESSAGE
:
41 if (this._commandsStatistics
[command
]) {
42 if (this._commandsStatistics
[command
].countResponse
) {
43 this._commandsStatistics
[command
].countResponse
++;
45 this._commandsStatistics
[command
].countResponse
= 1;
48 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
49 this._commandsStatistics
[command
].countResponse
= 1;
52 case Constants
.OCPP_JSON_CALL_ERROR_MESSAGE
:
53 if (this._commandsStatistics
[command
]) {
54 if (this._commandsStatistics
[command
].countError
) {
55 this._commandsStatistics
[command
].countError
++;
57 this._commandsStatistics
[command
].countError
= 1;
60 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
61 this._commandsStatistics
[command
].countError
= 1;
65 logger
.error(`${this._logPrefix()} Wrong message type ${messageType}`);
70 addPerformanceTimer(command
: string, duration
: number): void {
71 // Map to proper command name
73 sendMeterValues
: 'MeterValues',
74 startTransaction
: 'StartTransaction',
75 stopTransaction
: 'StopTransaction',
77 if (MAPCOMMAND
[command
]) {
78 command
= MAPCOMMAND
[command
] as string;
80 // Initialize command statistics
81 if (!this._commandsStatistics
[command
]) {
82 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
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
;
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}`);
98 logger
.info(this._logPrefix() + ' %j', this._commandsStatistics
);
101 _displayInterval(): void {
102 if (Configuration
.getStatisticsDisplayInterval() > 0) {
105 }, Configuration
.getStatisticsDisplayInterval() * 1000);
106 logger
.info(this._logPrefix() + ' displayed every ' + Configuration
.getStatisticsDisplayInterval().toString() + 's');
111 this._displayInterval();
114 private _logPrefix(): string {
115 return Utils
.logPrefix(` ${this._objName} Statistics:`);