2023-12-30 23:03:54 +00:00
|
|
|
const sqlite = require("sqlite3");
|
2020-04-21 22:59:53 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
const logger = require("./logger");
|
2021-04-21 20:29:57 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
const config = require("../config");
|
|
|
|
const { TimeTracker } = require("./time");
|
|
|
|
const dataFolder = "data/";
|
2020-05-11 23:29:26 +00:00
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
class Database {
|
2023-12-30 23:03:54 +00:00
|
|
|
constructor(app) {
|
|
|
|
this._app = app;
|
|
|
|
this._sql = new sqlite.Database(dataFolder + "database.sql");
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
getDailyDatabase() {
|
2020-08-01 19:46:04 +00:00
|
|
|
if (!config.createDailyDatabaseCopy) {
|
2023-12-30 23:03:54 +00:00
|
|
|
return;
|
2020-08-01 19:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
const date = new Date();
|
|
|
|
const fileName = `database_copy_${date.getDate()}-${
|
|
|
|
date.getMonth() + 1
|
|
|
|
}-${date.getFullYear()}.sql`;
|
2020-08-01 19:46:04 +00:00
|
|
|
|
|
|
|
if (fileName !== this._currentDatabaseCopyFileName) {
|
|
|
|
if (this._currentDatabaseCopyInstance) {
|
2023-12-30 23:03:54 +00:00
|
|
|
this._currentDatabaseCopyInstance.close();
|
2020-08-01 19:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
this._currentDatabaseCopyInstance = new sqlite.Database(
|
|
|
|
dataFolder + fileName
|
|
|
|
);
|
|
|
|
this._currentDatabaseCopyFileName = fileName;
|
2020-08-01 19:46:04 +00:00
|
|
|
|
|
|
|
// Ensure the initial tables are created
|
|
|
|
// This does not created indexes since it is only inserted to
|
|
|
|
this._currentDatabaseCopyInstance.serialize(() => {
|
2023-12-30 23:03:54 +00:00
|
|
|
this._currentDatabaseCopyInstance.run(
|
|
|
|
"CREATE TABLE IF NOT EXISTS pings (timestamp BIGINT NOT NULL, ip TINYTEXT, playerCount MEDIUMINT)",
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
logger.log(
|
|
|
|
"error",
|
|
|
|
"Cannot create initial table for daily database"
|
|
|
|
);
|
|
|
|
throw err;
|
|
|
|
}
|
2021-04-21 20:29:57 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
);
|
|
|
|
});
|
2020-08-01 19:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
return this._currentDatabaseCopyInstance;
|
2020-08-01 19:46:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
ensureIndexes(callback) {
|
|
|
|
const handleError = (err) => {
|
2021-04-21 20:29:57 +00:00
|
|
|
if (err) {
|
2023-12-30 23:03:54 +00:00
|
|
|
logger.log("error", "Cannot create table or table index");
|
|
|
|
throw err;
|
2021-04-21 20:29:57 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
};
|
2021-04-21 20:29:57 +00:00
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
this._sql.serialize(() => {
|
2023-12-30 23:03:54 +00:00
|
|
|
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 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);
|
|
|
|
// Queries are executed one at a time; this is the last one.
|
|
|
|
// Note that queries not scheduled directly in the callback function of
|
|
|
|
// #serialize are not necessarily serialized.
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
loadGraphPoints(graphDuration, callback) {
|
2020-05-08 07:06:39 +00:00
|
|
|
// Query recent pings
|
2023-12-30 23:03:54 +00:00
|
|
|
const endTime = TimeTracker.getEpochMillis();
|
|
|
|
const startTime = endTime - graphDuration;
|
2020-05-08 07:06:39 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
this.getRecentPings(startTime, endTime, (pingData) => {
|
|
|
|
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
|
2023-12-30 23:03:54 +00:00
|
|
|
let graphData = relativeGraphData[row.ip];
|
2020-05-11 23:12:29 +00:00
|
|
|
if (!graphData) {
|
2023-12-30 23:03:54 +00:00
|
|
|
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
|
2023-12-30 23:03:54 +00:00
|
|
|
graphData[0].push(row.timestamp);
|
|
|
|
graphData[1].push(row.playerCount);
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +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) {
|
2023-12-30 23:03:54 +00:00
|
|
|
const graphData = relativeGraphData[ip];
|
2020-04-21 22:59:53 +00:00
|
|
|
|
|
|
|
// Push the data into the instance and cull if needed
|
2023-12-30 23:03:54 +00:00
|
|
|
serverRegistration.loadGraphPoints(
|
|
|
|
startTime,
|
|
|
|
graphData[0],
|
|
|
|
graphData[1]
|
|
|
|
);
|
2020-04-21 22:59:53 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
break;
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
});
|
2020-04-21 22:59:53 +00:00
|
|
|
|
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) {
|
2023-12-30 23:03:54 +00:00
|
|
|
const serverIp = Object.keys(relativeGraphData)[0];
|
|
|
|
const timestamps = relativeGraphData[serverIp][0];
|
2020-05-11 23:12:29 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
this._app.timeTracker.loadGraphPoints(startTime, timestamps);
|
2020-05-11 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
callback();
|
|
|
|
});
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
loadRecords(callback) {
|
|
|
|
let completedTasks = 0;
|
2020-04-21 22:59:53 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
this._app.serverRegistrations.forEach((serverRegistration) => {
|
2020-04-21 22:59:53 +00:00
|
|
|
// Find graphPeaks
|
|
|
|
// This pre-computes the values prior to clients connecting
|
2023-12-30 23:03:54 +00:00
|
|
|
serverRegistration.findNewGraphPeak();
|
2020-04-21 22:59:53 +00:00
|
|
|
|
|
|
|
// Query recordData
|
|
|
|
// When complete increment completeTasks to know when complete
|
2023-12-30 23:03:54 +00:00
|
|
|
this.getRecord(
|
|
|
|
serverRegistration.data.ip,
|
|
|
|
(hasRecord, playerCount, timestamp) => {
|
|
|
|
if (hasRecord) {
|
2022-03-13 11:47:45 +00:00
|
|
|
serverRegistration.recordData = {
|
2023-12-30 23:03:54 +00:00
|
|
|
playerCount,
|
|
|
|
timestamp: TimeTracker.toSeconds(timestamp),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.getRecordLegacy(
|
|
|
|
serverRegistration.data.ip,
|
|
|
|
(hasRecordLegacy, playerCountLegacy, timestampLegacy) => {
|
|
|
|
// New values that will be inserted to table
|
|
|
|
let newTimestamp = null;
|
|
|
|
let newPlayerCount = null;
|
|
|
|
|
|
|
|
// If legacy record found, use it for insertion
|
|
|
|
if (hasRecordLegacy) {
|
|
|
|
newTimestamp = timestampLegacy;
|
|
|
|
newPlayerCount = playerCountLegacy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set record to recordData
|
|
|
|
serverRegistration.recordData = {
|
|
|
|
playerCount: newPlayerCount,
|
|
|
|
timestamp: TimeTracker.toSeconds(newTimestamp),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Insert server entry to records table
|
|
|
|
const statement = this._sql.prepare(
|
|
|
|
"INSERT INTO players_record (timestamp, ip, playerCount) VALUES (?, ?, ?)"
|
|
|
|
);
|
|
|
|
statement.run(
|
|
|
|
newTimestamp,
|
|
|
|
serverRegistration.data.ip,
|
|
|
|
newPlayerCount,
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(
|
|
|
|
`Cannot insert initial player count record of ${serverRegistration.data.ip}`
|
|
|
|
);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
statement.finalize();
|
2022-03-13 11:47:45 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
);
|
|
|
|
}
|
2020-04-21 22:59:53 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
// Check if completedTasks hit the finish value
|
|
|
|
// Fire callback since #readyDatabase is complete
|
|
|
|
if (++completedTasks === this._app.serverRegistrations.length) {
|
|
|
|
callback();
|
|
|
|
}
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
);
|
|
|
|
});
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
getRecentPings(startTime, endTime, callback) {
|
|
|
|
this._sql.all(
|
|
|
|
"SELECT * FROM pings WHERE timestamp >= ? AND timestamp <= ?",
|
|
|
|
[startTime, endTime],
|
|
|
|
(err, data) => {
|
|
|
|
if (err) {
|
|
|
|
logger.log("error", "Cannot get recent pings");
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
callback(data);
|
2021-04-21 20:29:57 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
);
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
getRecord(ip, callback) {
|
|
|
|
this._sql.all(
|
|
|
|
"SELECT playerCount, timestamp FROM players_record WHERE ip = ?",
|
|
|
|
[ip],
|
|
|
|
(err, data) => {
|
|
|
|
if (err) {
|
|
|
|
logger.log("error", `Cannot get ping record for ${ip}`);
|
|
|
|
throw err;
|
|
|
|
}
|
2021-04-21 20:29:57 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
// Record not found
|
|
|
|
if (data[0] === undefined) {
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-13 11:47:45 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
const playerCount = data[0].playerCount;
|
|
|
|
const timestamp = data[0].timestamp;
|
2022-03-13 11:47:45 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
// Allow null player counts and timestamps, the frontend will safely handle them
|
|
|
|
callback(true, playerCount, timestamp);
|
|
|
|
}
|
|
|
|
);
|
2022-03-13 11:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieves record from pings table, used for converting to separate table
|
2023-12-30 23:03:54 +00:00
|
|
|
getRecordLegacy(ip, callback) {
|
|
|
|
this._sql.all(
|
|
|
|
"SELECT MAX(playerCount), timestamp FROM pings WHERE ip = ?",
|
|
|
|
[ip],
|
|
|
|
(err, data) => {
|
|
|
|
if (err) {
|
|
|
|
logger.log("error", `Cannot get legacy ping record for ${ip}`);
|
|
|
|
throw err;
|
|
|
|
}
|
2022-03-13 11:47:45 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
// For empty results, data will be length 1 with [null, null]
|
|
|
|
const playerCount = data[0]["MAX(playerCount)"];
|
|
|
|
const timestamp = data[0].timestamp;
|
2020-05-21 00:56:10 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
// Allow null timestamps, the frontend will safely handle them
|
|
|
|
// This allows insertion of free standing records without a known timestamp
|
|
|
|
if (playerCount !== null) {
|
|
|
|
callback(true, playerCount, timestamp);
|
|
|
|
} else {
|
|
|
|
callback(false);
|
|
|
|
}
|
2020-05-21 00:56:10 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
);
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
insertPing(ip, timestamp, unsafePlayerCount) {
|
|
|
|
this._insertPingTo(ip, timestamp, unsafePlayerCount, this._sql);
|
2020-08-01 19:46:04 +00:00
|
|
|
|
|
|
|
// Push a copy of the data into the database copy, if any
|
|
|
|
// This creates an "insert only" copy of the database for archiving
|
2023-12-30 23:03:54 +00:00
|
|
|
const dailyDatabase = this.getDailyDatabase();
|
2020-08-01 19:46:04 +00:00
|
|
|
if (dailyDatabase) {
|
2023-12-30 23:03:54 +00:00
|
|
|
this._insertPingTo(ip, timestamp, unsafePlayerCount, dailyDatabase);
|
2020-08-01 19:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
_insertPingTo(ip, timestamp, unsafePlayerCount, db) {
|
|
|
|
const statement = db.prepare(
|
|
|
|
"INSERT INTO pings (timestamp, ip, playerCount) VALUES (?, ?, ?)"
|
|
|
|
);
|
|
|
|
statement.run(timestamp, ip, unsafePlayerCount, (err) => {
|
2021-04-21 20:29:57 +00:00
|
|
|
if (err) {
|
2023-12-30 23:03:54 +00:00
|
|
|
logger.error(`Cannot insert ping record of ${ip} at ${timestamp}`);
|
|
|
|
throw err;
|
2021-04-21 20:29:57 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
});
|
|
|
|
statement.finalize();
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
2022-03-13 11:47:45 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
updatePlayerCountRecord(ip, playerCount, timestamp) {
|
|
|
|
const statement = this._sql.prepare(
|
|
|
|
"UPDATE players_record SET timestamp = ?, playerCount = ? WHERE ip = ?"
|
|
|
|
);
|
|
|
|
statement.run(timestamp, playerCount, ip, (err) => {
|
2022-03-13 11:47:45 +00:00
|
|
|
if (err) {
|
2023-12-30 23:03:54 +00:00
|
|
|
logger.error(
|
|
|
|
`Cannot update player count record of ${ip} at ${timestamp}`
|
|
|
|
);
|
|
|
|
throw err;
|
2022-03-13 11:47:45 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
});
|
|
|
|
statement.finalize();
|
2022-03-13 11:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
initOldPingsDelete(callback) {
|
2022-10-29 16:51:14 +00:00
|
|
|
// Delete old pings on startup
|
2023-12-30 23:03:54 +00:00
|
|
|
logger.info("Deleting old pings..");
|
2022-10-29 16:51:14 +00:00
|
|
|
this.deleteOldPings(() => {
|
2023-12-30 23:03:54 +00:00
|
|
|
const oldPingsCleanupInterval =
|
|
|
|
config.oldPingsCleanup.interval || 3600000;
|
2022-10-29 16:51:14 +00:00
|
|
|
if (oldPingsCleanupInterval > 0) {
|
|
|
|
// Delete old pings periodically
|
2023-12-30 23:03:54 +00:00
|
|
|
setInterval(() => this.deleteOldPings(), oldPingsCleanupInterval);
|
2022-10-29 16:51:14 +00:00
|
|
|
}
|
2022-05-22 14:28:58 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
callback();
|
|
|
|
});
|
2022-03-13 11:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
deleteOldPings(callback) {
|
2022-03-13 11:47:45 +00:00
|
|
|
// The oldest timestamp that will be kept
|
2023-12-30 23:03:54 +00:00
|
|
|
const oldestTimestamp = TimeTracker.getEpochMillis() - config.graphDuration;
|
2022-03-13 11:47:45 +00:00
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
const deleteStart = TimeTracker.getEpochMillis();
|
|
|
|
const statement = this._sql.prepare(
|
|
|
|
"DELETE FROM pings WHERE timestamp < ?;"
|
|
|
|
);
|
|
|
|
statement.run(oldestTimestamp, (err) => {
|
2022-03-13 11:47:45 +00:00
|
|
|
if (err) {
|
2023-12-30 23:03:54 +00:00
|
|
|
logger.error("Cannot delete old pings");
|
|
|
|
throw err;
|
2022-03-13 11:47:45 +00:00
|
|
|
} else {
|
2023-12-30 23:03:54 +00:00
|
|
|
const deleteTook = TimeTracker.getEpochMillis() - deleteStart;
|
|
|
|
logger.info(`Old pings deleted in ${deleteTook}ms`);
|
2022-05-22 14:28:58 +00:00
|
|
|
|
2022-10-29 16:51:14 +00:00
|
|
|
if (callback) {
|
2023-12-30 23:03:54 +00:00
|
|
|
callback();
|
2022-05-22 14:28:58 +00:00
|
|
|
}
|
2022-03-13 11:47:45 +00:00
|
|
|
}
|
2023-12-30 23:03:54 +00:00
|
|
|
});
|
|
|
|
statement.finalize();
|
2022-03-13 11:47:45 +00:00
|
|
|
}
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:03:54 +00:00
|
|
|
module.exports = Database;
|