From 80a5d5c5815ac368319fa8656020d14dc67a0620 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sat, 24 Nov 2018 16:11:08 +0100 Subject: [PATCH] chore(modernize): Modernize ClientMetricsController --- lib/routes/client-api/index.js | 4 +-- lib/routes/client-api/metrics.js | 56 +++++++++++++++++++------------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/lib/routes/client-api/index.js b/lib/routes/client-api/index.js index 644c12b7d2..d42e8161f7 100644 --- a/lib/routes/client-api/index.js +++ b/lib/routes/client-api/index.js @@ -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)); } diff --git a/lib/routes/client-api/metrics.js b/lib/routes/client-api/metrics.js index 81fa73cca8..1cb89686f7 100644 --- a/lib/routes/client-api/metrics.js +++ b/lib/routes/client-api/metrics.js @@ -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;