From 55b854a592132a500ce1c522162328a9041cc50c Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 28 Jun 2023 13:36:47 +0100 Subject: [PATCH] Add /misc/server/statistics route Signed-off-by: Fred Boniface --- src/configs/version.configs.js | 2 +- src/controllers/stats.controllers.js | 17 ++++-- src/routes/misc.routes.js | 2 +- src/services/stats.services.js | 80 +++++++++++++++++++++++++++- 4 files changed, 94 insertions(+), 7 deletions(-) diff --git a/src/configs/version.configs.js b/src/configs/version.configs.js index ca0a352..fa62f8a 100644 --- a/src/configs/version.configs.js +++ b/src/configs/version.configs.js @@ -1,6 +1,6 @@ const version = { api: ['/api/v1/','/api/v2'], - app: '2023.6.13' + app: '2023.6.14' }; module.exports = version; \ No newline at end of file diff --git a/src/controllers/stats.controllers.js b/src/controllers/stats.controllers.js index d39b7e5..95e4786 100644 --- a/src/controllers/stats.controllers.js +++ b/src/controllers/stats.controllers.js @@ -1,6 +1,6 @@ const stat = require('../services/stats.services'); -async function get(req, res, next) { +async function get(req, res, next) { // API V1 try { res.json(await stat.hits()); } catch (err) { @@ -10,7 +10,7 @@ async function get(req, res, next) { } } -async function versions(req, res, next) { +async function versions(req, res, next) { // API v2 try { res.json(await stat.versions()); } catch (err) { @@ -20,7 +20,18 @@ async function versions(req, res, next) { } } +async function statistics(req, res, next) { // Api v2 + try { + res.json(await stat.statistics()); + } catch (err) { + console.error('Controller Error', err); + err.status = 500; + next(err); + } +} + module.exports = { get, - versions + versions, + statistics }; \ No newline at end of file diff --git a/src/routes/misc.routes.js b/src/routes/misc.routes.js index 0dce9af..59489be 100644 --- a/src/routes/misc.routes.js +++ b/src/routes/misc.routes.js @@ -8,7 +8,7 @@ const statCtr = require('../controllers/stats.controllers'); // Routes -router.get('/server/stats'); +router.get('/server/stats', statCtr.statistics); router.get('/server/versions', statCtr.versions); router.post('/issue', issueCtr.post); diff --git a/src/services/stats.services.js b/src/services/stats.services.js index 2bad7c8..01001a3 100644 --- a/src/services/stats.services.js +++ b/src/services/stats.services.js @@ -1,4 +1,3 @@ -/* global process */ const log = require('../utils/log.utils'); // Log Helper const db = require('../services/dbAccess.services'); const os = require('os'); @@ -54,7 +53,84 @@ async function versions() { return data; } +async function statistics() { + log.out('statsServices.statistics: Fetching statistics', 'info'); + + const countersPromise = db.query('meta', { target: 'counters' }); + const timetablePromise = db.query('meta', { target: 'timetable' }); + const pisPromise = db.query('meta', { target: 'pis' }); + const corpusPromise = db.query('meta', { target: 'corpus' }); + const reasonCodesPromise = db.query('meta', { target: 'reasonCodes' }); + + const lengthUsersPromise = db.colCount('users'); + const lengthRegistrationsPromise = db.colCount('registrations'); + const lengthCorpusPromise = db.colCount('corpus'); + const lengthStationsPromise = db.colCount('stations'); + const lengthPisPromise = db.colCount('pis'); + const lengthTimetablePromise = db.colCount('timetable'); + const lengthReasonCodesPromise = db.colCount('reasonCodes'); + + const [ + counters, + timetable, + pis, + corpus, + reasonCodes, + lengthUsers, + lengthRegistrations, + lengthCorpus, + lengthStations, + lengthPis, + lengthTimetable, + lengthReasonCodes + ] = await Promise.all([ + countersPromise, + timetablePromise, + pisPromise, + corpusPromise, + reasonCodesPromise, + lengthUsersPromise, + lengthRegistrationsPromise, + lengthCorpusPromise, + lengthStationsPromise, + lengthPisPromise, + lengthTimetablePromise, + lengthReasonCodesPromise + ]); + + return { + hostname: os.hostname() || 'Unknown', + runtimeMode: process.env.NODE_ENV || 'Unknown', + updateTimes: { + timetable: timetable[0]['updated'], + pis: pis[0]['updated'], + corpus: corpus[0]['updated'], + reasonCodes: reasonCodes[0]['updated'] + }, + requestCounts: { + ldbws_api: counters[0]['ldbws'] || 0, + lsbsvws_api: counters[0]['ldbsvws'] || 0, + corpus_api: counters[0]['corpus_api'] || 0, + timetable_db: counters[0]['timetable'] || 0, + pis_db: counters[0]['pis'] || 0, + corpus_db: counters[0]['corpus'] || 0, + stations_db: counters[0]['stations'] || 0 + }, + dbLengths: { + users: lengthUsers, + registrations: lengthRegistrations, + corpus: lengthCorpus, + stations: lengthStations, + pis: lengthPis, + timetable: lengthTimetable, + reasonCodes: lengthReasonCodes + }, + }; +} + + module.exports = { hits, - versions + versions, + statistics }; \ No newline at end of file