mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-06 00:07:44 +01:00
client-instances WIP
This commit is contained in:
parent
4f8f5f536b
commit
dc29680067
60
packages/unleash-api/lib/db/client-instances.js
Normal file
60
packages/unleash-api/lib/db/client-instances.js
Normal file
@ -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 };
|
||||||
|
};
|
10
packages/unleash-api/lib/db/index.js
Normal file
10
packages/unleash-api/lib/db/index.js
Normal file
@ -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),
|
||||||
|
});
|
@ -5,9 +5,9 @@ const ClientMetrics = require('../client-metrics');
|
|||||||
const ClientMetricsService = require('../client-metrics-service');
|
const ClientMetricsService = require('../client-metrics-service');
|
||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
const { metricsDb, clientStrategiesDb } = config;
|
const { clientMetricsDb, clientStrategiesDb } = config;
|
||||||
const metrics = new ClientMetrics();
|
const metrics = new ClientMetrics();
|
||||||
const service = new ClientMetricsService(metricsDb);
|
const service = new ClientMetricsService(clientMetricsDb);
|
||||||
|
|
||||||
// Just som dummo demo data
|
// Just som dummo demo data
|
||||||
clientStrategiesDb.insertOrUpdate('demo-app', ['default', 'test']).then(() => console.log('inserted client_strategies'));
|
clientStrategiesDb.insertOrUpdate('demo-app', ['default', 'test']).then(() => console.log('inserted client_strategies'));
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = require('../scripts/migration-runner').create('010-create-client-instances');
|
@ -0,0 +1 @@
|
|||||||
|
DROP TABLE client_instances;
|
@ -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()
|
||||||
|
);
|
@ -18,8 +18,7 @@ function createApp (options) {
|
|||||||
const eventStore = new EventStore(eventDb);
|
const eventStore = new EventStore(eventDb);
|
||||||
const featureDb = require('./lib/db/feature')(db, eventStore);
|
const featureDb = require('./lib/db/feature')(db, eventStore);
|
||||||
const strategyDb = require('./lib/db/strategy')(db, eventStore);
|
const strategyDb = require('./lib/db/strategy')(db, eventStore);
|
||||||
const metricsDb = require('./lib/db/metrics')(db);
|
const { clientInstancesDb, clientMetricsDb, clientStrategiesDb } = require('./lib/db')(db);
|
||||||
const clientStrategiesDb = require('./lib/db/client-strategies')(db);
|
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
baseUriPath: options.baseUriPath,
|
baseUriPath: options.baseUriPath,
|
||||||
@ -30,8 +29,9 @@ function createApp (options) {
|
|||||||
eventStore,
|
eventStore,
|
||||||
featureDb,
|
featureDb,
|
||||||
strategyDb,
|
strategyDb,
|
||||||
metricsDb,
|
clientMetricsDb,
|
||||||
clientStrategiesDb,
|
clientStrategiesDb,
|
||||||
|
clientInstancesDb,
|
||||||
};
|
};
|
||||||
|
|
||||||
const app = require('./app')(config);
|
const app = require('./app')(config);
|
||||||
|
@ -11,8 +11,7 @@ const EventStore = require('../../lib/event-store');
|
|||||||
const eventStore = new EventStore(eventDb);
|
const eventStore = new EventStore(eventDb);
|
||||||
const featureDb = require('../../lib/db/feature')(knex, eventStore);
|
const featureDb = require('../../lib/db/feature')(knex, eventStore);
|
||||||
const strategyDb = require('../../lib/db/strategy')(knex, eventStore);
|
const strategyDb = require('../../lib/db/strategy')(knex, eventStore);
|
||||||
const metricsDb = require('../../lib/db/metrics')(knex);
|
const { clientInstancesDb, clientStrategiesDb, clientMetricsDb } = require('../../lib/db')(knex);
|
||||||
const clientStrategiesDb = require('../../lib/db/client-strategies')(knex);
|
|
||||||
|
|
||||||
|
|
||||||
const app = require('../../app')({
|
const app = require('../../app')({
|
||||||
@ -22,8 +21,9 @@ const app = require('../../app')({
|
|||||||
eventStore,
|
eventStore,
|
||||||
featureDb,
|
featureDb,
|
||||||
strategyDb,
|
strategyDb,
|
||||||
metricsDb,
|
|
||||||
clientStrategiesDb,
|
clientStrategiesDb,
|
||||||
|
clientInstancesDb,
|
||||||
|
clientMetricsDb,
|
||||||
});
|
});
|
||||||
|
|
||||||
BPromise.promisifyAll(request);
|
BPromise.promisifyAll(request);
|
||||||
|
Loading…
Reference in New Issue
Block a user