Create roll heatmap table and sproc, add dailyRate to command_cnt table

This commit is contained in:
Ean Milligan (Bastion) 2022-06-25 02:09:41 -04:00
parent 011120845e
commit b69cf2060f
4 changed files with 56 additions and 2 deletions

View File

@ -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 all_keys;`);
await dbClient.execute(`DROP TABLE IF EXISTS allowed_guilds;`); await dbClient.execute(`DROP TABLE IF EXISTS allowed_guilds;`);
await dbClient.execute(`DROP TABLE IF EXISTS roll_log;`); 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 PROCEDURE IF EXISTS INC_CNT;`);
await dbClient.execute(`DROP TABLE IF EXISTS command_cnt;`); await dbClient.execute(`DROP TABLE IF EXISTS command_cnt;`);
console.log('Tables dropped'); console.log('Tables dropped');
@ -25,13 +27,14 @@ await dbClient.execute(`
CREATE TABLE command_cnt ( CREATE TABLE command_cnt (
command char(20) NOT NULL, command char(20) NOT NULL,
count bigint unsigned NOT NULL DEFAULT 0, count bigint unsigned NOT NULL DEFAULT 0,
dailyRate float unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (command), PRIMARY KEY (command),
UNIQUE KEY command_cnt_command_UNIQUE (command) UNIQUE KEY command_cnt_command_UNIQUE (command)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`); `);
console.log('Table created'); console.log('Table created');
console.log('Attempt creating increment Stored Procedure'); console.log('Attempt creating increment count Stored Procedure');
await dbClient.execute(` await dbClient.execute(`
CREATE PROCEDURE INC_CNT( CREATE PROCEDURE INC_CNT(
IN cmd CHAR(20) IN cmd CHAR(20)
@ -44,6 +47,44 @@ await dbClient.execute(`
`); `);
console.log('Stored Procedure created'); 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 // Roll log, holds rolls when requests
console.log('Attempting to create table roll_log'); console.log('Attempting to create table roll_log');
await dbClient.execute(` await dbClient.execute(`

View File

@ -18,5 +18,13 @@ for (const command of commands) {
} }
console.log('Insertion done'); 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(); await dbClient.close();
console.log('Done!'); console.log('Done!');

View File

@ -16,7 +16,9 @@ import utils from '../utils.ts';
export const roll = async (message: DiscordenoMessage, args: string[], command: string) => { export const roll = async (message: DiscordenoMessage, args: string[], command: string) => {
// Light telemetry to see how many times a command is being run // 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 is on, only allow this command to be used in the devServer
if (DEVMODE && message.guildId !== config.devServer) { if (DEVMODE && message.guildId !== config.devServer) {

View File

@ -10,7 +10,10 @@ export const dbClient = await new Client().connect({
password: config.db.password, password: config.db.password,
}); });
const weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
export const queries = { export const queries = {
insertRollLogCmd: (api: number, error: number) => `INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,${api},${error})`, insertRollLogCmd: (api: number, error: number) => `INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,${api},${error})`,
callIncCnt: (cmdName: string) => `CALL INC_CNT("${cmdName}");`, callIncCnt: (cmdName: string) => `CALL INC_CNT("${cmdName}");`,
callIncHeatmap: (dateObj: Date) => `CALL INC_HEATMAP("${weekDays[dateObj.getDay()]}", ${dateObj.getHours()});`,
}; };