2020-04-21 22:59:53 +00:00
|
|
|
const sqlite = require('sqlite3')
|
|
|
|
|
2020-05-11 23:44:29 +00:00
|
|
|
const { TimeTracker } = require('./time')
|
2020-05-11 23:29:26 +00:00
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
class Database {
|
|
|
|
constructor (app) {
|
|
|
|
this._app = app
|
|
|
|
this._sql = new sqlite.Database('database.sql')
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureIndexes () {
|
|
|
|
this._sql.serialize(() => {
|
|
|
|
this._sql.run('CREATE TABLE IF NOT EXISTS pings (timestamp BIGINT NOT NULL, ip TINYTEXT, playerCount MEDIUMINT)')
|
|
|
|
this._sql.run('CREATE INDEX IF NOT EXISTS ip_index ON pings (ip, playerCount)')
|
|
|
|
this._sql.run('CREATE INDEX IF NOT EXISTS timestamp_index on PINGS (timestamp)')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 07:06:39 +00:00
|
|
|
loadGraphPoints (graphDuration, callback) {
|
|
|
|
// Query recent pings
|
|
|
|
const endTime = new Date().getTime()
|
|
|
|
const startTime = endTime - graphDuration
|
|
|
|
|
2020-05-08 07:07:59 +00:00
|
|
|
this.getRecentPings(startTime, endTime, pingData => {
|
2020-05-11 23:12:29 +00:00
|
|
|
const relativeGraphData = []
|
2020-04-21 22:59:53 +00:00
|
|
|
|
|
|
|
for (const row of pingData) {
|
2020-05-08 06:54:04 +00:00
|
|
|
// Load into temporary array
|
|
|
|
// This will be culled prior to being pushed to the serverRegistration
|
2020-05-11 23:12:29 +00:00
|
|
|
let graphData = relativeGraphData[row.ip]
|
|
|
|
if (!graphData) {
|
|
|
|
relativeGraphData[row.ip] = graphData = [[], []]
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
2020-05-08 06:54:04 +00:00
|
|
|
|
2020-05-11 23:12:29 +00:00
|
|
|
// DANGER!
|
|
|
|
// This will pull the timestamp from each row into memory
|
|
|
|
// This is built under the assumption that each round of pings shares the same timestamp
|
|
|
|
// This enables all timestamp arrays to have consistent point selection and graph correctly
|
|
|
|
graphData[0].push(row.timestamp)
|
|
|
|
graphData[1].push(row.playerCount)
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 23:12:29 +00:00
|
|
|
Object.keys(relativeGraphData).forEach(ip => {
|
2020-04-21 22:59:53 +00:00
|
|
|
// Match IPs to serverRegistration object
|
|
|
|
for (const serverRegistration of this._app.serverRegistrations) {
|
|
|
|
if (serverRegistration.data.ip === ip) {
|
2020-05-11 23:12:29 +00:00
|
|
|
const graphData = relativeGraphData[ip]
|
2020-04-21 22:59:53 +00:00
|
|
|
|
|
|
|
// Push the data into the instance and cull if needed
|
2020-05-11 23:12:29 +00:00
|
|
|
serverRegistration.loadGraphPoints(startTime, graphData[0], graphData[1])
|
2020-04-21 22:59:53 +00:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-11 23:12:29 +00:00
|
|
|
// Since all timestamps are shared, use the array from the first ServerRegistration
|
|
|
|
// This is very dangerous and can break if data is out of sync
|
|
|
|
if (Object.keys(relativeGraphData).length > 0) {
|
|
|
|
const serverIp = Object.keys(relativeGraphData)[0]
|
|
|
|
const timestamps = relativeGraphData[serverIp][0]
|
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
this._app.timeTracker.loadGraphPoints(startTime, timestamps)
|
2020-05-11 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
loadRecords (callback) {
|
|
|
|
let completedTasks = 0
|
|
|
|
|
|
|
|
this._app.serverRegistrations.forEach(serverRegistration => {
|
|
|
|
// Find graphPeaks
|
|
|
|
// This pre-computes the values prior to clients connecting
|
|
|
|
serverRegistration.findNewGraphPeak()
|
|
|
|
|
|
|
|
// Query recordData
|
|
|
|
// When complete increment completeTasks to know when complete
|
|
|
|
this.getRecord(serverRegistration.data.ip, (playerCount, timestamp) => {
|
|
|
|
serverRegistration.recordData = {
|
2020-05-11 09:12:46 +00:00
|
|
|
playerCount,
|
2020-05-11 23:29:26 +00:00
|
|
|
timestamp: TimeTracker.toSeconds(timestamp)
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if completedTasks hit the finish value
|
|
|
|
// Fire callback since #readyDatabase is complete
|
|
|
|
if (++completedTasks === this._app.serverRegistrations.length) {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 07:06:39 +00:00
|
|
|
getRecentPings (startTime, endTime, callback) {
|
|
|
|
this._sql.all('SELECT * FROM pings WHERE timestamp >= ? AND timestamp <= ?', [
|
|
|
|
startTime,
|
|
|
|
endTime
|
2020-04-21 22:59:53 +00:00
|
|
|
], (_, data) => callback(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
getRecord (ip, callback) {
|
|
|
|
this._sql.all('SELECT MAX(playerCount), timestamp FROM pings WHERE ip = ?', [
|
|
|
|
ip
|
|
|
|
], (_, data) => callback(data[0]['MAX(playerCount)'], data[0].timestamp))
|
|
|
|
}
|
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
insertPing (ip, timestamp, unsafePlayerCount) {
|
2020-04-21 22:59:53 +00:00
|
|
|
const statement = this._sql.prepare('INSERT INTO pings (timestamp, ip, playerCount) VALUES (?, ?, ?)')
|
2020-05-11 23:29:26 +00:00
|
|
|
statement.run(timestamp, ip, unsafePlayerCount)
|
2020-04-21 22:59:53 +00:00
|
|
|
statement.finalize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Database
|