1 import Configuration from
'./Configuration';
2 import Utils from
'./Utils';
3 import logger from
'./Logger';
5 export default class Statistics
{
6 private static instance
: Statistics
;
8 private _objName
: string;
10 private constructor() {
11 this._statistics
= {};
14 set
objName(objName
: string) {
15 this._objName
= objName
;
18 static getInstance(): Statistics
{
19 if (!Statistics
.instance
) {
20 Statistics
.instance
= new Statistics();
22 return Statistics
.instance
;
25 addMessage(command
, response
= false) {
27 if (this._statistics
[command
]) {
28 if (this._statistics
[command
].countResponse
) {
29 this._statistics
[command
].countResponse
++;
31 this._statistics
[command
].countResponse
= 1;
34 this._statistics
[command
] = {};
35 this._statistics
[command
].countResponse
= 1;
37 } else if (this._statistics
[command
] && this._statistics
[command
].count
) {
38 this._statistics
[command
].count
++;
40 this._statistics
[command
] = {};
41 this._statistics
[command
].count
= 1;
45 addPerformanceTimer(command
, duration
) {
46 let currentStatistics
;
47 // Map to proper command name
49 sendMeterValues
: 'MeterValues',
50 startTransaction
: 'StartTransaction',
51 stopTransaction
: 'StopTransaction',
53 // Get current command statistics
54 if (MAPCOMMAND
[command
]) {
55 currentStatistics
= this._statistics
[MAPCOMMAND
[command
]];
56 } else if (this._statistics
[command
]) {
57 currentStatistics
= this._statistics
[command
];
59 this._statistics
[command
] = {};
60 currentStatistics
= this._statistics
[command
];
63 if (currentStatistics
) {
64 // Update current statistics timers
65 currentStatistics
.countTime
= currentStatistics
.countTime
? currentStatistics
.countTime
+ 1 : 1;
66 currentStatistics
.minTime
= currentStatistics
.minTime
? (currentStatistics
.minTime
> duration
? duration
: currentStatistics
.minTime
) : duration
;
67 currentStatistics
.maxTime
= currentStatistics
.maxTime
? (currentStatistics
.maxTime
< duration
? duration
: currentStatistics
.maxTime
) : duration
;
68 currentStatistics
.totalTime
= currentStatistics
.totalTime
? currentStatistics
.totalTime
+ duration
: duration
;
69 currentStatistics
.avgTime
= currentStatistics
.totalTime
/ currentStatistics
.countTime
;
73 logPerformance(entry
, className
: string): void {
74 this.addPerformanceTimer(entry
.name
, entry
.duration
);
75 logger
.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`);
79 logger
.info(this._logPrefix() + ' %j', this._statistics
);
82 _displayInterval(): void {
83 if (Configuration
.getStatisticsDisplayInterval() > 0) {
86 }, Configuration
.getStatisticsDisplayInterval() * 1000);
87 logger
.info(this._logPrefix() + ' displayed every ' + Configuration
.getStatisticsDisplayInterval() + 's');
92 this._displayInterval();
95 private _logPrefix(): string {
96 return Utils
.logPrefix(` ${this._objName} Statistics:`);