From 7322b8dc82d9e013056634f4c89bff282782f4ab Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Wed, 29 Apr 2020 04:21:50 -0500 Subject: [PATCH] merge updateHistoryGraph behavior into update flag --- assets/js/main.js | 33 ++++++++++++++------------------- lib/ping.js | 13 +++++++------ lib/servers.js | 8 +++++++- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/assets/js/main.js b/assets/js/main.js index b6ee89f..589f6f9 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -62,25 +62,6 @@ document.addEventListener('DOMContentLoaded', function () { app.graphDisplayManager.initEventListeners() }) - socket.on('updateHistoryGraph', function (data) { - // Skip any incoming updates if the graph is disabled - // The backend shouldn't send these anyways - if (!app.graphDisplayManager.isVisible) { - return - } - - const serverRegistration = app.serverRegistry.getServerRegistration(data.serverId) - - if (serverRegistration) { - app.graphDisplayManager.addGraphPoint(serverRegistration.serverId, data.timestamp, data.playerCount) - - // Only redraw the graph if not mutating hidden data - if (serverRegistration.isVisible) { - app.graphDisplayManager.requestRedraw() - } - } - }) - socket.on('add', function (data) { data.forEach(app.addServer) }) @@ -94,6 +75,20 @@ document.addEventListener('DOMContentLoaded', function () { if (serverRegistration) { serverRegistration.updateServerStatus(data, false, app.publicConfig.minecraftVersions) } + + // Use update payloads to conditionally append data to graph + // Skip any incoming updates if the graph is disabled + if (data.updateHistoryGraph && app.graphDisplayManager.isVisible) { + // Update may not be successful, safely append 0 points + const playerCount = data.result ? data.result.players.online : 0 + + app.graphDisplayManager.addGraphPoint(serverRegistration.serverId, data.timestamp, playerCount) + + // Only redraw the graph if not mutating hidden data + if (serverRegistration.isVisible) { + app.graphDisplayManager.requestRedraw() + } + } }) socket.on('updateMojangServices', function (data) { diff --git a/lib/ping.js b/lib/ping.js index 5731c56..ac6a67c 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -109,6 +109,8 @@ class PingController { serverRegistration.addPing(timestamp, resp) + let updateHistoryGraph = false + if (config.logToDatabase) { const playerCount = resp ? resp.players.online : 0 @@ -116,15 +118,14 @@ class PingController { this._app.database.insertPing(serverRegistration.data.ip, timestamp, playerCount) if (serverRegistration.addGraphPoint(resp !== undefined, playerCount, timestamp)) { - this._app.server.broadcast('updateHistoryGraph', { - serverId: serverRegistration.serverId, - playerCount: playerCount, - timestamp: timestamp - }) + updateHistoryGraph = true } } - this._app.server.broadcast('update', serverRegistration.getUpdate(timestamp, resp, err, version)) + // Generate a combined update payload + // This includes any modified fields and flags used by the frontend + // This will not be cached and can contain live metadata + this._app.server.broadcast('update', serverRegistration.getUpdate(timestamp, resp, err, version, updateHistoryGraph)) } } diff --git a/lib/servers.js b/lib/servers.js index f405207..ec78b7c 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -14,7 +14,7 @@ class ServerRegistration { this._pingHistory = [] } - getUpdate (timestamp, resp, err, version) { + getUpdate (timestamp, resp, err, version, updateHistoryGraph) { const update = { serverId: this.serverId, timestamp: timestamp @@ -55,6 +55,12 @@ class ServerRegistration { if (this.findNewGraphPeak()) { update.graphPeakData = this.getGraphPeak() } + + // Handled inside logToDatabase to validate logic from #getUpdate call + // Only append when true since an undefined value == false + if (updateHistoryGraph) { + update.updateHistoryGraph = true + } } } else if (err) { // Append a filtered copy of err