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,
"connectTimeout": 2500
},
"oldPingsCleanup": {
"enabled": false,
"interval": 3600000
},
"logFailedPings": true,
"logToDatabase": false,
"deleteOldPings": false,
"graphDuration": 86400000,
"serverGraphDuration": 180000
}

@ -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()

@ -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()
}
}