mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-20 00:08:02 +01:00
WIP client_strategies
This commit is contained in:
parent
da753da20d
commit
6b6b400847
48
packages/unleash-api/lib/db/client-strategies.js
Normal file
48
packages/unleash-api/lib/db/client-strategies.js
Normal file
@ -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 };
|
||||||
|
};
|
@ -5,10 +5,14 @@ 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 = config.metricsDb;
|
const { metricsDb, clientStrategiesDb } = config;
|
||||||
const metrics = new ClientMetrics();
|
const metrics = new ClientMetrics();
|
||||||
const service = new ClientMetricsService(metricsDb);
|
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) => {
|
service.on('metrics', (entries) => {
|
||||||
entries.forEach((m) => metrics.addPayload(m.metrics));
|
entries.forEach((m) => metrics.addPayload(m.metrics));
|
||||||
});
|
});
|
||||||
@ -43,4 +47,8 @@ module.exports = function (app, config) {
|
|||||||
|
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get('/client/strategies', (req, res) => {
|
||||||
|
clientStrategiesDb.getAll().then(data => res.json(data));
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = require('../scripts/migration-runner').create('009-create-client-strategies');
|
@ -0,0 +1,2 @@
|
|||||||
|
--create client_strategies
|
||||||
|
DROP TABLE client_strategies;
|
@ -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
|
||||||
|
);
|
@ -19,6 +19,7 @@ function createApp (options) {
|
|||||||
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 metricsDb = require('./lib/db/metrics')(db);
|
||||||
|
const clientStrategiesDb = require('./lib/db/client-strategies')(db);
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
baseUriPath: options.baseUriPath,
|
baseUriPath: options.baseUriPath,
|
||||||
@ -30,6 +31,7 @@ function createApp (options) {
|
|||||||
featureDb,
|
featureDb,
|
||||||
strategyDb,
|
strategyDb,
|
||||||
metricsDb,
|
metricsDb,
|
||||||
|
clientStrategiesDb,
|
||||||
};
|
};
|
||||||
|
|
||||||
const app = require('./app')(config);
|
const app = require('./app')(config);
|
||||||
|
Loading…
Reference in New Issue
Block a user