From 5a3eac338a4502a690b07dc7bd45ba4308418503 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Fri, 17 Apr 2020 01:14:27 +1200 Subject: [PATCH] Ability to store request history in database --- src/lib/constants.js | 5 +++-- src/lib/structures/RequestHistory.js | 6 ++++++ src/lib/utils/upgradedb.js | 11 +++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/lib/constants.js b/src/lib/constants.js index 3e76488..61a70b5 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -64,7 +64,8 @@ let constants = { updater_cache_time: 2*60*1000, cache_sweep_interval: 3*60*1000, db_user_id: true, - db_post_n3: true + db_post_n3: true, + db_request_history: false }, // Instagram uses this stuff. This shouldn't be changed, except to fix a bug that hasn't yet been fixed upstream. @@ -122,7 +123,7 @@ let constants = { } }, - database_version: 2 + database_version: 3 } // Override values from config and export the result diff --git a/src/lib/structures/RequestHistory.js b/src/lib/structures/RequestHistory.js index b5f87ae..0cab94e 100644 --- a/src/lib/structures/RequestHistory.js +++ b/src/lib/structures/RequestHistory.js @@ -1,3 +1,6 @@ +const constants = require("../constants") +const db = require("../db") + class RequestHistory { /** * @param {string[]} tracked list of things that can be tracked @@ -23,6 +26,9 @@ class RequestHistory { const entry = this.store.get(key) entry.lastRequestAt = Date.now() entry.lastRequestSuccessful = success + if (constants.caching.db_request_history) { + db.prepare("INSERT INTO RequestHistory (type, success, timestamp) VALUES (?, ?, ?)").run(key, +entry.lastRequestSuccessful, entry.lastRequestAt) + } } export() { diff --git a/src/lib/utils/upgradedb.js b/src/lib/utils/upgradedb.js index 2ef7445..5cc95e6 100644 --- a/src/lib/utils/upgradedb.js +++ b/src/lib/utils/upgradedb.js @@ -38,7 +38,18 @@ const deltas = new Map([ db.prepare("ALTER TABLE Users_New RENAME TO Users") .run() })() + }], + // version 2 to version 3 + [3, function() { + db.transaction(() => { + db.prepare("DROP TABLE IF EXISTS RequestHistory") + .run() + db.prepare("CREATE TABLE RequestHistory (type TEXT NOT NULL, success INTEGER NOT NULL, timestamp INTEGER NOT NULL)") + .run() + })() }] + + ]) module.exports = async function() {