mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-14 00:19:16 +01:00
chore(modernize): Admin MetricsController
This commit is contained in:
parent
a2c330f92e
commit
738c26fd62
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,138 +1,142 @@
|
|||||||
'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);
|
||||||
});
|
}
|
||||||
|
|
||||||
router.get('/seen-apps', (req, res) => {
|
async getSeenApps(req, res) {
|
||||||
const seenApps = metrics.getSeenAppsPerToggle();
|
const seenApps = this.metrics.getSeenAppsPerToggle();
|
||||||
clientApplicationsStore
|
const applications = await this.clientApplicationsStore.getApplications();
|
||||||
.getApplications()
|
const metaData = applications.reduce((result, entry) => {
|
||||||
.then(toLookup)
|
result[entry.appName] = entry;
|
||||||
.then(metaData => {
|
return result;
|
||||||
Object.keys(seenApps).forEach(key => {
|
}, {});
|
||||||
seenApps[key] = seenApps[key].map(entry => {
|
|
||||||
if (metaData[entry.appName]) {
|
Object.keys(seenApps).forEach(key => {
|
||||||
return Object.assign(
|
seenApps[key] = seenApps[key].map(entry => {
|
||||||
{},
|
if (metaData[entry.appName]) {
|
||||||
entry,
|
return Object.assign({}, entry, metaData[entry.appName]);
|
||||||
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);
|
||||||
|
|
||||||
Promise.all([
|
try {
|
||||||
clientApplicationsStore.getApplication(appName),
|
const [
|
||||||
clientInstanceStore.getByAppName(appName),
|
application,
|
||||||
strategyStore.getStrategies(),
|
instances,
|
||||||
featureToggleStore.getFeatures(),
|
strategies,
|
||||||
])
|
features,
|
||||||
.then(([application, instances, strategies, features]) => {
|
] = await Promise.all([
|
||||||
const appDetails = {
|
this.clientApplicationsStore.getApplication(appName),
|
||||||
appName: application.appName,
|
this.clientInstanceStore.getByAppName(appName),
|
||||||
createdAt: application.createdAt,
|
this.strategyStore.getStrategies(),
|
||||||
description: application.description,
|
this.featureToggleStore.getFeatures(),
|
||||||
url: application.url,
|
]);
|
||||||
color: application.color,
|
|
||||||
icon: application.icon,
|
|
||||||
strategies: application.strategies.map(name => {
|
|
||||||
const found = strategies.find(
|
|
||||||
feature => feature.name === name
|
|
||||||
);
|
|
||||||
if (found) {
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
return { name, notFound: true };
|
|
||||||
}),
|
|
||||||
instances,
|
|
||||||
seenToggles: seenToggles.map(name => {
|
|
||||||
const found = features.find(
|
|
||||||
feature => feature.name === name
|
|
||||||
);
|
|
||||||
if (found) {
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
return { name, notFound: true };
|
|
||||||
}),
|
|
||||||
links: {
|
|
||||||
self: `/api/applications/${application.appName}`,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
res.json(appDetails);
|
|
||||||
})
|
|
||||||
.catch(err => catchLogAndSendErrorResponse(err, res));
|
|
||||||
});
|
|
||||||
|
|
||||||
return router;
|
const appDetails = {
|
||||||
};
|
appName: application.appName,
|
||||||
|
createdAt: application.createdAt,
|
||||||
|
description: application.description,
|
||||||
|
url: application.url,
|
||||||
|
color: application.color,
|
||||||
|
icon: application.icon,
|
||||||
|
strategies: application.strategies.map(name => {
|
||||||
|
const found = strategies.find(f => f.name === name);
|
||||||
|
return found ? found : { name, notFound: true };
|
||||||
|
}),
|
||||||
|
instances,
|
||||||
|
seenToggles: seenToggles.map(name => {
|
||||||
|
const found = features.find(f => f.name === name);
|
||||||
|
return found ? found : { name, notFound: true };
|
||||||
|
}),
|
||||||
|
links: {
|
||||||
|
self: `/api/applications/${application.appName}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
res.json(appDetails);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(err);
|
||||||
|
res.status(500).end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = MetricsController;
|
||||||
|
Loading…
Reference in New Issue
Block a user