mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
e55378e1c4
Impelements: - http://unleash.host.com/api/client/seen-toggles - http://unleash.host.com/api/metrics/feature-toggles - http://localhost:4242/api/client/applications - http://localhost:4242/api/client/applications/:appName
55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const logger = require('../logger');
|
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
const TEN_SECONDS = 10 * 1000;
|
|
|
|
class ClientMetricsStore extends EventEmitter {
|
|
|
|
constructor (metricsDb, pollInterval = TEN_SECONDS) {
|
|
super();
|
|
this.metricsDb = metricsDb;
|
|
this.highestIdSeen = 0;
|
|
this.timer;
|
|
|
|
//Build internal state
|
|
metricsDb.getMetricsLastHour()
|
|
.then((metrics) => this._emitMetrics(metrics))
|
|
.then(() => this._startPoller(pollInterval))
|
|
.then(() => this.emit('ready'))
|
|
.catch((err) => logger.error(err));
|
|
}
|
|
|
|
_startPoller (pollInterval) {
|
|
this.timer = setInterval(() => this._fetchNewAndEmit(), pollInterval);
|
|
this.timer.unref();
|
|
}
|
|
|
|
_fetchNewAndEmit() {
|
|
this.metricsDb.getNewMetrics(this.highestIdSeen)
|
|
.then((metrics) => this._emitMetrics(metrics))
|
|
}
|
|
|
|
_emitMetrics (metrics) {
|
|
if (metrics && metrics.length > 0) {
|
|
this.highestIdSeen = metrics[metrics.length - 1].id;
|
|
metrics.forEach(m => this.emit('metrics', m.metrics));
|
|
}
|
|
}
|
|
|
|
// Insert new client metrics
|
|
insert (metrics) {
|
|
return this.metricsDb.insert(metrics)
|
|
}
|
|
|
|
destroy () {
|
|
try {
|
|
clearInterval(this.timer);
|
|
} catch (e) {}
|
|
}
|
|
};
|
|
|
|
module.exports = ClientMetricsStore;
|