backend/src/services/stats.services.js
Fred Boniface cb8d53ae36 Add timetable count to stats
Signed-off-by: Fred Boniface <fred@fjla.uk>
2023-05-31 19:14:42 +01:00

46 lines
1.4 KiB
JavaScript

/* global process */
const log = require('../utils/log.utils'); // Log Helper
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');
const timetableCount = db.colCount('timetable');
// Insert data
json.mode = process.env.NODE_ENV;
json.verBkend = vers.app;
json.verApi = vers.api;
json.host = 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;
json.count.timetable = await timetableCount;
return json;
}
async function hits(){
log.out('statsServices.hits: Statistics Requested', 'info');
const out = await buildJson();
log.out(`statsServices.hits: Sending Data: ${JSON.stringify(out)}`, 'info');
return out;
}
module.exports = {
hits
};