From 9450fc6fe2b842ee86f13894b6afb3e12c538bdc Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 2 Feb 2023 19:49:44 +0000 Subject: [PATCH] FullStack: Move stats endpoint Signed-off-by: Fred Boniface --- app.js | 2 ++ src/controllers/stats.controllers.js | 13 +++++++++++++ src/routes/list.routes.js | 1 - src/routes/stats.routes.js | 8 ++++++++ src/services/list.services.js | 12 +----------- src/services/stats.services.js | 16 ++++++++++++++++ static/js/stat.js | 2 +- 7 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 src/controllers/stats.controllers.js create mode 100644 src/routes/stats.routes.js create mode 100644 src/services/stats.services.js diff --git a/app.js b/app.js index 374bc69..5269fb9 100644 --- a/app.js +++ b/app.js @@ -20,6 +20,7 @@ const ldbRtr = require('./src/routes/ldb.routes'); // /ldb endpoints const kubeRtr = require('./src/routes/kube.routes'); // /kube endpoints const findRtr = require('./src/routes/find.routes'); // /find endpoints const issueRtr = require('./src/routes/issue.routes') // /issue endpoints +const statRtr = require('./src/routes/stats.routes'); // /stat endpoints const initDb = require('./src/utils/dbinit.utils'); // DB Init Utility // Set Server Configurations @@ -59,6 +60,7 @@ app.use('/api/v1/ldb', ldbRtr); app.use('/api/v1/kube', kubeRtr); app.use('/api/v1/find', findRtr); app.use('/api/v1/issue', issueRtr); +app.use('/api/v1/stats', statRtr) // Start Express app.listen(srvPort, srvListen, (error) =>{ diff --git a/src/controllers/stats.controllers.js b/src/controllers/stats.controllers.js new file mode 100644 index 0000000..f42e0f6 --- /dev/null +++ b/src/controllers/stats.controllers.js @@ -0,0 +1,13 @@ +const stat = require('../services/stats.services'); + +async function get(req, res, next) { + try { + res.json(await stat.hits()) + } catch (err) { + console.error(`Controller Error`, err); + next(err); + } +} + +module.exports = { + get} \ No newline at end of file diff --git a/src/routes/list.routes.js b/src/routes/list.routes.js index 51bfbde..f49d13d 100644 --- a/src/routes/list.routes.js +++ b/src/routes/list.routes.js @@ -16,6 +16,5 @@ const listController = require('../controllers/list.controllers'); router.get('/stations', listController.getStations); router.get('/corpus', listController.getCorpus); -router.get('/hits', listController.hits) module.exports = router; \ No newline at end of file diff --git a/src/routes/stats.routes.js b/src/routes/stats.routes.js new file mode 100644 index 0000000..ae46cbc --- /dev/null +++ b/src/routes/stats.routes.js @@ -0,0 +1,8 @@ +const express = require('express'); +const router = express.Router(); +const statsController = require('../controllers/stats.controllers'); + + +router.get('/', statsController.get); + +module.exports = router; \ No newline at end of file diff --git a/src/services/list.services.js b/src/services/list.services.js index a84d099..7601291 100644 --- a/src/services/list.services.js +++ b/src/services/list.services.js @@ -14,17 +14,7 @@ async function getCorpus(){ return out; } -async function hits(){ - var dat = await db.query("meta", {target: "counters"}); - log.out(`listServices.meta: fetched server meta`) - let out = {} - out.host = os.hostname() - out.dat = dat - return out; -} - module.exports = { getStations, - getCorpus, - hits + getCorpus } \ No newline at end of file diff --git a/src/services/stats.services.js b/src/services/stats.services.js new file mode 100644 index 0000000..7d31ca2 --- /dev/null +++ b/src/services/stats.services.js @@ -0,0 +1,16 @@ +const log = require('../utils/log.utils'); // Log Helper +const db = require('../services/dbAccess.services') +const os = require('os') + +async function hits(){ + var dat = await db.query("meta", {target: "counters"}); + log.out(`listServices.meta: fetched server meta`) + let out = {} + out.host = os.hostname() + out.dat = dat + return out; +} + +module.exports = { + hits +} \ No newline at end of file diff --git a/static/js/stat.js b/static/js/stat.js index 5730b5c..254ef18 100644 --- a/static/js/stat.js +++ b/static/js/stat.js @@ -5,7 +5,7 @@ async function init() { } async function get() { - var url = `${window.location.origin}/api/v1/list/hits`; + var url = `${window.location.origin}/api/v1/stats`; var resp = await fetch(url); return await resp.json(); }