mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-06 00:07:44 +01:00
35 lines
907 B
JavaScript
35 lines
907 B
JavaScript
|
'use strict';
|
||
|
const POLL_INTERVAL = 10000;
|
||
|
|
||
|
module.exports = class UnleashClientMetrics {
|
||
|
constructor (metricsDb) {
|
||
|
this.metricsDb = metricsDb;
|
||
|
this.metrics = [];
|
||
|
this.highestIdSeen = 0;
|
||
|
metricsDb.getMetricsLastWeek().then(metrics => {
|
||
|
this.addMetrics(metrics);
|
||
|
this.startPoller();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
addMetrics (metrics) {
|
||
|
metrics.forEach(m => this.metrics.push(m));
|
||
|
this.highestIdSeen = this.metrics[this.metrics.length - 1].id;
|
||
|
}
|
||
|
|
||
|
startPoller () {
|
||
|
setInterval(() => {
|
||
|
this.metricsDb.getNewMetrics(this.highestIdSeen)
|
||
|
.then(metrics => this.addMetrics(metrics));
|
||
|
}, POLL_INTERVAL).unref();
|
||
|
}
|
||
|
|
||
|
getMetrics () {
|
||
|
return this.metrics;
|
||
|
}
|
||
|
|
||
|
insert (metrics) {
|
||
|
this.metricsDb.insert(metrics).then(() => console.log('new metrics inserted!'));
|
||
|
}
|
||
|
};
|