1 import CommandStatistics
, { CommandStatisticsData
, PerfEntry
} 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 const perfEntry
: PerfEntry
= {} as PerfEntry
;
72 perfEntry
.name
= entry
.name
;
73 perfEntry
.entryType
= entry
.entryType
;
74 perfEntry
.startTime
= entry
.startTime
;
75 perfEntry
.duration
= entry
.duration
;
76 logger
.info(`${this._logPrefix()} object ${className} method performance entry: %j`, perfEntry
);
80 logger
.info(this._logPrefix() + ' %j', this._commandsStatistics
);
83 _displayInterval(): void {
84 if (Configuration
.getStatisticsDisplayInterval() > 0) {
87 }, Configuration
.getStatisticsDisplayInterval() * 1000);
88 logger
.info(this._logPrefix() + ' displayed every ' + Utils
.secondsToHHMMSS(Configuration
.getStatisticsDisplayInterval()));
93 this._displayInterval();
96 private median(dataSet
: number[]): number {
97 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
100 const sortedDataSet
= dataSet
.slice().sort();
101 const middleIndex
= Math.floor(sortedDataSet
.length
/ 2);
102 if (sortedDataSet
.length
% 2) {
103 return sortedDataSet
[middleIndex
/ 2];
105 return (sortedDataSet
[(middleIndex
- 1)] + sortedDataSet
[middleIndex
]) / 2;
108 private addPerformanceTimer(command
: string, duration
: number): void {
109 // Map to proper command name
111 sendMeterValues
: 'MeterValues',
112 startTransaction
: 'StartTransaction',
113 stopTransaction
: 'StopTransaction',
115 if (MAPCOMMAND
[command
]) {
116 command
= MAPCOMMAND
[command
] as string;
118 // Initialize command statistics
119 if (!this._commandsStatistics
[command
]) {
120 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
122 // Update current statistics timers
123 this._commandsStatistics
[command
].countTimeMeasurement
= this._commandsStatistics
[command
].countTimeMeasurement
? this._commandsStatistics
[command
].countTimeMeasurement
+ 1 : 1;
124 this._commandsStatistics
[command
].currentTimeMeasurement
= duration
;
125 this._commandsStatistics
[command
].minTimeMeasurement
= this._commandsStatistics
[command
].minTimeMeasurement
? (this._commandsStatistics
[command
].minTimeMeasurement
> duration
? duration
: this._commandsStatistics
[command
].minTimeMeasurement
) : duration
;
126 this._commandsStatistics
[command
].maxTimeMeasurement
= this._commandsStatistics
[command
].maxTimeMeasurement
? (this._commandsStatistics
[command
].maxTimeMeasurement
< duration
? duration
: this._commandsStatistics
[command
].maxTimeMeasurement
) : duration
;
127 this._commandsStatistics
[command
].totalTimeMeasurement
= this._commandsStatistics
[command
].totalTimeMeasurement
? this._commandsStatistics
[command
].totalTimeMeasurement
+ duration
: duration
;
128 this._commandsStatistics
[command
].avgTimeMeasurement
= this._commandsStatistics
[command
].totalTimeMeasurement
/ this._commandsStatistics
[command
].countTimeMeasurement
;
129 Array.isArray(this._commandsStatistics
[command
].timeMeasurementSeries
) ? this._commandsStatistics
[command
].timeMeasurementSeries
.push(duration
) : this._commandsStatistics
[command
].timeMeasurementSeries
= [duration
];
130 this._commandsStatistics
[command
].medTimeMeasurement
= this.median(this._commandsStatistics
[command
].timeMeasurementSeries
);
133 private _logPrefix(): string {
134 return Utils
.logPrefix(` ${this._objName} Statistics:`);