Modified old pings cleanup config, ip as primary key in player records table

This commit is contained in:
mjezek 2022-10-29 18:51:14 +02:00
parent 37a1579b37
commit 4f81041e97
No known key found for this signature in database
GPG Key ID: 724CD28A57A1A1BE
3 changed files with 18 additions and 12 deletions

@ -7,9 +7,12 @@
"pingAll": 3000, "pingAll": 3000,
"connectTimeout": 2500 "connectTimeout": 2500
}, },
"oldPingsCleanup": {
"enabled": false,
"interval": 3600000
},
"logFailedPings": true, "logFailedPings": true,
"logToDatabase": false, "logToDatabase": false,
"deleteOldPings": false,
"graphDuration": 86400000, "graphDuration": 86400000,
"serverGraphDuration": 180000 "serverGraphDuration": 180000
} }

@ -23,7 +23,7 @@ class App {
this.database.ensureIndexes(() => { this.database.ensureIndexes(() => {
this.database.loadGraphPoints(config.graphDuration, () => { this.database.loadGraphPoints(config.graphDuration, () => {
this.database.loadRecords(() => { this.database.loadRecords(() => {
if (config.deleteOldPings) { if (config.oldPingsCleanup && config.oldPingsCleanup.enabled) {
this.database.initOldPingsDelete(callback) this.database.initOldPingsDelete(callback)
} else { } else {
callback() callback()

@ -52,7 +52,7 @@ class Database {
this._sql.serialize(() => { 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 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 ip_index ON pings (ip, playerCount)', handleError)
this._sql.run('CREATE INDEX IF NOT EXISTS timestamp_index on PINGS (timestamp)', [], err => { this._sql.run('CREATE INDEX IF NOT EXISTS timestamp_index on PINGS (timestamp)', [], err => {
handleError(err) handleError(err)
@ -268,17 +268,20 @@ class Database {
} }
initOldPingsDelete (callback) { initOldPingsDelete (callback) {
// Delete old ping records on startup // Delete old pings on startup
logger.info('Deleting old ping records..') logger.info('Deleting old pings..')
this.deleteOldPingRecords(() => { this.deleteOldPings(() => {
// Delete old ping records every hour const oldPingsCleanupInterval = config.oldPingsCleanup.interval || 3600000
setInterval(() => this.deleteOldPingRecords(), 3600000) if (oldPingsCleanupInterval > 0) {
// Delete old pings periodically
setInterval(() => this.deleteOldPings(), oldPingsCleanupInterval)
}
callback() callback()
}) })
} }
deleteOldPingRecords (callback) { deleteOldPings (callback) {
// The oldest timestamp that will be kept // The oldest timestamp that will be kept
const oldestTimestamp = TimeTracker.getEpochMillis() - config.graphDuration const oldestTimestamp = TimeTracker.getEpochMillis() - config.graphDuration
@ -286,13 +289,13 @@ class Database {
const statement = this._sql.prepare('DELETE FROM pings WHERE timestamp < ?;') const statement = this._sql.prepare('DELETE FROM pings WHERE timestamp < ?;')
statement.run(oldestTimestamp, err => { statement.run(oldestTimestamp, err => {
if (err) { if (err) {
logger.error('Cannot delete old ping records') logger.error('Cannot delete old pings')
throw err throw err
} else { } else {
const deleteTook = TimeTracker.getEpochMillis() - deleteStart 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() callback()
} }
} }