2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
const Controller = require('../controller');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
class ClientMetricsController extends Controller {
|
2021-01-18 12:32:19 +01:00
|
|
|
constructor({ clientMetricsService }, getLogger) {
|
2018-12-03 08:59:13 +01:00
|
|
|
super();
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = getLogger('/api/client/metrics');
|
2021-01-18 12:32:19 +01:00
|
|
|
this.metrics = clientMetricsService;
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
this.post('/', this.registerMetrics);
|
2018-11-24 16:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async registerMetrics(req, res) {
|
2017-06-28 10:20:22 +02:00
|
|
|
const data = req.body;
|
|
|
|
const clientIp = req.ip;
|
|
|
|
|
2018-11-24 16:11:08 +01:00
|
|
|
try {
|
2021-01-18 12:32:19 +01:00
|
|
|
await this.metrics.registerClientMetrics(data, clientIp);
|
2020-04-14 22:29:11 +02:00
|
|
|
return res.status(202).end();
|
2018-11-24 16:11:08 +01:00
|
|
|
} catch (e) {
|
2021-01-21 13:47:08 +01:00
|
|
|
this.logger.warn('Failed to store metrics', e);
|
2021-01-18 12:32:19 +01:00
|
|
|
switch (e.name) {
|
|
|
|
case 'ValidationError':
|
|
|
|
return res.status(400).end();
|
|
|
|
default:
|
|
|
|
return res.status(500).end();
|
|
|
|
}
|
2018-11-24 16:11:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ClientMetricsController;
|