Add /misc/server/statistics route

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-06-28 13:36:47 +01:00
parent 07a0dd6f28
commit 55b854a592
4 changed files with 94 additions and 7 deletions

View File

@ -1,6 +1,6 @@
const version = {
api: ['/api/v1/','/api/v2'],
app: '2023.6.13'
app: '2023.6.14'
};
module.exports = version;

View File

@ -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
};

View File

@ -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);

View File

@ -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
};