Minetrack/lib/app.js

100 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-12-30 23:03:54 +00:00
const Database = require("./database");
const PingController = require("./ping");
const Server = require("./server");
const { TimeTracker } = require("./time");
const MessageOf = require("./message");
2023-12-30 23:03:54 +00:00
const config = require("../config");
const minecraftVersions = require("../minecraft_versions");
class App {
2023-12-30 23:03:54 +00:00
serverRegistrations = [];
2023-12-30 23:03:54 +00:00
constructor() {
this.pingController = new PingController(this);
this.server = new Server(this);
this.timeTracker = new TimeTracker(this);
}
2023-12-30 23:03:54 +00:00
loadDatabase(callback) {
this.database = new Database(this);
// Setup database instance
this.database.ensureIndexes(() => {
this.database.loadGraphPoints(config.graphDuration, () => {
2022-05-22 14:28:58 +00:00
this.database.loadRecords(() => {
if (config.oldPingsCleanup && config.oldPingsCleanup.enabled) {
2023-12-30 23:03:54 +00:00
this.database.initOldPingsDelete(callback);
2022-05-22 14:28:58 +00:00
} else {
2023-12-30 23:03:54 +00:00
callback();
2022-05-22 14:28:58 +00:00
}
2023-12-30 23:03:54 +00:00
});
});
});
}
2023-12-30 23:03:54 +00:00
handleReady() {
this.server.listen(config.site.ip, config.site.port);
// Allow individual modules to manage their own task scheduling
2023-12-30 23:03:54 +00:00
this.pingController.schedule();
}
handleClientConnection = (client) => {
if (config.logToDatabase) {
2023-12-30 23:03:54 +00:00
client.on("message", (message) => {
if (message === "requestHistoryGraph") {
// Send historical graphData built from all serverRegistrations
2023-12-30 23:03:54 +00:00
const graphData = this.serverRegistrations.map(
(serverRegistration) => serverRegistration.graphData
);
// Send graphData in object wrapper to avoid needing to explicity filter
// any header data being appended by #MessageOf since the graph data is fed
// directly into the graphing system
2023-12-30 23:03:54 +00:00
client.send(
MessageOf("historyGraph", {
timestamps: this.timeTracker.getGraphPoints(),
graphData,
})
);
}
2023-12-30 23:03:54 +00:00
});
}
const initMessage = {
config: (() => {
// Remap minecraftVersion entries into name values
2023-12-30 23:03:54 +00:00
const minecraftVersionNames = {};
Object.keys(minecraftVersions).forEach(function (key) {
2023-12-30 23:03:54 +00:00
minecraftVersionNames[key] = minecraftVersions[key].map(
(version) => version.name
);
});
// Send configuration data for rendering the page
return {
2023-12-30 23:03:54 +00:00
graphDurationLabel:
config.graphDurationLabel ||
Math.floor(config.graphDuration / (60 * 60 * 1000)) + "h",
graphMaxLength: TimeTracker.getMaxGraphDataLength(),
serverGraphMaxLength: TimeTracker.getMaxServerGraphDataLength(),
2023-12-30 23:03:54 +00:00
servers: this.serverRegistrations.map((serverRegistration) =>
serverRegistration.getPublicData()
),
minecraftVersions: minecraftVersionNames,
2023-12-30 23:03:54 +00:00
isGraphVisible: config.logToDatabase,
};
})(),
timestampPoints: this.timeTracker.getServerGraphPoints(),
2023-12-30 23:03:54 +00:00
servers: this.serverRegistrations.map((serverRegistration) =>
serverRegistration.getPingHistory()
),
};
2023-12-30 23:03:54 +00:00
client.send(MessageOf("init", initMessage));
};
}
2023-12-30 23:03:54 +00:00
module.exports = App;