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

35 lines
878 B
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-04 16:17:11 +01:00
module.exports = class UnleashClientMetrics extends EventEmitter {
2016-10-27 16:55:38 +02:00
constructor (metricsDb) {
2016-11-02 12:49:25 +01:00
super();
2016-11-04 16:17:11 +01:00
this.db = metricsDb;
2016-10-27 16:55:38 +02:00
this.highestIdSeen = 0;
2016-11-04 22:14:58 +01:00
this.db.getMetricsLastHour().then(metrics => {
2016-11-04 16:17:11 +01:00
this.addMetrics(metrics);
this.startPoller();
});
2016-10-27 16:55:38 +02:00
}
addMetrics (metrics) {
2016-11-04 16:17:11 +01:00
if (metrics && metrics.length > 0) {
this.highestIdSeen = metrics[metrics.length - 1].id;
2016-10-27 20:51:39 +02:00
}
2016-11-02 12:49:25 +01:00
this.emit('metrics', metrics);
2016-10-27 16:55:38 +02:00
}
2016-11-04 16:17:11 +01:00
startPoller () {
setInterval(() => {
this.db.getNewMetrics(this.highestIdSeen)
.then(metrics => this.addMetrics(metrics));
}, POLL_INTERVAL).unref();
2016-10-27 16:55:38 +02:00
}
insert (metrics) {
2016-11-04 16:17:11 +01:00
return this.db.insert(metrics);
2016-10-27 16:55:38 +02:00
}
};