add api for getting statistics

This commit is contained in:
Ean Milligan 2025-07-22 16:48:51 -04:00
parent 57cae9c01f
commit 0006e66b16
4 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,11 @@
meta {
name: Bot Statistics
type: http
seq: 2
}
get {
url: http://localhost:8166/api/stats
body: none
auth: inherit
}

View File

@ -102,6 +102,9 @@ const start = () => {
case '/ping':
case '/ping/':
return endpoints.get.apiPing();
case '/stats':
case '/stats/':
return endpoints.get.apiStats();
case '/key':
case '/key/':
return endpoints.get.apiKeyAdmin(query, apiUserid);

View File

@ -5,6 +5,7 @@ import { apiKey } from 'endpoints/gets/apiKey.ts';
import { apiKeyAdmin } from 'endpoints/gets/apiKeyAdmin.ts';
import { apiPing } from 'endpoints/gets/apiPing.ts';
import { apiRoll } from 'endpoints/gets/apiRoll.ts';
import { apiStats } from 'endpoints/gets/apiStats.ts';
import { generateWebView } from 'endpoints/gets/webView.ts';
import { heatmapPng } from 'endpoints/gets/heatmapPng.ts';
@ -24,6 +25,7 @@ export default {
apiKeyAdmin,
apiPing,
apiRoll,
apiStats,
generateWebView,
heatmapPng,
},

View File

@ -0,0 +1,44 @@
import { cache, cacheHandlers } from '@discordeno';
import { STATUS_CODE, STATUS_TEXT } from '@std/http/status';
import { basicReducer } from 'artigen/utils/reducers.ts';
import dbClient from 'db/client.ts';
import stdResponses from 'endpoints/stdResponses.ts';
import utils from 'utils/utils.ts';
export const apiStats = async (): Promise<Response> => {
const headers = new Headers();
headers.append('Content-Type', 'text/json');
const memberCount = cache.guilds
.array()
.map((guild) => guild.memberCount)
.reduce(basicReducer);
const cachedGuilds = await cacheHandlers.size('guilds');
const rollQuery = await dbClient
.query(`SELECT count, hourlyRate FROM command_cnt WHERE command = "roll";`)
.catch((e) => utils.commonLoggers.dbError('stats.ts:23', 'query', e));
const rollCount = BigInt(rollQuery[0].count ?? '0');
if (!rollCount) {
return stdResponses.InternalServerError('');
}
return new Response(
JSON.stringify({
guildCount: cachedGuilds + cache.dispatchedGuildIds.size,
memberCount,
rollCount,
}),
{
status: STATUS_CODE.OK,
statusText: STATUS_TEXT[STATUS_CODE.OK],
headers,
}
);
};