2016-10-27 13:13:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const logger = require('../logger');
|
|
|
|
const ClientMetrics = require('../client-metrics');
|
2016-10-27 16:55:38 +02: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) {
|
|
|
|
const metricsDb = config.metricsDb;
|
2016-10-27 13:13:51 +02:00
|
|
|
const metrics = new ClientMetrics();
|
2016-10-27 16:55:38 +02:00
|
|
|
const service = new ClientMetricsService(metricsDb);
|
2016-10-27 13:13:51 +02:00
|
|
|
|
2016-11-02 12:49:25 +01:00
|
|
|
service.on('metrics', (entries) => {
|
|
|
|
entries.forEach((m) => metrics.addPayload(m.metrics));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/service-metrics', (req, res) => {
|
2016-10-27 16:55:38 +02:00
|
|
|
res.json(service.getMetrics());
|
2016-11-02 12:49:25 +01:00
|
|
|
});
|
2016-10-27 16:55:38 +02:00
|
|
|
|
2016-11-02 12:49:25 +01:00
|
|
|
app.get('/metrics', (req, res) => {
|
|
|
|
res.json(metrics.getState());
|
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;
|
|
|
|
metrics.addPayload(data);
|
|
|
|
service.insert(data);
|
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-10-27 13:13:51 +02:00
|
|
|
};
|