From 3491c73b893cf062294bf4686a38b8d482883cd8 Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Fri, 8 May 2020 02:52:48 -0500 Subject: [PATCH] store pingHistory as player count directly --- assets/js/app.js | 8 +++---- assets/js/servers.js | 3 +-- assets/js/socket.js | 2 ++ lib/servers.js | 54 +++++++++++++++----------------------------- 4 files changed, 25 insertions(+), 42 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index a5b3283..32d4992 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -111,12 +111,12 @@ export class App { serverRegistration.initServerStatus(payload) - // 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') { + // playerCountHistory is only defined when the backend has previous ping data + // undefined playerCountHistory means this is a placeholder ping generated by the backend + if (typeof payload.playerCountHistory !== '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) + serverRegistration.addGraphPoints(payload.playerCountHistory, timestampPoints) } // Create the plot instance internally with the restructured and cleaned data diff --git a/assets/js/servers.js b/assets/js/servers.js index 71cb92b..1c8cc08 100644 --- a/assets/js/servers.js +++ b/assets/js/servers.js @@ -96,8 +96,7 @@ export class ServerRegistration { for (let i = 0; i < points.length; i++) { const point = points[i] const timestamp = timestampPoints[i] - - this._graphData.push([timestamp, point.result ? point.result.players.online : 0]) + this._graphData.push([timestamp, point]) } } diff --git a/assets/js/socket.js b/assets/js/socket.js index 2263d64..72770ed 100644 --- a/assets/js/socket.js +++ b/assets/js/socket.js @@ -67,6 +67,8 @@ export class SocketManager { } } + console.log(payload.servers) + payload.servers.forEach((serverPayload, serverId) => { this._app.addServer(serverId, serverPayload, payload.timestampPoints) }) diff --git a/lib/servers.js b/lib/servers.js index 164d532..7da838b 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -16,8 +16,15 @@ class ServerRegistration { } handlePing (timestamp, resp, err, version) { + const playerCount = resp ? resp.players.online : 0 + // Store into in-memory ping data - this.addPing(resp) + this._pingHistory.push(playerCount) + + // Trim pingHistory to avoid memory leaks + if (this._pingHistory.length > this._app.pingController.getMaxServerGraphDataLength()) { + this._pingHistory.shift() + } // Only notify the frontend to append to the historical graph // if both the graphing behavior is enabled and the backend agrees @@ -25,8 +32,6 @@ class ServerRegistration { let updateHistoryGraph = false if (config.logToDatabase) { - const playerCount = resp ? resp.players.online : 0 - if (this.addGraphPoint(resp !== undefined, playerCount, timestamp)) { updateHistoryGraph = true } @@ -90,27 +95,6 @@ class ServerRegistration { return update } - addPing (resp) { - const ping = {} - - if (resp) { - // Append a result object - // This filters out unwanted data from resp - ping.result = { - players: { - online: resp.players.online - } - } - } - - this._pingHistory.push(ping) - - // Trim pingHistory to avoid memory leaks - if (this._pingHistory.length > this._app.pingController.getMaxServerGraphDataLength()) { - this._pingHistory.shift() - } - } - getPingHistory () { if (this._pingHistory.length > 0) { const payload = { @@ -127,21 +111,19 @@ 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 - } else if (lastPing.error) { - payload.error = lastPing.error + // Assume the ping was a success and define result + // pingHistory does not keep error references, so its impossible to detect if this is an error + // It is also pointless to store that data since it will be short lived + payload.result = { + players: { + online: this._pingHistory[this._pingHistory.length - 1] + } } // 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) + // Include the last value even though it is contained within payload + // The frontend will only push to its graphData from playerCountHistory + payload.playerCountHistory = this._pingHistory return payload }