diff --git a/packages/unleash-api/lib/db/client-metrics-store.js b/packages/unleash-api/lib/db/client-metrics-store.js new file mode 100644 index 0000000000..36251d65d8 --- /dev/null +++ b/packages/unleash-api/lib/db/client-metrics-store.js @@ -0,0 +1,57 @@ +'use strict'; + +const logger = require('../logger'); +const METRICS_COLUMNS = ['id', 'created_at', 'metrics']; +const TABLE = 'client_metrics'; + +const mapRow = (row) => ({ + id: row.id, + createdAt: row.created_at, + metrics: row.metrics, +}); + +class ClientMetricsStore { + + constructor (db) { + this.db = db; + this._removeMetricsOlderThanOneHour(); + + setInterval(() => this._removeMetricsOlderThanOneHour(), 60 * 60 * 1000).unref(); + } + + _removeMetricsOlderThanOneHour () { + this.db(TABLE) + .whereRaw('created_at < now() - interval \'1 hour\'') + .del() + .then((res) => logger.info(`Delted ${res} metrics`)); + } + + // Insert new client metrics + insert (metrics) { + return this.db(TABLE).insert({ metrics }); + } + + // Used at startup to load all metrics last week into memory! + getMetricsLastHour () { + return this.db + .select(METRICS_COLUMNS) + .from(TABLE) + .limit(2000) + .whereRaw('created_at > now() - interval \'1 hour\'') + .orderBy('created_at', 'asc') + .map(mapRow); + } + + // Used to poll for new metrics + getNewMetrics (lastKnownId) { + return this.db + .select(METRICS_COLUMNS) + .from(TABLE) + .limit(1000) + .where('id', '>', lastKnownId) + .orderBy('created_at', 'asc') + .map(mapRow); + } +}; + +module.exports = ClientMetricsStore; diff --git a/packages/unleash-api/lib/db/client-metrics.js b/packages/unleash-api/lib/db/client-metrics.js deleted file mode 100644 index 760dc17208..0000000000 --- a/packages/unleash-api/lib/db/client-metrics.js +++ /dev/null @@ -1,43 +0,0 @@ -'use strict'; - -const METRICS_COLUMNS = ['id', 'created_at', 'metrics']; -const TABLE = 'client_metrics'; - -module.exports = function (db) { - // Insert new client metrics - function insert (metrics) { - return db(TABLE).insert({ metrics }); - } - - // Used at startup to load all metrics last week into memory! - function getMetricsLastHour () { - return db - .select(METRICS_COLUMNS) - .from(TABLE) - .limit(2000) - .whereRaw('created_at > now() - interval \'1 hour\'') - .orderBy('created_at', 'asc') - .map(mapRow); - } - - // Used to poll for new metrics - function getNewMetrics (lastKnownId) { - return db - .select(METRICS_COLUMNS) - .from(TABLE) - .limit(1000) - .where('id', '>', lastKnownId) - .orderBy('created_at', 'asc') - .map(mapRow); - } - - function mapRow (row) { - return { - id: row.id, - createdAt: row.created_at, - metrics: row.metrics, - }; - } - - return { insert, getMetricsLastHour, getNewMetrics }; -}; diff --git a/packages/unleash-api/lib/db/index.js b/packages/unleash-api/lib/db/index.js index cd5ff06811..9b97acdf9c 100644 --- a/packages/unleash-api/lib/db/index.js +++ b/packages/unleash-api/lib/db/index.js @@ -4,7 +4,7 @@ const EventStore = require('./event-store'); const FeatureToggleStore = require('./feature-toggle-store'); const StrategyStore = require('./strategy-store'); const clientInstancesDbCreator = require('./client-instances'); -const clientMetricsDbCreator = require('./client-metrics'); +const ClientMetricsStore = require('./client-metrics-store'); const clientStrategiesDbCreator = require('./client-strategies'); module.exports = (db) => { @@ -15,7 +15,7 @@ module.exports = (db) => { featureToggleStore: new FeatureToggleStore(db, eventStore), strategyStore: new StrategyStore(db, eventStore), clientInstancesDb: clientInstancesDbCreator(db), - clientMetricsDb: clientMetricsDbCreator(db), + clientMetricsStore: new ClientMetricsStore(db), clientStrategiesDb: clientStrategiesDbCreator(db), }; }; diff --git a/packages/unleash-api/lib/routes/metrics.js b/packages/unleash-api/lib/routes/metrics.js index 294ca2af8a..f480ee49f3 100644 --- a/packages/unleash-api/lib/routes/metrics.js +++ b/packages/unleash-api/lib/routes/metrics.js @@ -6,12 +6,12 @@ const ClientMetricsService = require('../client-metrics/service'); module.exports = function (app, config) { const { - clientMetricsDb, + clientMetricsStore, clientStrategiesDb, clientInstancesDb, } = config; const metrics = new ClientMetrics(); - const service = new ClientMetricsService(clientMetricsDb); + const service = new ClientMetricsService(clientMetricsStore); service.on('metrics', (entries) => { entries.forEach((m) => { diff --git a/packages/unleash-api/server-impl.js b/packages/unleash-api/server-impl.js index 1611329480..95ee9e484c 100644 --- a/packages/unleash-api/server-impl.js +++ b/packages/unleash-api/server-impl.js @@ -18,7 +18,7 @@ function createApp (options) { featureToggleStore, strategyStore, clientInstancesDb, - clientMetricsDb, + clientMetricsStore, clientStrategiesDb, } = require('./lib/db')(db); @@ -30,7 +30,7 @@ function createApp (options) { eventStore, featureToggleStore, strategyStore, - clientMetricsDb, + clientMetricsStore, clientStrategiesDb, clientInstancesDb, }; diff --git a/packages/unleash-api/test/e2e/test-helper.js b/packages/unleash-api/test/e2e/test-helper.js index b5337df1b9..f85efcaad5 100644 --- a/packages/unleash-api/test/e2e/test-helper.js +++ b/packages/unleash-api/test/e2e/test-helper.js @@ -12,7 +12,7 @@ const { strategyStore, clientInstancesDb, clientStrategiesDb, - clientMetricsDb, + clientMetricsStore, } = require('../../lib/db')(knex); @@ -24,7 +24,7 @@ const app = require('../../app')({ strategyStore, clientStrategiesDb, clientInstancesDb, - clientMetricsDb, + clientMetricsStore, }); BPromise.promisifyAll(request);