Skip to content

Commit

Permalink
feat(metrics): add temporary/permanent/status roles count individually
Browse files Browse the repository at this point in the history
  • Loading branch information
tippfehlr committed Mar 11, 2024
1 parent ecc07fb commit 84fdc79
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/modules/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DBCurrentlyActiveActivity,
addActivity,
getActivityRoles,
getDBUserCount,
getUserCount,
getGuildConfig,
getRolesCount,
getStatusRoles,
Expand Down Expand Up @@ -98,7 +98,7 @@ client.on(Events.ClientReady, () => {
singular: '%s user',
plural: '%s users',
locale: 'en-US',
count: getDBUserCount()
count: getUserCount()
}),
type: ActivityType.Watching
}
Expand Down Expand Up @@ -128,7 +128,7 @@ client.on(Events.ClientReady, () => {
`Logged in as ${client.user?.username}#${client.user?.discriminator} (${client.user?.id})`
);
log.info(
`The bot is currently on ${client.guilds.cache.size} guilds with ${getDBUserCount()} users and manages ${getRolesCount()} roles`
`The bot is currently on ${client.guilds.cache.size} guilds with ${getUserCount()} users and manages ${getRolesCount()} roles`
);

const activityCountInCurrentlyActiveActivities = (
Expand Down
27 changes: 13 additions & 14 deletions src/modules/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,30 @@ export async function addActivity(guildID: string, activityName: string) {
).run(guildID, activityName, 1);
}

export function getDBUserCount(): number {
export function getUserCount(): number {
return (prepare('SELECT COUNT(*) FROM users').get() as { 'COUNT(*)': number })['COUNT(*)'];
}

export function getDBActivityRoleCount(): number {
export function getActivityRoleCount(): number {
return (prepare('SELECT COUNT(*) FROM activityRoles').get() as { 'COUNT(*)': number })['COUNT(*)'];
}

export function getDBStatusRoleCount(): number {
export function getStatusRoleCount(): number {
return (prepare('SELECT COUNT(*) FROM statusRoles').get() as { 'COUNT(*)': number })['COUNT(*)'];
}

export function getDBCurrentlyActiveActivityCount(): number {
export function getCurrentlyActiveActivityCount(): number {
return (prepare('SELECT COUNT(*) FROM currentlyActiveActivities').get() as { 'COUNT(*)': number })['COUNT(*)'];
}

export function getTempRoleCount(): number {
return (prepare('SELECT COUNT(*) FROM activityRoles WHERE live = 1').get() as { 'COUNT(*)': number; })['COUNT(*)'];
}

export function getPermRoleCount(): number {
return (prepare('SELECT COUNT(*) FROM activityRoles WHERE live = 0').get() as { 'COUNT(*)': number; })['COUNT(*)'];
}

export function getRolesCount(): number {
return (
prepare('SELECT COUNT(*) FROM activityRoles').get() as {
'COUNT(*)': number;
}
)['COUNT(*)'] +
(
prepare('SELECT COUNT(*) FROM statusRoles').get() as {
'COUNT(*)': number;
}
)['COUNT(*)']
return getActivityRoleCount() + getStatusRoleCount()
}
7 changes: 5 additions & 2 deletions src/modules/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import config from './config';
import { log } from './messages';

import { stats as botStats, resetStats as resetBotStats } from './bot';
import { getDBUserCount, getRolesCount, stats as dbStats, resetStats as resetDBStats } from './db';
import { getUserCount, getRolesCount, stats as dbStats, resetStats as resetDBStats, getTempRoleCount, getPermRoleCount, getStatusRoleCount } from './db';

export let client: InfluxDB;
export let writeApi: WriteApi;
Expand All @@ -31,10 +31,13 @@ export async function configureInfluxDB() {
writeIntPoint('web_socket_errors', 'web_socket_errors', botStats.webSocketErrors);
writeIntPoint('guilds', 'guilds_total', discordClient.guilds.cache.size);
writeIntPoint('roles', 'roles_count', getRolesCount());
writeIntPoint('roles', 'temporary_roles_count', getTempRoleCount());
writeIntPoint('roles', 'permanent_roles_count', getPermRoleCount());
writeIntPoint('roles', 'status_roles_count', getStatusRoleCount());
writeApi.writePoint(
new Point('users')
.intField('users_cache_total', discordClient.users.cache.size)
.intField('users_db_total', getDBUserCount())
.intField('users_db_total', getUserCount())
);
writeIntPoint('db', 'calls', dbStats.dbCalls);
const ram = process.memoryUsage();
Expand Down

0 comments on commit 84fdc79

Please sign in to comment.