add /stats API endpoint

This commit is contained in:
janderedev 2022-03-23 20:13:43 +01:00
parent f830e99a64
commit 03b2ec5f66
No known key found for this signature in database
GPG key ID: 5D5E18ACB990F57A
3 changed files with 30 additions and 0 deletions

View file

@ -27,6 +27,7 @@ export { logger, app, db, PORT, SESSION_LIFETIME }
import('./routes/internal/ws'), import('./routes/internal/ws'),
import('./routes/root'), import('./routes/root'),
import('./routes/stats'),
import('./routes/login'), import('./routes/login'),
import('./routes/dash/servers'), import('./routes/dash/servers'),
import('./routes/dash/server'), import('./routes/dash/server'),

24
api/src/routes/stats.ts Normal file
View file

@ -0,0 +1,24 @@
import { app, logger } from '..';
import { Request, Response } from 'express';
import { botReq } from './internal/ws';
let SERVER_COUNT = 0;
const fetchStats = async () => {
try {
const res = await botReq('stats');
if (!res.success) return logger.warn(`Failed to fetch bot stats: ${res.statusCode} / ${res.error}`);
if (res.servers) SERVER_COUNT = Number(res.servers);
} catch(e) {
console.error(e);
}
}
fetchStats();
setInterval(() => fetchStats(), 10000);
app.get('/stats', async (req: Request, res: Response) => {
res.send({
servers: SERVER_COUNT,
});
});

View file

@ -136,6 +136,11 @@ wsEvents.on('req:requestLogin', async (data: any, cb: (data: WSResponse) => void
} }
}); });
wsEvents.on('req:stats', async (_data: any, cb: (data: { servers: number }) => void) => {
const servers = bot.servers.size;
cb({ servers });
});
export { wsEvents, wsSend, WSResponse } export { wsEvents, wsSend, WSResponse }
import('./api/servers'); import('./api/servers');