From cd5c2e3914a93cb42b82a8dab4c84088e5068cbc Mon Sep 17 00:00:00 2001 From: sveisvei Date: Fri, 4 Nov 2016 16:17:11 +0100 Subject: [PATCH] remove internal storage --- .../unleash-api/lib/client-metrics-service.js | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/packages/unleash-api/lib/client-metrics-service.js b/packages/unleash-api/lib/client-metrics-service.js index 4ffec9bd95..e60a88a685 100644 --- a/packages/unleash-api/lib/client-metrics-service.js +++ b/packages/unleash-api/lib/client-metrics-service.js @@ -3,42 +3,32 @@ const POLL_INTERVAL = 10000; const { EventEmitter } = require('events'); -module.exports = class UnleashClientMetricsService extends EventEmitter { +module.exports = class UnleashClientMetrics extends EventEmitter { constructor (metricsDb) { super(); - this.metricsDb = metricsDb; - this.metrics = []; + this.db = metricsDb; this.highestIdSeen = 0; - this.fetch(); - } - - fetch () { - return this.metricsDb - .getNewMetrics(this.highestIdSeen) - .then(metrics => { - this.startTimer(); - this.addMetrics(metrics); - return metrics; - }); + this.db.getMetricsLastWeek().then(metrics => { + this.addMetrics(metrics); + this.startPoller(); + }); } addMetrics (metrics) { - metrics.forEach(m => this.metrics.push(m)); - if (this.metrics && this.metrics.length > 0) { - this.highestIdSeen = this.metrics[this.metrics.length - 1].id; + if (metrics && metrics.length > 0) { + this.highestIdSeen = metrics[metrics.length - 1].id; } this.emit('metrics', metrics); } - startTimer () { - setInterval(() => this.fetch(), POLL_INTERVAL).unref(); - } - - getMetrics () { - return this.metrics; + startPoller () { + setInterval(() => { + this.db.getNewMetrics(this.highestIdSeen) + .then(metrics => this.addMetrics(metrics)); + }, POLL_INTERVAL).unref(); } insert (metrics) { - return this.metricsDb.insert(metrics); + return this.db.insert(metrics); } };