1 import Configuration from
'./Configuration';
2 import Constants from
'./Constants';
3 import Utils from
'./Utils';
4 import logger from
'./Logger';
6 export default class Statistics
{
7 private static instance
: Statistics
;
9 private _objName
: string;
11 private constructor() {
12 this._statistics
= {};
15 set
objName(objName
: string) {
16 this._objName
= objName
;
19 static getInstance(): Statistics
{
20 if (!Statistics
.instance
) {
21 Statistics
.instance
= new Statistics();
23 return Statistics
.instance
;
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
].countRequest
) {
30 this._statistics
[command
].countRequest
++;
32 this._statistics
[command
] = {};
33 this._statistics
[command
].countRequest
= 1;
36 case Constants
.OCPP_JSON_CALL_RESULT_MESSAGE
:
37 if (this._statistics
[command
]) {
38 if (this._statistics
[command
].countResponse
) {
39 this._statistics
[command
].countResponse
++;
41 this._statistics
[command
].countResponse
= 1;
44 this._statistics
[command
] = {};
45 this._statistics
[command
].countResponse
= 1;
48 case Constants
.OCPP_JSON_CALL_ERROR_MESSAGE
:
49 if (this._statistics
[command
]) {
50 if (this._statistics
[command
].countError
) {
51 this._statistics
[command
].countError
++;
53 this._statistics
[command
].countError
= 1;
56 this._statistics
[command
] = {};
57 this._statistics
[command
].countError
= 1;
61 logger
.error(`${this._logPrefix()} Wrong message type ${messageType}`);
66 addPerformanceTimer(command
: string, duration
: number): void {
67 // Map to proper command name
69 sendMeterValues
: 'MeterValues',
70 startTransaction
: 'StartTransaction',
71 stopTransaction
: 'StopTransaction',
73 if (MAPCOMMAND
[command
]) {
74 command
= MAPCOMMAND
[command
];
76 // Initialize command statistics
77 if (!this._statistics
[command
]) {
78 this._statistics
[command
] = {};
80 // Update current statistics timers
81 this._statistics
[command
].countTime
= this._statistics
[command
].countTime
? this._statistics
[command
].countTime
+ 1 : 1;
82 this._statistics
[command
].minTime
= this._statistics
[command
].minTime
? (this._statistics
[command
].minTime
> duration
? duration
: this._statistics
[command
].minTime
) : duration
;
83 this._statistics
[command
].maxTime
= this._statistics
[command
].maxTime
? (this._statistics
[command
].maxTime
< duration
? duration
: this._statistics
[command
].maxTime
) : duration
;
84 this._statistics
[command
].totalTime
= this._statistics
[command
].totalTime
? this._statistics
[command
].totalTime
+ duration
: duration
;
85 this._statistics
[command
].avgTime
= this._statistics
[command
].totalTime
/ this._statistics
[command
].countTime
;
88 logPerformance(entry
, className
: string): void {
89 this.addPerformanceTimer(entry
.name
, entry
.duration
);
90 logger
.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`);
94 logger
.info(this._logPrefix() + ' %j', this._statistics
);
97 _displayInterval(): void {
98 if (Configuration
.getStatisticsDisplayInterval() > 0) {
101 }, Configuration
.getStatisticsDisplayInterval() * 1000);
102 logger
.info(this._logPrefix() + ' displayed every ' + Configuration
.getStatisticsDisplayInterval() + 's');
107 this._displayInterval();
110 private _logPrefix(): string {
111 return Utils
.logPrefix(` ${this._objName} Statistics:`);