mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-01 00:08:27 +01:00
WIP client_strategies
This commit is contained in:
parent
937f14addb
commit
337dfeb506
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');
|
||||
|
||||
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));
|
||||
});
|
||||
};
|
||||
|
@ -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 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);
|
||||
|
Loading…
Reference in New Issue
Block a user