1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/client-metrics/service.js
2020-02-20 08:30:37 +01:00

35 lines
878 B
JavaScript

'use strict';
const POLL_INTERVAL = 10000;
const { EventEmitter } = require('events');
module.exports = class UnleashClientMetrics extends EventEmitter {
constructor (metricsDb) {
super();
this.db = metricsDb;
this.highestIdSeen = 0;
this.db.getMetricsLastHour().then(metrics => {
this.addMetrics(metrics);
this.startPoller();
});
}
addMetrics (metrics) {
if (metrics && metrics.length > 0) {
this.highestIdSeen = metrics[metrics.length - 1].id;
}
this.emit('metrics', metrics);
}
startPoller () {
setInterval(() => {
this.db.getNewMetrics(this.highestIdSeen)
.then(metrics => this.addMetrics(metrics));
}, POLL_INTERVAL).unref();
}
insert (metrics) {
return this.db.insert(metrics);
}
};