1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/packages/unleash-api/lib/client-metrics-service.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-10-27 16:55:38 +02:00
'use strict';
2016-10-27 20:51:39 +02:00
2016-10-27 16:55:38 +02:00
const POLL_INTERVAL = 10000;
2016-11-02 12:49:25 +01:00
const { EventEmitter } = require('events');
2016-10-27 16:55:38 +02:00
2016-11-02 12:49:25 +01:00
module.exports = class UnleashClientMetricsService extends EventEmitter {
2016-10-27 16:55:38 +02:00
constructor (metricsDb) {
2016-11-02 12:49:25 +01:00
super();
2016-10-27 16:55:38 +02:00
this.metricsDb = metricsDb;
this.metrics = [];
this.highestIdSeen = 0;
2016-11-02 12:49:25 +01:00
this.fetch();
}
fetch () {
return this.metricsDb
.getNewMetrics(this.highestIdSeen)
.then(metrics => {
this.startTimer();
this.addMetrics(metrics);
return metrics;
});
2016-10-27 16:55:38 +02:00
}
addMetrics (metrics) {
metrics.forEach(m => this.metrics.push(m));
2016-10-27 20:51:39 +02:00
if (this.metrics && this.metrics.length > 0) {
this.highestIdSeen = this.metrics[this.metrics.length - 1].id;
}
2016-11-02 12:49:25 +01:00
this.emit('metrics', metrics);
2016-10-27 16:55:38 +02:00
}
2016-11-02 12:49:25 +01:00
startTimer () {
setInterval(() => this.fetch(), POLL_INTERVAL).unref();
2016-10-27 16:55:38 +02:00
}
getMetrics () {
return this.metrics;
}
insert (metrics) {
2016-11-02 12:49:25 +01:00
return this.metricsDb.insert(metrics);
2016-10-27 16:55:38 +02:00
}
};