d6174d96dea7a602fa19712816283e42a8a25701
1 import CommandStatistics
, { CommandStatisticsData
, PerfEntry
} from
'../types/CommandStatistics';
2 import { IncomingRequestCommand
, RequestCommand
} from
'../types/ocpp/1.6/Requests';
4 import CircularArray from
'./CircularArray';
5 import Configuration from
'./Configuration';
6 import { MessageType
} from
'../types/ocpp/MessageType';
7 import { PerformanceEntry
} from
'perf_hooks';
8 import Utils from
'./Utils';
9 import logger from
'./Logger';
11 export default class Statistics
{
12 private static instance
: Statistics
;
13 private _objName
: string;
14 private _commandsStatistics
: CommandStatistics
;
16 private constructor() {
17 this._commandsStatistics
= {} as CommandStatistics
;
20 set
objName(objName
: string) {
21 this._objName
= objName
;
24 static getInstance(): Statistics
{
25 if (!Statistics
.instance
) {
26 Statistics
.instance
= new Statistics();
28 return Statistics
.instance
;
31 addMessage(command
: RequestCommand
| IncomingRequestCommand
, messageType
: MessageType
): void {
32 switch (messageType
) {
33 case MessageType
.CALL_MESSAGE
:
34 if (this._commandsStatistics
[command
] && this._commandsStatistics
[command
].countRequest
) {
35 this._commandsStatistics
[command
].countRequest
++;
37 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
38 this._commandsStatistics
[command
].countRequest
= 1;
41 case MessageType
.CALL_RESULT_MESSAGE
:
42 if (this._commandsStatistics
[command
]) {
43 if (this._commandsStatistics
[command
].countResponse
) {
44 this._commandsStatistics
[command
].countResponse
++;
46 this._commandsStatistics
[command
].countResponse
= 1;
49 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
50 this._commandsStatistics
[command
].countResponse
= 1;
53 case MessageType
.CALL_ERROR_MESSAGE
:
54 if (this._commandsStatistics
[command
]) {
55 if (this._commandsStatistics
[command
].countError
) {
56 this._commandsStatistics
[command
].countError
++;
58 this._commandsStatistics
[command
].countError
= 1;
61 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
62 this._commandsStatistics
[command
].countError
= 1;
66 logger
.error(`${this._logPrefix()} Wrong message type ${messageType}`);
71 logPerformance(entry
: PerformanceEntry
, className
: string): void {
72 this.addPerformanceTimer(entry
.name
as RequestCommand
| IncomingRequestCommand
, entry
.duration
);
73 const perfEntry
: PerfEntry
= {} as PerfEntry
;
74 perfEntry
.name
= entry
.name
;
75 perfEntry
.entryType
= entry
.entryType
;
76 perfEntry
.startTime
= entry
.startTime
;
77 perfEntry
.duration
= entry
.duration
;
78 logger
.info(`${this._logPrefix()} object ${className} method(s) performance entry: %j`, perfEntry
);
82 this._displayInterval();
85 private _display(): void {
86 logger
.info(this._logPrefix() + ' %j', this._commandsStatistics
);
89 private _displayInterval(): void {
90 if (Configuration
.getStatisticsDisplayInterval() > 0) {
93 }, Configuration
.getStatisticsDisplayInterval() * 1000);
94 logger
.info(this._logPrefix() + ' displayed every ' + Utils
.secondsToHHMMSS(Configuration
.getStatisticsDisplayInterval()));
98 private median(dataSet
: number[]): number {
99 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
102 const sortedDataSet
= dataSet
.slice().sort();
103 const middleIndex
= Math.floor(sortedDataSet
.length
/ 2);
104 if (sortedDataSet
.length
% 2) {
105 return sortedDataSet
[middleIndex
/ 2];
107 return (sortedDataSet
[(middleIndex
- 1)] + sortedDataSet
[middleIndex
]) / 2;
110 private addPerformanceTimer(command
: RequestCommand
| IncomingRequestCommand
, duration
: number): void {
111 // Map to proper command name
113 sendMeterValues
: 'MeterValues',
114 startTransaction
: 'StartTransaction',
115 stopTransaction
: 'StopTransaction',
117 if (MAPCOMMAND
[command
]) {
118 command
= MAPCOMMAND
[command
] as RequestCommand
| IncomingRequestCommand
;
120 // Initialize command statistics
121 if (!this._commandsStatistics
[command
]) {
122 this._commandsStatistics
[command
] = {} as CommandStatisticsData
;
124 // Update current statistics timers
125 this._commandsStatistics
[command
].countTimeMeasurement
= this._commandsStatistics
[command
].countTimeMeasurement
? this._commandsStatistics
[command
].countTimeMeasurement
+ 1 : 1;
126 this._commandsStatistics
[command
].currentTimeMeasurement
= duration
;
127 this._commandsStatistics
[command
].minTimeMeasurement
= this._commandsStatistics
[command
].minTimeMeasurement
? (this._commandsStatistics
[command
].minTimeMeasurement
> duration
? duration
: this._commandsStatistics
[command
].minTimeMeasurement
) : duration
;
128 this._commandsStatistics
[command
].maxTimeMeasurement
= this._commandsStatistics
[command
].maxTimeMeasurement
? (this._commandsStatistics
[command
].maxTimeMeasurement
< duration
? duration
: this._commandsStatistics
[command
].maxTimeMeasurement
) : duration
;
129 this._commandsStatistics
[command
].totalTimeMeasurement
= this._commandsStatistics
[command
].totalTimeMeasurement
? this._commandsStatistics
[command
].totalTimeMeasurement
+ duration
: duration
;
130 this._commandsStatistics
[command
].avgTimeMeasurement
= this._commandsStatistics
[command
].totalTimeMeasurement
/ this._commandsStatistics
[command
].countTimeMeasurement
;
131 Array.isArray(this._commandsStatistics
[command
].timeMeasurementSeries
) ? this._commandsStatistics
[command
].timeMeasurementSeries
.push(duration
) : this._commandsStatistics
[command
].timeMeasurementSeries
= [duration
] as CircularArray
<number>;
132 this._commandsStatistics
[command
].medTimeMeasurement
= this.median(this._commandsStatistics
[command
].timeMeasurementSeries
);
135 private _logPrefix(): string {
136 return Utils
.logPrefix(` ${this._objName} Statistics:`);