1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-03 01:18:43 +02:00

chore(modernize): Admin MetricsController

This commit is contained in:
ivaosthu 2018-12-04 08:50:45 +01:00 committed by Ivar Conradi Østhus
parent a2c330f92e
commit 738c26fd62
2 changed files with 109 additions and 105 deletions

View File

@ -5,7 +5,7 @@ const FeatureController = require('./feature.js');
const ArchiveController = require('./archive.js'); const ArchiveController = require('./archive.js');
const EventController = require('./event.js'); const EventController = require('./event.js');
const strategies = require('./strategy'); const strategies = require('./strategy');
const metrics = require('./metrics'); const MetricsController = require('./metrics');
const UserController = require('./user'); const UserController = require('./user');
const apiDef = require('./api-def.json'); const apiDef = require('./api-def.json');
@ -20,7 +20,7 @@ class AdminApi extends Controller {
this.app.use('/archive', new ArchiveController(stores).router); this.app.use('/archive', new ArchiveController(stores).router);
this.app.use('/strategies', strategies.router(config)); this.app.use('/strategies', strategies.router(config));
this.app.use('/events', new EventController(stores).router); this.app.use('/events', new EventController(stores).router);
this.app.use('/metrics', metrics.router(config)); this.app.use('/metrics', new MetricsController(stores).router);
this.app.use('/user', new UserController().router); this.app.use('/user', new UserController().router);
} }

View File

