1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00
unleash.unleash/lib/routes/metrics.js

78 lines
2.3 KiB
JavaScript
Raw Normal View History

'use strict';
const logger = require('../logger');
const ClientMetrics = require('../client-metrics');
2016-11-04 16:19:31 +01:00
const ClientMetricsService = require('../client-metrics/service');
const joi = require('joi');
2016-11-11 15:46:59 +01:00
const { clientMetricsSchema, clientRegisterSchema } = require('./metrics-schema');
2016-10-27 16:55:38 +02:00
module.exports = function (app, config) {
2016-11-04 23:02:55 +01:00
const {
2016-11-05 13:36:44 +01:00
clientMetricsStore,
clientStrategyStore,
clientInstanceStore,
} = config.stores;
2016-11-11 15:46:59 +01:00
const metrics = new ClientMetrics();
2016-11-05 13:36:44 +01:00
const service = new ClientMetricsService(clientMetricsStore);
2016-11-02 12:49:25 +01:00
service.on('metrics', (entries) => {
2016-11-04 16:16:55 +01:00
entries.forEach((m) => {
metrics.addPayload(m.metrics);
});
2016-11-02 12:49:25 +01:00
});
2016-11-04 16:16:55 +01:00
app.get('/metrics', (req, res) => {
res.json(metrics.getMetricsOverview());
2016-11-02 12:49:25 +01:00
});
2016-10-27 16:55:38 +02:00
2016-11-05 11:50:34 +01:00
app.get('/metrics/features', (req, res) => {
2016-11-04 16:16:55 +01:00
res.json(metrics.getTogglesMetrics());
});
2016-11-02 12:49:25 +01:00
app.post('/client/metrics', (req, res) => {
2016-11-11 15:46:59 +01:00
const data = req.body;
joi.validate(data, clientMetricsSchema, (err, cleaned) => {
if (err) {
return res.status(400).json(err);
}
2016-11-11 15:46:59 +01:00
service.insert(cleaned)
2016-11-04 16:16:55 +01:00
.catch(e => logger.error('Error inserting metrics data', e));
2016-11-11 15:46:59 +01:00
res.status(202).end();
});
});
2016-11-02 12:49:25 +01:00
app.post('/client/register', (req, res) => {
2016-11-04 23:02:55 +01:00
const data = req.body;
2016-11-05 12:42:58 +01:00
const clientIp = req.ip;
2016-11-02 12:49:25 +01:00
joi.validate(data, clientRegisterSchema, (err, cleaned) => {
if (err) {
2016-11-10 22:05:50 +01:00
return res.status(400).json(err);
}
clientStrategyStore.insert(cleaned.appName, cleaned.strategies)
.then(() => clientInstanceStore.insert({
appName: cleaned.appName,
instanceId: cleaned.instanceId,
clientIp,
}))
2016-11-11 15:46:59 +01:00
.then(() => logger.info('New client registered!'))
.catch((error) => logger.error('Error registering client', error));
2016-11-11 15:46:59 +01:00
res.status(202).end();
});
2016-10-27 16:55:38 +02:00
});
2016-11-02 23:17:28 +01:00
app.get('/client/strategies', (req, res) => {
clientStrategyStore.getAll().then(data => res.json(data));
2016-11-02 23:17:28 +01:00
});
2016-11-04 23:02:55 +01:00
app.get('/client/instances', (req, res) => {
clientInstanceStore.getAll()
2016-11-04 23:02:55 +01:00
.then(data => res.json(data))
2016-11-11 15:46:59 +01:00
.catch(err => logger.error(err));
2016-11-04 23:02:55 +01:00
});
};