| 1 | const Configuration = require('./Configuration'); |
| 2 | const logger = require('./Logger'); |
| 3 | const Utils = require('./Utils'); |
| 4 | |
| 5 | class Statistics { |
| 6 | constructor(objName) { |
| 7 | this._objName = objName; |
| 8 | this._statistics = {}; |
| 9 | } |
| 10 | |
| 11 | _basicFormatLog() { |
| 12 | return Utils.basicFormatLog(` ${this._objName} Statistics:`); |
| 13 | } |
| 14 | |
| 15 | addMessage(command, response = false) { |
| 16 | if (response) { |
| 17 | if (this._statistics[command]) { |
| 18 | if (this._statistics[command].countResponse) { |
| 19 | this._statistics[command].countResponse++; |
| 20 | } else { |
| 21 | this._statistics[command].countResponse = 1; |
| 22 | } |
| 23 | } else { |
| 24 | this._statistics[command] = {}; |
| 25 | this._statistics[command].countResponse = 1; |
| 26 | } |
| 27 | } else if (this._statistics[command] && this._statistics[command].count) { |
| 28 | this._statistics[command].count++; |
| 29 | } else { |
| 30 | this._statistics[command] = {}; |
| 31 | this._statistics[command].count = 1; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | addPerformanceTimer(command, duration) { |
| 36 | let currentStatistics; |
| 37 | // Map to proper command name |
| 38 | const MAPCOMMAND = { |
| 39 | sendMeterValues: 'MeterValues', |
| 40 | startTransaction: 'StartTransaction', |
| 41 | stopTransaction: 'StopTransaction', |
| 42 | }; |
| 43 | // Get current command statistics |
| 44 | if (MAPCOMMAND[command]) { |
| 45 | currentStatistics = this._statistics[MAPCOMMAND[command]]; |
| 46 | } else if (this._statistics[command]) { |
| 47 | currentStatistics = this._statistics[command]; |
| 48 | } else { |
| 49 | this._statistics[command] = {}; |
| 50 | currentStatistics = this._statistics[command]; |
| 51 | } |
| 52 | |
| 53 | if (currentStatistics) { |
| 54 | // Update current statistics timers |
| 55 | currentStatistics.countTime = currentStatistics.countTime ? currentStatistics.countTime + 1 : 1; |
| 56 | currentStatistics.minTime = currentStatistics.minTime ? (currentStatistics.minTime > duration ? duration : currentStatistics.minTime) : duration; |
| 57 | currentStatistics.maxTime = currentStatistics.maxTime ? (currentStatistics.maxTime < duration ? duration : currentStatistics.maxTime) : duration; |
| 58 | currentStatistics.totalTime = currentStatistics.totalTime ? currentStatistics.totalTime + duration : duration; |
| 59 | currentStatistics.avgTime = currentStatistics.totalTime / currentStatistics.countTime; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | logPerformance(entry, className) { |
| 64 | this.addPerformanceTimer(entry.name, entry.duration); |
| 65 | logger.info(`${this._basicFormatLog()} class->${className}, method->${entry.name}, duration->${entry.duration}`); |
| 66 | } |
| 67 | |
| 68 | _display() { |
| 69 | logger.info(this._basicFormatLog() + ' %j', this._statistics); |
| 70 | } |
| 71 | |
| 72 | _displayInterval() { |
| 73 | if (Configuration.getStatisticsDisplayInterval() > 0) { |
| 74 | setInterval(() => { |
| 75 | this._display(); |
| 76 | }, Configuration.getStatisticsDisplayInterval() * 1000); |
| 77 | logger.info(this._basicFormatLog() + ' displayed every ' + Configuration.getStatisticsDisplayInterval() + 's'); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | async start() { |
| 82 | this._displayInterval(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | module.exports = Statistics; |