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
|
|
|
const { clientMetricsSchema } = require('./metrics-schema');
|
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
class ClientMetricsController extends Controller {
|
2019-04-30 21:14:23 +02:00
|
|
|
constructor({ clientMetricsStore, clientInstanceStore }, getLogger) {
|
2018-12-03 08:59:13 +01:00
|
|
|
super();
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = getLogger('/api/client/metrics');
|
2018-11-24 16:11:08 +01:00
|
|
|
this.clientMetricsStore = clientMetricsStore;
|
|
|
|
this.clientInstanceStore = clientInstanceStore;
|
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;
|
|
|
|
|
2020-01-02 19:23:52 +01:00
|
|
|
const { error, value } = clientMetricsSchema.validate(data);
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-24 16:11:08 +01:00
|
|
|
if (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger.warn('Invalid metrics posted', error);
|
2018-11-24 16:11:08 +01:00
|
|
|
return res.status(400).json(error);
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-24 16:11:08 +01:00
|
|
|
try {
|
|
|
|
await this.clientMetricsStore.insert(value);
|
|
|
|
await this.clientInstanceStore.insert({
|
|
|
|
appName: value.appName,
|
|
|
|
instanceId: value.instanceId,
|
|
|
|
clientIp,
|
|
|
|
});
|
2020-04-14 22:29:11 +02:00
|
|
|
return res.status(202).end();
|
2018-11-24 16:11:08 +01:00
|
|
|
} catch (e) {
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger.error('failed to store metrics', e);
|
2020-04-14 22:29:11 +02:00
|
|
|
return res.status(500).end();
|
2018-11-24 16:11:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ClientMetricsController;
|