diff --git a/packages/unleash-api/lib/db/client-strategies.js b/packages/unleash-api/lib/db/client-strategies.js new file mode 100644 index 0000000000..ffe8d7a55c --- /dev/null +++ b/packages/unleash-api/lib/db/client-strategies.js @@ -0,0 +1,48 @@ +'use strict'; + +const COLUMNS = ['app_name', 'strategies']; +const TABLE = 'client_strategies'; + +module.exports = function (db) { + function update (appName, strategies) { + return db(TABLE) + .where('app_name', appName) // eslint-disable-line + .update({ + strategies: JSON.stringify(strategies), + updated_at: 'now()', // eslint-disable-line + }); + } + + function insert (appName, strategies) { + return db(TABLE).insert({ + app_name: appName, // eslint-disable-line + strategies: JSON.stringify(strategies), + }); + } + + function insertOrUpdate (appName, strategies) { + return db(TABLE) + .count('*') + .where('app_name', appName) + .map(row => ({ count: row.count })) + .then(rows => { + return rows[0].count > 0 ? update(appName, strategies) : insert(appName, strategies); + }); + } + + function getAll () { + return db + .select(COLUMNS) + .from(TABLE) + .map(mapRow); + } + + function mapRow (row) { + return { + appName: row.app_name, + strategies: row.strategies, + }; + } + + return { insertOrUpdate, getAll }; +}; diff --git a/packages/unleash-api/lib/routes/metrics.js b/packages/unleash-api/lib/routes/metrics.js index d0890c3585..8fc16a80d1 100644 --- a/packages/unleash-api/lib/routes/metrics.js +++ b/packages/unleash-api/lib/routes/metrics.js @@ -5,10 +5,14 @@ const ClientMetrics = require('../client-metrics'); const ClientMetricsService = require('../client-metrics-service'); module.exports = function (app, config) { - const metricsDb = config.metricsDb; + const { metricsDb, clientStrategiesDb } = config; const metrics = new ClientMetrics(); const service = new ClientMetricsService(metricsDb); + // Just som dummo demo data + clientStrategiesDb.insertOrUpdate('demo-app', ['default', 'test']).then(() => console.log('inserted client_strategies')); + + service.on('metrics', (entries) => { entries.forEach((m) => metrics.addPayload(m.metrics)); }); @@ -43,4 +47,8 @@ module.exports = function (app, config) { res.end(); }); + + app.get('/client/strategies', (req, res) => { + clientStrategiesDb.getAll().then(data => res.json(data)); + }); }; diff --git a/packages/unleash-api/migrations/20161102212415-create-client-strategies.js b/packages/unleash-api/migrations/20161102212415-create-client-strategies.js new file mode 100644 index 0000000000..6a6ef7f26a --- /dev/null +++ b/packages/unleash-api/migrations/20161102212415-create-client-strategies.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('../scripts/migration-runner').create('009-create-client-strategies'); diff --git a/packages/unleash-api/migrations/sql/009-create-client-strategies.down.sql b/packages/unleash-api/migrations/sql/009-create-client-strategies.down.sql new file mode 100644 index 0000000000..85f83c3d35 --- /dev/null +++ b/packages/unleash-api/migrations/sql/009-create-client-strategies.down.sql @@ -0,0 +1,2 @@ +--create client_strategies +DROP TABLE client_strategies; diff --git a/packages/unleash-api/migrations/sql/009-create-client-strategies.up.sql b/packages/unleash-api/migrations/sql/009-create-client-strategies.up.sql new file mode 100644 index 0000000000..27e412b957 --- /dev/null +++ b/packages/unleash-api/migrations/sql/009-create-client-strategies.up.sql @@ -0,0 +1,6 @@ +--create new client_strategies table +CREATE TABLE client_strategies ( + app_name varchar(255) PRIMARY KEY NOT NULL, + updated_at timestamp default now(), + strategies json +); diff --git a/packages/unleash-api/server-impl.js b/packages/unleash-api/server-impl.js index 7bf4878c2d..3ae6def4d7 100644 --- a/packages/unleash-api/server-impl.js +++ b/packages/unleash-api/server-impl.js @@ -19,6 +19,7 @@ function createApp (options) { const featureDb = require('./lib/db/feature')(db, eventStore); const strategyDb = require('./lib/db/strategy')(db, eventStore); const metricsDb = require('./lib/db/metrics')(db); + const clientStrategiesDb = require('./lib/db/client-strategies')(db); const config = { baseUriPath: options.baseUriPath, @@ -30,6 +31,7 @@ function createApp (options) { featureDb, strategyDb, metricsDb, + clientStrategiesDb, }; const app = require('./app')(config);