From b479cd29484e1c883b4beebc64861a58e7837f60 Mon Sep 17 00:00:00 2001 From: Dessa Simpson Date: Sun, 25 Oct 2020 21:31:18 -0700 Subject: [PATCH] Rewrite ban insert query to use one insert rather than one per ban Fixes #21 --- src/app.ts | 21 ++++++++++++++++++--- src/queries.ts | 5 ----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/app.ts b/src/app.ts index 242f4de..f7ab392 100644 --- a/src/app.ts +++ b/src/app.ts @@ -313,10 +313,24 @@ async function processBannedUsers() { dbconn.query("DELETE FROM bans"); log(LogLevel.DEBUG,"Ban list:") log(LogLevel.DEBUG,JSON.stringify(response.data,null,2)); + var insertBanQuery = "INSERT INTO bans (userid) VALUES "; + var banRow = 0; + var bansArray: number[] = []; for (var ban of response.data) { - if (ban.expires_at == '') dbconn.query(Object.assign(queries.insertBan,{ values: [ban.user_id] })) + if (ban.expires_at == '') { + banRow++; + insertBanQuery += `($${banRow}), `; + bansArray.push(ban.user_id as number); + } } - db.query("CALL update_scores()"); + var banQueryConfig = { + text: insertBanQuery.slice(0,-2), // Cut last `, ` off of the end + values: bansArray + }; + log(LogLevel.DEBUG,"banQueryConfig object:") + log(LogLevel.DEBUG,JSON.stringify(banQueryConfig,null,2)) + dbconn.query(banQueryConfig); + dbconn.query("CALL update_scores()"); await dbconn.query('COMMIT'); } catch (e) { await dbconn.query('ROLLBACK'); @@ -326,7 +340,8 @@ async function processBannedUsers() { setTimeout(processBannedUsers,3600000+Math.floor(Math.random()*900000)) // Run every 1-1.25 hours to balance load } -setTimeout(processBannedUsers,3600000+Math.floor(Math.random()*900000)) // Run every 1-1.25 hours to balance load +//setTimeout(processBannedUsers,3600000+Math.floor(Math.random()*900000)) // Run every 1-1.25 hours to balance load +processBannedUsers(); app.listen(config.port, () => { console.log(`Listening on port ${config.port}`); diff --git a/src/queries.ts b/src/queries.ts index 2e288a7..eb2d952 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -10,11 +10,6 @@ export const checkBan = { text: "SELECT userid FROM bans WHERE userid = $1" } -export const insertBan = { - name: "insertBan", - text: "INSERT INTO bans (userid) VALUES ($1)" -} - // Streamer-related queries export const getStreamerId = { name: "getStreamerId",