backend/src/services/stats.services.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

/* 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');
// 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');
const out = await buildJson();
log.out(`statsServices.hits: Sending Data: ${JSON.stringify(out)}`, 'info');
return out;
}
module.exports = {
hits
};