From b69cf2060f4505ead04fa8f60c1a0568c03ae065 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Sat, 25 Jun 2022 02:09:41 -0400 Subject: [PATCH] Create roll heatmap table and sproc, add dailyRate to command_cnt table --- db/initialize.ts | 43 +++++++++++++++++++++++++++++++++++++++++- db/populateDefaults.ts | 8 ++++++++ src/commands/roll.ts | 4 +++- src/db.ts | 3 +++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/db/initialize.ts b/db/initialize.ts index 321233a..9b28923 100644 --- a/db/initialize.ts +++ b/db/initialize.ts @@ -15,6 +15,8 @@ await dbClient.execute(`DROP TABLE IF EXISTS allowed_channels;`); await dbClient.execute(`DROP TABLE IF EXISTS all_keys;`); await dbClient.execute(`DROP TABLE IF EXISTS allowed_guilds;`); await dbClient.execute(`DROP TABLE IF EXISTS roll_log;`); +await dbClient.execute(`DROP PROCEDURE IF EXISTS INC_HEATMAP;`); +await dbClient.execute(`DROP TABLE IF EXISTS roll_time_heatmap;`); await dbClient.execute(`DROP PROCEDURE IF EXISTS INC_CNT;`); await dbClient.execute(`DROP TABLE IF EXISTS command_cnt;`); console.log('Tables dropped'); @@ -25,13 +27,14 @@ await dbClient.execute(` CREATE TABLE command_cnt ( command char(20) NOT NULL, count bigint unsigned NOT NULL DEFAULT 0, + dailyRate float unsigned NOT NULL DEFAULT 0, PRIMARY KEY (command), UNIQUE KEY command_cnt_command_UNIQUE (command) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; `); console.log('Table created'); -console.log('Attempt creating increment Stored Procedure'); +console.log('Attempt creating increment count Stored Procedure'); await dbClient.execute(` CREATE PROCEDURE INC_CNT( IN cmd CHAR(20) @@ -44,6 +47,44 @@ await dbClient.execute(` `); console.log('Stored Procedure created'); +// Holds daily average of commands +console.log('Attempting to create table roll_time_heatmap'); +await dbClient.execute(` + CREATE TABLE roll_time_heatmap ( + hour tinyint(1) unsigned NOT NULL, + sunday bigint unsigned NOT NULL DEFAULT 0, + monday bigint unsigned NOT NULL DEFAULT 0, + tuesday bigint unsigned NOT NULL DEFAULT 0, + wednesday bigint unsigned NOT NULL DEFAULT 0, + thursday bigint unsigned NOT NULL DEFAULT 0, + friday bigint unsigned NOT NULL DEFAULT 0, + saturday bigint unsigned NOT NULL DEFAULT 0, + PRIMARY KEY (hour), + UNIQUE KEY roll_time_heatmap_hour_UNIQUE (hour) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +`); +console.log('Table created'); + +console.log('Attempt creating increment heatmap Stored Procedure'); +await dbClient.execute(` + CREATE PROCEDURE INC_HEATMAP( + IN dy varchar(10), + IN hr tinyint(1) + ) + BEGIN + SET @s1=CONCAT('SELECT ',dy,' FROM roll_time_heatmap WHERE hour = ',hr,' INTO @oldcnt'); + PREPARE stmt1 FROM @s1; + EXECUTE stmt1; + DEALLOCATE PREPARE stmt1; + + SET @s2=CONCAT('UPDATE roll_time_heatmap SET ',dy,' = @oldcnt + 1 WHERE hour = ',hr); + PREPARE stmt2 FROM @s2; + EXECUTE stmt2; + DEALLOCATE PREPARE stmt2; + END +`); +console.log('Stored Procedure created'); + // Roll log, holds rolls when requests console.log('Attempting to create table roll_log'); await dbClient.execute(` diff --git a/db/populateDefaults.ts b/db/populateDefaults.ts index 5c17030..bd2c4bb 100644 --- a/db/populateDefaults.ts +++ b/db/populateDefaults.ts @@ -18,5 +18,13 @@ for (const command of commands) { } console.log('Insertion done'); +console.log('Attempting to insert default hours into roll_time_heatmap'); +for (let i = 0; i <= 23; i++) { + await dbClient.execute('INSERT INTO roll_time_heatmap(hour) values(?)', [i]).catch((e) => { + console.log(`Failed to insert hour ${i} into database`, e); + }); +} +console.log('Insertion done'); + await dbClient.close(); console.log('Done!'); diff --git a/src/commands/roll.ts b/src/commands/roll.ts index 1a21f48..d84c6e0 100644 --- a/src/commands/roll.ts +++ b/src/commands/roll.ts @@ -16,7 +16,9 @@ import utils from '../utils.ts'; export const roll = async (message: DiscordenoMessage, args: string[], command: string) => { // Light telemetry to see how many times a command is being run - dbClient.execute(queries.callIncCnt('roll')).catch((e) => utils.commonLoggers.dbError('roll.ts:19', 'call sproc INC_CNT on', e)); + const currDateTime = new Date(); + dbClient.execute(queries.callIncCnt('roll')).catch((e) => utils.commonLoggers.dbError('roll.ts:20', 'call sproc INC_CNT on', e)); + dbClient.execute(queries.callIncHeatmap(currDateTime)).catch((e) => utils.commonLoggers.dbError('roll.ts:21', 'update', e)); // If DEVMODE is on, only allow this command to be used in the devServer if (DEVMODE && message.guildId !== config.devServer) { diff --git a/src/db.ts b/src/db.ts index ec83c63..0236c59 100644 --- a/src/db.ts +++ b/src/db.ts @@ -10,7 +10,10 @@ export const dbClient = await new Client().connect({ password: config.db.password, }); +const weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; + export const queries = { insertRollLogCmd: (api: number, error: number) => `INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,${api},${error})`, callIncCnt: (cmdName: string) => `CALL INC_CNT("${cmdName}");`, + callIncHeatmap: (dateObj: Date) => `CALL INC_HEATMAP("${weekDays[dateObj.getDay()]}", ${dateObj.getHours()});`, };