From 99c7842c2ad84be8445a41328b2cf0e63ef56758 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sun, 7 May 2023 21:00:11 +0100 Subject: [PATCH] Refacor and increase number of stats Signed-off-by: Fred Boniface --- src/services/dbAccess.services.js | 12 +++++++++- src/services/stats.services.js | 40 ++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/services/dbAccess.services.js b/src/services/dbAccess.services.js index 44df034..0818324 100644 --- a/src/services/dbAccess.services.js +++ b/src/services/dbAccess.services.js @@ -73,11 +73,21 @@ async function delRegReq(uuid) { col.deleteOne({uuid: uuid}); } +async function colCount(collection) { + log.out(`dbAccess.colCount: Counting entries in collection: ${collection}`); + await client.connect(); + let col = db.collection(collection); + let count = col.countDocuments(); + log.out(`dbAccess.colCount: Collection: ${collection} contains ${count} documents`); + return await count; +} + module.exports = { query, increment, // Probqbly doesn't need exporting? - It does, ldbServices needs to increment when the API is hit! addUser, userAtime, addRegReq, - delRegReq + delRegReq, + colCount }; \ No newline at end of file diff --git a/src/services/stats.services.js b/src/services/stats.services.js index a064984..7f4d79e 100644 --- a/src/services/stats.services.js +++ b/src/services/stats.services.js @@ -3,19 +3,37 @@ const db = require('../services/dbAccess.services'); const os = require('os'); const vers = require('../configs/version.configs'); +async function buildJson() { + let json = {}; + json.count = {}; + // Async call all db queries + const counters = db.query('meta', {target: 'counters'}); + const versions = db.query('meta', {target: 'versions'}); + const userCount = db.colCount('users'); + const regCount = db.colCount('registrations'); + const pisCount = db.colCount('pis'); + const corpusCount = db.colCount('corpus'); + const stationsCount = db.colCount('stations'); + + // Insert data + json.mode = process.env.NODE_ENV; + json.verBkend = vers.app; + json.verApi = vers.api; + json.hostname = os.hostname(); + // Await and insert async calls + json.dat = await counters; + json.ver = await versions; + json.count.users = await userCount; + json.count.reg = await regCount; + json.count.pis = await pisCount; + json.count.corpus = await corpusCount; + json.count.stations = await stationsCount; + return json; +} + async function hits(){ log.out('statsServices.hits: Statistics Requested', 'info'); - var dat = db.query('meta', {target: 'counters'}); - var ver = db.query('meta', {target: 'versions'}); - log.out('statsServices.hits: fetched server meta', 'info'); - let out = {}; - out.host = os.hostname(); - // eslint-disable-next-line no-undef - out.mode = process.env.NODE_ENV; - out.verBkend = vers.app; - out.verApi = vers.api; - out.dat = await dat; - out.ver = await ver; + const out = await buildJson(); log.out(`statsServices.hits: Sending Data: ${JSON.stringify(out)}`, 'info'); return out; }