@ -1,104 +1,116 @@
'use strict'; 'use strict';
const { Router } = require('express'); const Controller = require('../controller');
const logger = require('../../logger')('/admin-api/metrics.js'); const logger = require('../../logger')('/admin-api/metrics.js');
const ClientMetrics = require('../../client-metrics'); const ClientMetrics = require('../../client-metrics');
const catchLogAndSendErrorResponse = (err, res) => { class MetricsController extends Controller {
logger.error(err); constructor({
res.status(500).end();
};
exports.router = function(config) {
const {
clientMetricsStore, clientMetricsStore,
clientInstanceStore, clientInstanceStore,
clientApplicationsStore, clientApplicationsStore,
strategyStore, strategyStore,
featureToggleStore, featureToggleStore,
} = config.stores; }) {
super();
this.metrics = new ClientMetrics(clientMetricsStore);
this.clientInstanceStore = clientInstanceStore;
this.clientApplicationsStore = clientApplicationsStore;
this.strategyStore = strategyStore;
this.featureToggleStore = featureToggleStore;
const metrics = new ClientMetrics(clientMetricsStore); this.get('/seen-toggles', this.getSeenToggles);
const router = Router(); this.get('/seen-apps', this.getSeenApps);
this.get('/feature-toggles', this.getFeatureToggles);
this.get('/feature-toggles/:name', this.getFeatureToggle);
this.post('/applications/:appName', this.createApplication);
this.get('/applications/', this.getApplications);
this.get('/applications/:appName', this.getApplication);
}
router.get('/seen-toggles', (req, res) => { getSeenToggles(req, res) {
const seenAppToggles = metrics.getAppsWithToggles(); const seenAppToggles = this.metrics.getAppsWithToggles();
res.json(seenAppToggles); res.json(seenAppToggles);
}); }
async getSeenApps(req, res) {
const seenApps = this.metrics.getSeenAppsPerToggle();
const applications = await this.clientApplicationsStore.getApplications();
const metaData = applications.reduce((result, entry) => {
result[entry.appName] = entry;
return result;
}, {});
router.get('/seen-apps', (req, res) => {
const seenApps = metrics.getSeenAppsPerToggle();
clientApplicationsStore
.getApplications()
.then(toLookup)
.then(metaData => {
Object.keys(seenApps).forEach(key => { Object.keys(seenApps).forEach(key => {
seenApps[key] = seenApps[key].map(entry => { seenApps[key] = seenApps[key].map(entry => {
if (metaData[entry.appName]) { if (metaData[entry.appName]) {
return Object.assign( return Object.assign({}, entry, metaData[entry.appName]);
{},
entry,
metaData[entry.appName]
);
} }
return entry; return entry;
}); });
}); });
res.json(seenApps); res.json(seenApps);
}); }
});
router.get('/feature-toggles', (req, res) => { getFeatureToggles(req, res) {
res.json(metrics.getTogglesMetrics()); res.json(this.metrics.getTogglesMetrics());
}); }
router.get('/feature-toggles/:name', (req, res) => { getFeatureToggle(req, res) {
const name = req.params.name; const name = req.params.name;
const data = metrics.getTogglesMetrics(); const data = this.metrics.getTogglesMetrics();
const lastHour = data.lastHour[name] || {}; const lastHour = data.lastHour[name] || {};
const lastMinute = data.lastMinute[name] || {}; const lastMinute = data.lastMinute[name] || {};
res.json({ res.json({
lastHour, lastHour,
lastMinute, lastMinute,
}); });
}); }
router.post('/applications/:appName', (req, res) => { // Todo: add joi-schema validation
async createApplication(req, res) {
const input = Object.assign({}, req.body, { const input = Object.assign({}, req.body, {
appName: req.params.appName, appName: req.params.appName,
}); });
clientApplicationsStore
.upsert(input)
.then(() => res.status(202).end())
.catch(err => catchLogAndSendErrorResponse(err, res));
});
function toLookup(metaData) { try {
return metaData.reduce((result, entry) => { await this.clientApplicationsStore.upsert(input);
result[entry.appName] = entry; res.status(202).end();
return result; } catch (err) {
}, {}); logger.error(err);
res.status(500).end();
}
} }
router.get('/applications/', (req, res) => { async getApplications(req, res) {
clientApplicationsStore try {
.getApplications(req.query) const applications = await this.clientApplicationsStore.getApplications(
.then(applications => res.json({ applications })) req.query
.catch(err => catchLogAndSendErrorResponse(err, res)); );
}); res.json({ applications });
} catch (err) {
logger.error(err);
res.status(500).end();
}
}
router.get('/applications/:appName', (req, res) => { async getApplication(req, res) {
const appName = req.params.appName; const appName = req.params.appName;
const seenToggles = metrics.getSeenTogglesByAppName(appName); const seenToggles = this.metrics.getSeenTogglesByAppName(appName);
try {
const [
application,
instances,
strategies,
features,
] = await Promise.all([
this.clientApplicationsStore.getApplication(appName),
this.clientInstanceStore.getByAppName(appName),
this.strategyStore.getStrategies(),
this.featureToggleStore.getFeatures(),
]);
Promise.all([
clientApplicationsStore.getApplication(appName),
clientInstanceStore.getByAppName(appName),
strategyStore.getStrategies(),
featureToggleStore.getFeatures(),
])
.then(([application, instances, strategies, features]) => {
const appDetails = { const appDetails = {
appName: application.appName, appName: application.appName,
createdAt: application.createdAt, createdAt: application.createdAt,
@ -107,32 +119,24 @@ exports.router = function(config) {
color: application.color, color: application.color,
icon: application.icon, icon: application.icon,
strategies: application.strategies.map(name => { strategies: application.strategies.map(name => {
const found = strategies.find( const found = strategies.find(f => f.name === name);
feature => feature.name === name return found ? found : { name, notFound: true };
);
if (found) {
return found;
}
return { name, notFound: true };
}), }),
instances, instances,
seenToggles: seenToggles.map(name => { seenToggles: seenToggles.map(name => {
const found = features.find( const found = features.find(f => f.name === name);
feature => feature.name === name return found ? found : { name, notFound: true };
);
if (found) {
return found;
}
return { name, notFound: true };
}), }),
links: { links: {
self: `/api/applications/${application.appName}`, self: `/api/applications/${application.appName}`,
}, },
}; };
res.json(appDetails); res.json(appDetails);
}) } catch (err) {
.catch(err => catchLogAndSendErrorResponse(err, res)); logger.error(err);
}); res.status(500).end();
}
}
}
return router; module.exports = MetricsController;
};