1 import Configuration
from './Configuration.js';
2 import Utils
from './Utils.js';
3 import logger
from './Logger.js';
5 export default class Statistics
{
12 set objName(objName
) {
13 this._objName
= objName
;
16 static getInstance() {
17 if (!Statistics
.instance
) {
18 Statistics
.instance
= new Statistics();
20 return Statistics
.instance
;
24 return Utils
.logPrefix(` ${this._objName} Statistics:`);
27 addMessage(command
, response
= false) {
29 if (this._statistics
[command
]) {
30 if (this._statistics
[command
].countResponse
) {
31 this._statistics
[command
].countResponse
++;
33 this._statistics
[command
].countResponse
= 1;
36 this._statistics
[command
] = {};
37 this._statistics
[command
].countResponse
= 1;
39 } else if (this._statistics
[command
] && this._statistics
[command
].count
) {
40 this._statistics
[command
].count
++;
42 this._statistics
[command
] = {};
43 this._statistics
[command
].count
= 1;
47 addPerformanceTimer(command
, duration
) {
48 let currentStatistics
;
49 // Map to proper command name
51 sendMeterValues
: 'MeterValues',
52 startTransaction
: 'StartTransaction',
53 stopTransaction
: 'StopTransaction',
55 // Get current command statistics
56 if (MAPCOMMAND
[command
]) {
57 currentStatistics
= this._statistics
[MAPCOMMAND
[command
]];
58 } else if (this._statistics
[command
]) {
59 currentStatistics
= this._statistics
[command
];
61 this._statistics
[command
] = {};
62 currentStatistics
= this._statistics
[command
];
65 if (currentStatistics
) {
66 // Update current statistics timers
67 currentStatistics
.countTime
= currentStatistics
.countTime
? currentStatistics
.countTime
+ 1 : 1;
68 currentStatistics
.minTime
= currentStatistics
.minTime
? (currentStatistics
.minTime
> duration
? duration
: currentStatistics
.minTime
) : duration
;
69 currentStatistics
.maxTime
= currentStatistics
.maxTime
? (currentStatistics
.maxTime
< duration
? duration
: currentStatistics
.maxTime
) : duration
;
70 currentStatistics
.totalTime
= currentStatistics
.totalTime
? currentStatistics
.totalTime
+ duration
: duration
;
71 currentStatistics
.avgTime
= currentStatistics
.totalTime
/ currentStatistics
.countTime
;
75 logPerformance(entry
, className
) {
76 this.addPerformanceTimer(entry
.name
, entry
.duration
);
77 logger
.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`);
81 logger
.info(this._logPrefix() + ' %j', this._statistics
);
85 if (Configuration
.getStatisticsDisplayInterval() > 0) {
88 }, Configuration
.getStatisticsDisplayInterval() * 1000);
89 logger
.info(this._logPrefix() + ' displayed every ' + Configuration
.getStatisticsDisplayInterval() + 's');
94 this._displayInterval();