simplify addServer/pingHistory & placeholder generation prior to optimizations

This commit is contained in:
Nick Krecklow 2020-05-08 02:39:30 -05:00
parent a3624d38ff
commit e45f8b6639
No known key found for this signature in database
GPG Key ID: 5F149FDE156FFA94
5 changed files with 29 additions and 39 deletions

@ -102,26 +102,29 @@ export class App {
.reduce((sum, current) => sum + current, 0) .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. // Even if the backend has never pinged the server, the frontend is promised a placeholder object.
// result = undefined // result = undefined
// error = defined with "Waiting" description // error = defined with "Waiting" description
// info = safely defined with configured data // info = safely defined with configured data
const latestPing = pings[pings.length - 1]
const serverRegistration = this.serverRegistry.createServerRegistration(serverId) const serverRegistration = this.serverRegistry.createServerRegistration(serverId)
serverRegistration.initServerStatus(latestPing) serverRegistration.initServerStatus(payload)
// Push the historical data into the graph // pingHistory is only defined when the backend has previous ping data
// This will trim and format the data so it is ready for the graph to render once init // undefined pingHistory means this is a placeholder ping generated by the backend
serverRegistration.addGraphPoints(pings, timestampPoints) 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 // Create the plot instance internally with the restructured and cleaned data
serverRegistration.buildPlotInstance() serverRegistration.buildPlotInstance()
// Handle the last known state (if any) as an incoming update // Handle the last known state (if any) as an incoming update
// This triggers the main update pipeline and enables centralized update handling // 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 // Allow the ServerRegistration to bind any DOM events with app instance context
serverRegistration.initEventListeners() serverRegistration.initEventListeners()

@ -93,11 +93,6 @@ export class ServerRegistration {
} }
addGraphPoints (points, timestampPoints) { 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++) { for (let i = 0; i < points.length; i++) {
const point = points[i] const point = points[i]
const timestamp = timestampPoints[i] const timestamp = timestampPoints[i]
@ -164,7 +159,7 @@ export class ServerRegistration {
this.lastPeakData = data this.lastPeakData = data
} }
updateServerStatus (ping, isInitialUpdate, minecraftVersions) { updateServerStatus (ping, minecraftVersions) {
if (ping.versions) { if (ping.versions) {
const versionsElement = document.getElementById('version_' + this.serverId) 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) document.getElementById('player-count-value_' + this.serverId).innerText = formatNumber(ping.result.players.online)
// An updated favicon has been sent, update the src // 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 (ping.favicon) {
if (!isInitialUpdate && ping.favicon) {
document.getElementById('favicon_' + this.serverId).setAttribute('src', ping.favicon) document.getElementById('favicon_' + this.serverId).setAttribute('src', ping.favicon)
} }
} }

@ -67,8 +67,8 @@ export class SocketManager {
} }
} }
payload.servers.forEach((pings, serverId) => { payload.servers.forEach((serverPayload, serverId) => {
this._app.addServer(serverId, pings, payload.timestampPoints) this._app.addServer(serverId, serverPayload, payload.timestampPoints)
}) })
if (payload.mojangServices) { if (payload.mojangServices) {
@ -92,7 +92,7 @@ export class SocketManager {
if (serverRegistration) { if (serverRegistration) {
serverRegistration.handlePing(serverUpdate, payload.timestamp) 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 // Use update payloads to conditionally append data to graph

@ -10,9 +10,9 @@
"connectTimeout": 2500 "connectTimeout": 2500
}, },
"performance": { "performance": {
"skipUnfurlSrv": false "skipUnfurlSrv": true
}, },
"logToDatabase": false, "logToDatabase": true,
"graphDuration": 86400000, "graphDuration": 86400000,
"serverGraphDuration": 180000 "serverGraphDuration": 180000
} }

@ -113,17 +113,6 @@ class ServerRegistration {
getPingHistory () { getPingHistory () {
if (this._pingHistory.length > 0) { 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 = { const payload = {
versions: this.versions, versions: this.versions,
recordData: this.recordData, recordData: this.recordData,
@ -138,6 +127,11 @@ class ServerRegistration {
payload.graphPeakData = graphPeakData 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 // Conditionally append to avoid defining fields with undefined values
if (lastPing.result) { if (lastPing.result) {
payload.result = lastPing.result payload.result = lastPing.result
@ -145,21 +139,20 @@ class ServerRegistration {
payload.error = lastPing.error payload.error = lastPing.error
} }
// Insert the reconstructed update as the last entry // Send a copy of pingHistory
// pingHistory is already sorted during its copy from _pingHistory // Omit the last value since it is contained within payload
pingHistory.push(payload) payload.pingHistory = this._pingHistory.slice(0, this._pingHistory.length - 1)
return pingHistory return payload
} }
return [{ return {
error: { error: {
message: 'Waiting...', message: 'Pinging...'
placeholder: true
}, },
recordData: this.recordData, recordData: this.recordData,
graphPeakData: this.getGraphPeak() graphPeakData: this.getGraphPeak()
}] }
} }
loadGraphPoints (points) { loadGraphPoints (points) {