mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			45 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| const POLL_INTERVAL = 10000;
 | |
| const { EventEmitter } = require('events');
 | |
| 
 | |
| module.exports = class UnleashClientMetricsService extends EventEmitter {
 | |
|     constructor (metricsDb) {
 | |
|         super();
 | |
|         this.metricsDb = metricsDb;
 | |
|         this.metrics = [];
 | |
|         this.highestIdSeen = 0;
 | |
|         this.fetch();
 | |
|     }
 | |
| 
 | |
|     fetch () {
 | |
|         return this.metricsDb
 | |
|             .getNewMetrics(this.highestIdSeen)
 | |
|             .then(metrics => {
 | |
|                 this.startTimer();
 | |
|                 this.addMetrics(metrics);
 | |
|                 return metrics;
 | |
|             });
 | |
|     }
 | |
| 
 | |
|     addMetrics (metrics) {
 | |
|         metrics.forEach(m => this.metrics.push(m));
 | |
|         if (this.metrics && this.metrics.length > 0) {
 | |
|             this.highestIdSeen = this.metrics[this.metrics.length - 1].id;
 | |
|         }
 | |
|         this.emit('metrics', metrics);
 | |
|     }
 | |
| 
 | |
|     startTimer () {
 | |
|         setInterval(() => this.fetch(), POLL_INTERVAL).unref();
 | |
|     }
 | |
| 
 | |
|     getMetrics () {
 | |
|         return this.metrics;
 | |
|     }
 | |
| 
 | |
|     insert (metrics) {
 | |
|         return this.metricsDb.insert(metrics);
 | |
|     }
 | |
| };
 |