mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
chore(modernize): Modernize ClientMetricsController
This commit is contained in:
parent
8913c4fac5
commit
80a5d5c581
@ -2,7 +2,7 @@
|
||||
|
||||
const { Router } = require('express');
|
||||
const FeatureController = require('./feature.js');
|
||||
const metrics = require('./metrics.js');
|
||||
const ClientMetricsController = require('./metrics.js');
|
||||
const register = require('./register.js');
|
||||
const clientApi = require('./client-api.json');
|
||||
|
||||
@ -13,7 +13,7 @@ class ClientApi {
|
||||
|
||||
router.get('/', this.index);
|
||||
router.use('/features', new FeatureController(config).router());
|
||||
router.use('/metrics', metrics.router(config));
|
||||
router.use('/metrics', new ClientMetricsController(config).router());
|
||||
router.use('/register', register.router(config));
|
||||
}
|
||||
|
||||
|
@ -6,34 +6,44 @@ const logger = require('../../logger')('client-api/metrics.js');
|
||||
|
||||
const { clientMetricsSchema } = require('./metrics-schema');
|
||||
|
||||
exports.router = config => {
|
||||
const { clientMetricsStore, clientInstanceStore } = config.stores;
|
||||
const router = Router();
|
||||
class ClientMetricsController {
|
||||
constructor({ stores: { clientMetricsStore, clientInstanceStore } }) {
|
||||
const router = Router();
|
||||
this.clientMetricsStore = clientMetricsStore;
|
||||
this.clientInstanceStore = clientInstanceStore;
|
||||
this._router = router;
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
router.post('/', (req, res) => this.registerMetrics(req, res));
|
||||
}
|
||||
|
||||
async registerMetrics(req, res) {
|
||||
const data = req.body;
|
||||
const clientIp = req.ip;
|
||||
|
||||
joi.validate(data, clientMetricsSchema, (err, cleaned) => {
|
||||
if (err) {
|
||||
logger.warn('Invalid metrics posted', err);
|
||||
return res.status(400).json(err);
|
||||
}
|
||||
const { error, value } = joi.validate(data, clientMetricsSchema);
|
||||
|
||||
clientMetricsStore
|
||||
.insert(cleaned)
|
||||
.then(() =>
|
||||
clientInstanceStore.insert({
|
||||
appName: cleaned.appName,
|
||||
instanceId: cleaned.instanceId,
|
||||
clientIp,
|
||||
})
|
||||
)
|
||||
.catch(err => logger.error('failed to store metrics', err));
|
||||
if (error) {
|
||||
logger.warn('Invalid metrics posted', error);
|
||||
return res.status(400).json(error);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
return router;
|
||||
};
|
||||
router() {
|
||||
return this._router;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientMetricsController;
|
||||
|
Loading…
Reference in New Issue
Block a user