From 4f81041e97157e3fff4914dcad63cb6045b129a3 Mon Sep 17 00:00:00 2001 From: mjezek Date: Sat, 29 Oct 2022 18:51:14 +0200 Subject: [PATCH] Modified old pings cleanup config, ip as primary key in player records table --- config.json | 5 ++++- lib/app.js | 2 +- lib/database.js | 23 +++++++++++++---------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/config.json b/config.json index 462577c..d8d84f9 100644 --- a/config.json +++ b/config.json @@ -7,9 +7,12 @@ "pingAll": 3000, "connectTimeout": 2500 }, + "oldPingsCleanup": { + "enabled": false, + "interval": 3600000 + }, "logFailedPings": true, "logToDatabase": false, - "deleteOldPings": false, "graphDuration": 86400000, "serverGraphDuration": 180000 } diff --git a/lib/app.js b/lib/app.js index ddc4408..965438e 100644 --- a/lib/app.js +++ b/lib/app.js @@ -23,7 +23,7 @@ class App { this.database.ensureIndexes(() => { this.database.loadGraphPoints(config.graphDuration, () => { this.database.loadRecords(() => { - if (config.deleteOldPings) { + if (config.oldPingsCleanup && config.oldPingsCleanup.enabled) { this.database.initOldPingsDelete(callback) } else { callback() diff --git a/lib/database.js b/lib/database.js index 9b4828b..c0d5caa 100644 --- a/lib/database.js +++ b/lib/database.js @@ -52,7 +52,7 @@ class Database { this._sql.serialize(() => { this._sql.run('CREATE TABLE IF NOT EXISTS pings (timestamp BIGINT NOT NULL, ip TINYTEXT, playerCount MEDIUMINT)', handleError) - this._sql.run('CREATE TABLE IF NOT EXISTS players_record (timestamp BIGINT, ip TINYTEXT, playerCount MEDIUMINT)', handleError) + this._sql.run('CREATE TABLE IF NOT EXISTS players_record (timestamp BIGINT, ip TINYTEXT NOT NULL PRIMARY KEY, playerCount MEDIUMINT)', handleError) this._sql.run('CREATE INDEX IF NOT EXISTS ip_index ON pings (ip, playerCount)', handleError) this._sql.run('CREATE INDEX IF NOT EXISTS timestamp_index on PINGS (timestamp)', [], err => { handleError(err) @@ -268,17 +268,20 @@ class Database { } initOldPingsDelete (callback) { - // Delete old ping records on startup - logger.info('Deleting old ping records..') - this.deleteOldPingRecords(() => { - // Delete old ping records every hour - setInterval(() => this.deleteOldPingRecords(), 3600000) + // Delete old pings on startup + logger.info('Deleting old pings..') + this.deleteOldPings(() => { + const oldPingsCleanupInterval = config.oldPingsCleanup.interval || 3600000 + if (oldPingsCleanupInterval > 0) { + // Delete old pings periodically + setInterval(() => this.deleteOldPings(), oldPingsCleanupInterval) + } callback() }) } - deleteOldPingRecords (callback) { + deleteOldPings (callback) { // The oldest timestamp that will be kept const oldestTimestamp = TimeTracker.getEpochMillis() - config.graphDuration @@ -286,13 +289,13 @@ class Database { const statement = this._sql.prepare('DELETE FROM pings WHERE timestamp < ?;') statement.run(oldestTimestamp, err => { if (err) { - logger.error('Cannot delete old ping records') + logger.error('Cannot delete old pings') throw err } else { const deleteTook = TimeTracker.getEpochMillis() - deleteStart - logger.info(`Old ping records deleted in ${deleteTook}ms`) + logger.info(`Old pings deleted in ${deleteTook}ms`) - if (callback !== undefined) { + if (callback) { callback() } }