From e45f8b66397849c229daf36428812f85dc5cf8bf Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Fri, 8 May 2020 02:39:30 -0500 Subject: [PATCH] simplify addServer/pingHistory & placeholder generation prior to optimizations --- assets/js/app.js | 17 ++++++++++------- assets/js/servers.js | 10 ++-------- assets/js/socket.js | 6 +++--- config.json | 4 ++-- lib/servers.js | 31 ++++++++++++------------------- 5 files changed, 29 insertions(+), 39 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index 5804a20..a5b3283 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -102,26 +102,29 @@ export class App { .reduce((sum, current) => sum + current, 0) } - addServer = (serverId, pings, timestampPoints) => { + addServer = (serverId, payload, timestampPoints) => { // Even if the backend has never pinged the server, the frontend is promised a placeholder object. // result = undefined // error = defined with "Waiting" description // info = safely defined with configured data - const latestPing = pings[pings.length - 1] const serverRegistration = this.serverRegistry.createServerRegistration(serverId) - serverRegistration.initServerStatus(latestPing) + serverRegistration.initServerStatus(payload) - // Push the historical data into the graph - // This will trim and format the data so it is ready for the graph to render once init - serverRegistration.addGraphPoints(pings, timestampPoints) + // pingHistory is only defined when the backend has previous ping data + // undefined pingHistory means this is a placeholder ping generated by the backend + if (typeof payload.pingHistory !== 'undefined') { + // Push the historical data into the graph + // This will trim and format the data so it is ready for the graph to render once init + serverRegistration.addGraphPoints(payload.pingHistory, timestampPoints) + } // Create the plot instance internally with the restructured and cleaned data serverRegistration.buildPlotInstance() // Handle the last known state (if any) as an incoming update // This triggers the main update pipeline and enables centralized update handling - serverRegistration.updateServerStatus(latestPing, true, this.publicConfig.minecraftVersions) + serverRegistration.updateServerStatus(payload, this.publicConfig.minecraftVersions) // Allow the ServerRegistration to bind any DOM events with app instance context serverRegistration.initEventListeners() diff --git a/assets/js/servers.js b/assets/js/servers.js index 1aee659..cc518b8 100644 --- a/assets/js/servers.js +++ b/assets/js/servers.js @@ -93,11 +93,6 @@ export class ServerRegistration { } addGraphPoints (points, timestampPoints) { - // Test if the first point contains error.placeholder === true - // This is sent by the backend when the server hasn't been pinged yet - // These points will be disregarded to prevent the graph starting at 0 player count - points = points.filter(point => !point.error || !point.error.placeholder) - for (let i = 0; i < points.length; i++) { const point = points[i] const timestamp = timestampPoints[i] @@ -164,7 +159,7 @@ export class ServerRegistration { this.lastPeakData = data } - updateServerStatus (ping, isInitialUpdate, minecraftVersions) { + updateServerStatus (ping, minecraftVersions) { if (ping.versions) { const versionsElement = document.getElementById('version_' + this.serverId) @@ -214,8 +209,7 @@ export class ServerRegistration { document.getElementById('player-count-value_' + this.serverId).innerText = formatNumber(ping.result.players.online) // An updated favicon has been sent, update the src - // Ignore calls from 'add' events since they will have explicitly manually handled the favicon update - if (!isInitialUpdate && ping.favicon) { + if (ping.favicon) { document.getElementById('favicon_' + this.serverId).setAttribute('src', ping.favicon) } } diff --git a/assets/js/socket.js b/assets/js/socket.js index be79817..2263d64 100644 --- a/assets/js/socket.js +++ b/assets/js/socket.js @@ -67,8 +67,8 @@ export class SocketManager { } } - payload.servers.forEach((pings, serverId) => { - this._app.addServer(serverId, pings, payload.timestampPoints) + payload.servers.forEach((serverPayload, serverId) => { + this._app.addServer(serverId, serverPayload, payload.timestampPoints) }) if (payload.mojangServices) { @@ -92,7 +92,7 @@ export class SocketManager { if (serverRegistration) { serverRegistration.handlePing(serverUpdate, payload.timestamp) - serverRegistration.updateServerStatus(serverUpdate, payload.timestamp, false, this._app.publicConfig.minecraftVersions) + serverRegistration.updateServerStatus(serverUpdate, this._app.publicConfig.minecraftVersions) } // Use update payloads to conditionally append data to graph diff --git a/config.json b/config.json index ccd1794..ce470b7 100644 --- a/config.json +++ b/config.json @@ -10,9 +10,9 @@ "connectTimeout": 2500 }, "performance": { - "skipUnfurlSrv": false + "skipUnfurlSrv": true }, - "logToDatabase": false, + "logToDatabase": true, "graphDuration": 86400000, "serverGraphDuration": 180000 } diff --git a/lib/servers.js b/lib/servers.js index b5d51e7..164d532 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -113,17 +113,6 @@ class ServerRegistration { getPingHistory () { if (this._pingHistory.length > 0) { - const pingHistory = [] - - for (let i = 0; i < this._pingHistory.length - 1; i++) { - pingHistory[i] = this._pingHistory[i] - } - - // Insert the latest update manually into the array - // This is a mutated copy of the last update to contain live metadata - // The metadata is used by the frontend for rendering - const lastPing = this._pingHistory[this._pingHistory.length - 1] - const payload = { versions: this.versions, recordData: this.recordData, @@ -138,6 +127,11 @@ class ServerRegistration { payload.graphPeakData = graphPeakData } + // Insert the latest update manually into the array + // This is a mutated copy of the last update to contain live metadata + // The metadata is used by the frontend for rendering + const lastPing = this._pingHistory[this._pingHistory.length - 1] + // Conditionally append to avoid defining fields with undefined values if (lastPing.result) { payload.result = lastPing.result @@ -145,21 +139,20 @@ class ServerRegistration { payload.error = lastPing.error } - // Insert the reconstructed update as the last entry - // pingHistory is already sorted during its copy from _pingHistory - pingHistory.push(payload) + // Send a copy of pingHistory + // Omit the last value since it is contained within payload + payload.pingHistory = this._pingHistory.slice(0, this._pingHistory.length - 1) - return pingHistory + return payload } - return [{ + return { error: { - message: 'Waiting...', - placeholder: true + message: 'Pinging...' }, recordData: this.recordData, graphPeakData: this.getGraphPeak() - }] + } } loadGraphPoints (points) {