2016-10-27 13:13:51 +02:00
|
|
|
'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');
|
2016-10-27 13:13:51 +02:00
|
|
|
|
2016-10-27 16:55:38 +02:00
|
|
|
module.exports = function (app, config) {
|
2016-11-04 09:03:13 +01:00
|
|
|
const { clientMetricsDb, clientStrategiesDb } = config;
|
2016-10-27 13:13:51 +02:00
|
|
|
const metrics = new ClientMetrics();
|
2016-11-04 09:03:13 +01:00
|
|
|
const service = new ClientMetricsService(clientMetricsDb);
|
2016-10-27 13:13:51 +02:00
|
|
|
|
2016-11-02 23:17:28 +01:00
|
|
|
// Just som dummo demo data
|
|
|
|
clientStrategiesDb.insertOrUpdate('demo-app', ['default', 'test']).then(() => console.log('inserted client_strategies'));
|
|
|
|
|
|
|
|
|
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-04 16:16:55 +01:00
|
|
|
app.get('/toggle-metrics', (req, res) => {
|
|
|
|
res.json(metrics.getTogglesMetrics());
|
2016-10-27 13:13:51 +02:00
|
|
|
});
|
|
|
|
|
2016-11-02 12:49:25 +01:00
|
|
|
app.post('/client/metrics', (req, res) => {
|
2016-10-27 13:13:51 +02:00
|
|
|
try {
|
2016-11-02 12:49:25 +01:00
|
|
|
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
|
2016-11-04 16:16:55 +01:00
|
|
|
service
|
|
|
|
.insert(data)
|
|
|
|
.catch(e => logger.error('Error inserting metrics data', e));
|
2016-10-27 13:13:51 +02:00
|
|
|
} catch (e) {
|
2016-11-02 12:49:25 +01:00
|
|
|
logger.error('Error receiving metrics', e);
|
2016-10-27 13:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
2016-10-27 16:55:38 +02:00
|
|
|
|
2016-11-02 12:49:25 +01:00
|
|
|
app.post('/client/register', (req, res) => {
|
|
|
|
try {
|
|
|
|
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
|
|
|
|
metrics.registerClient(data);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error('Error registering client', e);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.end();
|
2016-10-27 16:55:38 +02:00
|
|
|
});
|
2016-11-02 23:17:28 +01:00
|
|
|
|
|
|
|
app.get('/client/strategies', (req, res) => {
|
|
|
|
clientStrategiesDb.getAll().then(data => res.json(data));
|
|
|
|
});
|
2016-10-27 13:13:51 +02:00
|
|
|
};
|