2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Router } = require('express');
|
|
|
|
const joi = require('joi');
|
2017-08-04 16:03:15 +02:00
|
|
|
const logger = require('../../logger')('client-api/metrics.js');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
const { clientMetricsSchema } = require('./metrics-schema');
|
|
|
|
|
2018-11-24 16:11:08 +01:00
|
|
|
class ClientMetricsController {
|
|
|
|
constructor({ stores: { clientMetricsStore, clientInstanceStore } }) {
|
|
|
|
const router = Router();
|
|
|
|
this.clientMetricsStore = clientMetricsStore;
|
|
|
|
this.clientInstanceStore = clientInstanceStore;
|
|
|
|
this._router = router;
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-24 16:11:08 +01:00
|
|
|
router.post('/', (req, res) => this.registerMetrics(req, res));
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
logger.warn('Invalid metrics posted', error);
|
|
|
|
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) {
|
|
|
|
logger.error('failed to store metrics', e);
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
router() {
|
|
|
|
return this._router;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ClientMetricsController;
|