Add DB audit command

This commit is contained in:
Ean Milligan (Bastion) 2023-04-30 00:27:15 -04:00
parent 022170a230
commit e26771f24e
5 changed files with 109 additions and 3 deletions

View File

@ -93,5 +93,20 @@ await dbClient.execute(`
`);
console.log('Table created');
// Database sizes view
console.log('Attempting to create view db_size');
await dbClient.execute(`
CREATE VIEW db_size AS
SELECT
table_name AS "table",
ROUND(((data_length + index_length) / 1024 / 1024), 3) AS "size",
table_rows AS "rows"
FROM information_schema.TABLES
WHERE
table_schema = "${config.db.name}"
AND table_name <> "db_size";
`);
console.log('View Created');
await dbClient.close();
console.log('Done!');

View File

@ -4,6 +4,7 @@ import { dbClient } from '../src/db.ts';
console.log('Attempting to insert default actions into command_cnt');
const actions = [
'msg-mention',
'cmd-audit',
'cmd-delete',
'cmd-help',
'cmd-info',

View File

@ -2,6 +2,7 @@ import { Bot, CreateApplicationCommand, log, LT, MakeRequired } from '../../deps
import { Command } from '../types/commandTypes.ts';
import utils from '../utils.ts';
import audit from './audit.ts';
import info from './info.ts';
import help from './help.ts';
import report from './report.ts';
@ -10,7 +11,7 @@ import deleteCmd from './delete.ts';
import managerJLA from './managerJLA.ts';
import { gameSelectionCommand } from '../buttons/event-creation/step1-gameSelection.ts';
export const commands: Array<Command> = [deleteCmd, info, report, setup, gameSelectionCommand, managerJLA, help];
export const commands: Array<Command> = [deleteCmd, info, report, setup, gameSelectionCommand, managerJLA, help, audit];
export const createSlashCommands = async (bot: Bot) => {
const globalCommands: MakeRequired<CreateApplicationCommand, 'name'>[] = [];

88
src/commands/audit.ts Normal file
View File

@ -0,0 +1,88 @@
import config from '../../config.ts';
import { ApplicationCommandTypes, ApplicationCommandOptionTypes, Bot, Interaction, InteractionResponseTypes, DiscordEmbedField } from '../../deps.ts';
import { infoColor2, infoEmbed, isLFGChannel, somethingWentWrong } from '../commandUtils.ts';
import { dbClient, queries } from '../db.ts';
import { CommandDetails } from '../types/commandTypes.ts';
import utils from '../utils.ts';
import { auditSlashName } from './slashCommandNames.ts';
const auditDbName = 'database';
const auditCustomActivities = 'custom-activities';
const auditGuildName = 'guilds';
const details: CommandDetails = {
name: auditSlashName,
description: `Developer Command for checking in on ${config.name}'s health.`,
type: ApplicationCommandTypes.ChatInput,
defaultMemberPermissions: ['ADMINISTRATOR'],
options: [
{
name: auditDbName,
type: ApplicationCommandOptionTypes.SubCommand,
description: `Developer Command: Checks ${config.name}'s DB size.`,
},
{
name: auditCustomActivities,
type: ApplicationCommandOptionTypes.SubCommand,
description: 'Developer Command: Checks for duplicate custom activities.',
},
{
name: auditGuildName,
type: ApplicationCommandOptionTypes.SubCommand,
description: `Developer Command: Checks in on channel and member counts of guilds that ${config.name} is in.`,
},
],
};
const execute = async (bot: Bot, interaction: Interaction) => {
if (interaction.member && interaction.guildId && interaction.data?.options?.[0].options) {
dbClient.execute(queries.callIncCnt('cmd-audit')).catch((e) => utils.commonLoggers.dbError('audit.ts@inc', 'call sproc INC_CNT on', e));
const auditName = interaction.data.options[0].name;
switch (auditName) {
case auditDbName:
// Get DB statistics
const auditQuery = await dbClient.query(`SELECT * FROM db_size;`).catch((e) => utils.commonLoggers.dbError('audit.ts@dbSize', 'query', e));
// Turn all tables into embed fields, currently only properly will handle 25 tables, but we'll fix that when group up gets 26 tables
const embedFields: Array<DiscordEmbedField> = [];
auditQuery.forEach((row: any) => {
embedFields.push({
name: `${row.table}`,
value: `**Size:** ${row.size} MB
**Rows:** ${row.rows}`,
inline: true,
});
});
bot.helpers.sendInteractionResponse(
interaction.id,
interaction.token,
{
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
flags: isLFGChannel(interaction.guildId || 0n, interaction.channelId || 0n),
embeds: [{
color: infoColor2,
title: 'Database Audit',
description: 'Lists all tables with their current size and row count.',
timestamp: new Date().getTime(),
fields: embedFields.slice(0, 25),
}],
},
},
).catch((e: Error) => utils.commonLoggers.interactionSendError('audit.ts@dbSize', interaction, e));
break;
case auditCustomActivities:
case auditGuildName:
default:
somethingWentWrong(bot, interaction, `auditNameNotHandled@${auditName}`);
break;
}
} else {
somethingWentWrong(bot, interaction, 'auditMissingData')
}
};
export default {
details,
execute,
};

View File

@ -1,7 +1,8 @@
export const auditSlashName = 'audit';
export const createEventSlashName = 'create-event';
export const deleteSlashName = 'delete-lfg-channel';
export const managerJLASlashName = 'event';
export const helpSlashName = 'help';
export const infoSlashName = 'info';
export const managerJLASlashName = 'event';
export const reportSlashName = 'report';
export const setupSlashName = 'setup';
export const createEventSlashName = 'create-event';