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

View File

@ -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()

View File

@ -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)
}
}

View File

@ -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

View File

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

View File

@ -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) {