From dc29680067e4185c11fdc6b9c7e0d6ca7c5bfef7 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Fri, 4 Nov 2016 09:03:13 +0100 Subject: [PATCH] client-instances WIP --- .../unleash-api/lib/db/client-instances.js | 60 +++++++++++++++++++ .../lib/db/{metrics.js => client-metrics.js} | 0 packages/unleash-api/lib/db/index.js | 10 ++++ packages/unleash-api/lib/routes/metrics.js | 4 +- .../20161104074441-create-client-instances.js | 3 + .../sql/010-create-client-instances.down.sql | 1 + .../sql/010-create-client-instances.up.sql | 7 +++ packages/unleash-api/server-impl.js | 6 +- packages/unleash-api/test/e2e/test-helper.js | 6 +- 9 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 packages/unleash-api/lib/db/client-instances.js rename packages/unleash-api/lib/db/{metrics.js => client-metrics.js} (100%) create mode 100644 packages/unleash-api/lib/db/index.js create mode 100644 packages/unleash-api/migrations/20161104074441-create-client-instances.js create mode 100644 packages/unleash-api/migrations/sql/010-create-client-instances.down.sql create mode 100644 packages/unleash-api/migrations/sql/010-create-client-instances.up.sql diff --git a/packages/unleash-api/lib/db/client-instances.js b/packages/unleash-api/lib/db/client-instances.js new file mode 100644 index 0000000000..6cbab6ccdf --- /dev/null +++ b/packages/unleash-api/lib/db/client-instances.js @@ -0,0 +1,60 @@ +/* eslint camelcase: "off" */ +'use strict'; + +const COLUMNS = ['app_name', 'instance_id', 'client_ip', 'last_seen', 'created_at']; +const TABLE = 'client_instances'; + +module.exports = function (db) { + function updateRow (details) { + return db(TABLE) + .where('app_name', details.appName) + .where('instance_id', details.instanceId) + .where('client_ip', details.clientIp) + .update({ + last_seen: 'now()', + }); + } + + function insertNewRow (details) { + return db(TABLE).insert({ + app_name: details.appName, + instance_id: details.instanceId, + client_ip: details.clientIp, + }); + } + + function insert (details) { + return db(TABLE) + .count('*') + .where('app_name', details.appName) + .where('instance_id', details.instanceId) + .where('client_ip', details.clientIp) + .map(row => ({ count: row.count })) + .then(rows => { + if (rows[0].count > 0) { + return updateRow(details); + } else { + return insertNewRow(details); + } + }); + } + + function getAll () { + return db + .select(COLUMNS) + .from(TABLE) + .map(mapRow); + } + + function mapRow (row) { + return { + appName: row.app_name, + instanceId: row.instance_id, + clientIp: row.client_ip, + lastSeen: row.last_seen, + createdAt: row.created_at, + }; + } + + return { insert, getAll }; +}; diff --git a/packages/unleash-api/lib/db/metrics.js b/packages/unleash-api/lib/db/client-metrics.js similarity index 100% rename from packages/unleash-api/lib/db/metrics.js rename to packages/unleash-api/lib/db/client-metrics.js diff --git a/packages/unleash-api/lib/db/index.js b/packages/unleash-api/lib/db/index.js new file mode 100644 index 0000000000..be1f5d4b1d --- /dev/null +++ b/packages/unleash-api/lib/db/index.js @@ -0,0 +1,10 @@ +'use strict'; +const clientInstancesDbCreator = require('./client-instances'); +const clientMetricsDbCreator = require('./client-metrics'); +const clientStrategiesDbCreator = require('./client-strategies'); + +module.exports = (db) => ({ + clientInstancesDb: clientInstancesDbCreator(db), + clientMetricsDb: clientMetricsDbCreator(db), + clientStrategiesDb: clientStrategiesDbCreator(db), +}); diff --git a/packages/unleash-api/lib/routes/metrics.js b/packages/unleash-api/lib/routes/metrics.js index 8fc16a80d1..b2194afd6e 100644 --- a/packages/unleash-api/lib/routes/metrics.js +++ b/packages/unleash-api/lib/routes/metrics.js @@ -5,9 +5,9 @@ const ClientMetrics = require('../client-metrics'); const ClientMetricsService = require('../client-metrics-service'); module.exports = function (app, config) { - const { metricsDb, clientStrategiesDb } = config; + const { clientMetricsDb, clientStrategiesDb } = config; const metrics = new ClientMetrics(); - const service = new ClientMetricsService(metricsDb); + const service = new ClientMetricsService(clientMetricsDb); // Just som dummo demo data clientStrategiesDb.insertOrUpdate('demo-app', ['default', 'test']).then(() => console.log('inserted client_strategies')); diff --git a/packages/unleash-api/migrations/20161104074441-create-client-instances.js b/packages/unleash-api/migrations/20161104074441-create-client-instances.js new file mode 100644 index 0000000000..039bb7f310 --- /dev/null +++ b/packages/unleash-api/migrations/20161104074441-create-client-instances.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('../scripts/migration-runner').create('010-create-client-instances'); diff --git a/packages/unleash-api/migrations/sql/010-create-client-instances.down.sql b/packages/unleash-api/migrations/sql/010-create-client-instances.down.sql new file mode 100644 index 0000000000..c113b5c4ce --- /dev/null +++ b/packages/unleash-api/migrations/sql/010-create-client-instances.down.sql @@ -0,0 +1 @@ +DROP TABLE client_instances; diff --git a/packages/unleash-api/migrations/sql/010-create-client-instances.up.sql b/packages/unleash-api/migrations/sql/010-create-client-instances.up.sql new file mode 100644 index 0000000000..f2c96e54a7 --- /dev/null +++ b/packages/unleash-api/migrations/sql/010-create-client-instances.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE client_instances ( + app_name varchar(255), + instance_id varchar(255), + client_ip varchar(255), + last_seen timestamp default now(), + created_at timestamp default now() +); diff --git a/packages/unleash-api/server-impl.js b/packages/unleash-api/server-impl.js index 3ae6def4d7..26cc7740cd 100644 --- a/packages/unleash-api/server-impl.js +++ b/packages/unleash-api/server-impl.js @@ -18,8 +18,7 @@ function createApp (options) { const eventStore = new EventStore(eventDb); 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 { clientInstancesDb, clientMetricsDb, clientStrategiesDb } = require('./lib/db')(db); const config = { baseUriPath: options.baseUriPath, @@ -30,8 +29,9 @@ function createApp (options) { eventStore, featureDb, strategyDb, - metricsDb, + clientMetricsDb, clientStrategiesDb, + clientInstancesDb, }; const app = require('./app')(config); diff --git a/packages/unleash-api/test/e2e/test-helper.js b/packages/unleash-api/test/e2e/test-helper.js index 43ffe551fc..92f77c145f 100644 --- a/packages/unleash-api/test/e2e/test-helper.js +++ b/packages/unleash-api/test/e2e/test-helper.js @@ -11,8 +11,7 @@ const EventStore = require('../../lib/event-store'); const eventStore = new EventStore(eventDb); const featureDb = require('../../lib/db/feature')(knex, eventStore); const strategyDb = require('../../lib/db/strategy')(knex, eventStore); -const metricsDb = require('../../lib/db/metrics')(knex); -const clientStrategiesDb = require('../../lib/db/client-strategies')(knex); +const { clientInstancesDb, clientStrategiesDb, clientMetricsDb } = require('../../lib/db')(knex); const app = require('../../app')({ @@ -22,8 +21,9 @@ const app = require('../../app')({ eventStore, featureDb, strategyDb, - metricsDb, clientStrategiesDb, + clientInstancesDb, + clientMetricsDb, }); BPromise.promisifyAll(request);