2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const joi = require('joi');
|
|
|
|
|
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;
|
|
|
|
|
2018-11-24 16:11:08 +01:00
|
|
|
const { error, value } = joi.validate(data, clientMetricsSchema);
|
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,
|
|
|
|
});
|
|
|
|
res.status(202).end();
|
|
|
|
} catch (e) {
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger.error('failed to store metrics', e);
|
2018-11-24 16:11:08 +01:00
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ClientMetricsController;
|