use serverId in protocol instead of legacy info object

This commit is contained in:
Nick Krecklow 2020-04-29 04:01:10 -05:00
parent 36c3e056c2
commit be92449d52
No known key found for this signature in database
GPG Key ID: 5F149FDE156FFA94
8 changed files with 59 additions and 93 deletions

@ -99,7 +99,7 @@ export class App {
// error = defined with "Waiting" description
// info = safely defined with configured data
const latestPing = pings[pings.length - 1]
const serverRegistration = this.serverRegistry.createServerRegistration(latestPing.info.name)
const serverRegistration = this.serverRegistry.createServerRegistration(latestPing.serverId)
serverRegistration.initServerStatus(latestPing)

@ -148,11 +148,7 @@ export class GraphDisplayManager {
this.loadLocalStorage()
}
// Remap the incoming data from being string (serverName) keyed into serverId keys
for (const serverName of Object.keys(graphData)) {
const serverRegistration = this._app.serverRegistry.getServerRegistration(serverName)
this._graphData[serverRegistration.serverId] = graphData[serverName]
}
this._graphData = graphData
// Explicitly define a height so flot.js can rescale the Y axis
document.getElementById('big-graph').style.height = '400px'

@ -5,7 +5,7 @@ import io from 'socket.io-client'
const app = new App()
document.addEventListener('DOMContentLoaded', function () {
const socket = io.connect({
const socket = io.connect('http://localhost:8080', {
reconnect: true,
reconnectDelay: 1000,
reconnectionAttempts: 10
@ -34,19 +34,22 @@ document.addEventListener('DOMContentLoaded', function () {
let lastRowCounter = 0
let controlsHTML = ''
Object.keys(data).sort().forEach(function (serverName) {
const serverRegistration = app.serverRegistry.getServerRegistration(serverName)
app.serverRegistry.getServerRegistrations()
.map(serverRegistration => serverRegistration.data.name)
.sort()
.forEach(serverName => {
const serverRegistration = app.serverRegistry.getServerRegistration(serverName)
controlsHTML += '<td>' +
'<input type="checkbox" class="graph-control" minetrack-server-id="' + serverRegistration.serverId + '" ' + (serverRegistration.isVisible ? 'checked' : '') + '>' +
' ' + serverName +
'</input></td>'
controlsHTML += '<td>' +
'<input type="checkbox" class="graph-control" minetrack-server-id="' + serverRegistration.serverId + '" ' + (serverRegistration.isVisible ? 'checked' : '') + '>' +
' ' + serverName +
'</input></td>'
// Occasionally break table rows using a magic number
if (++lastRowCounter % 6 === 0) {
controlsHTML += '</tr><tr>'
}
})
// Occasionally break table rows using a magic number
if (++lastRowCounter % 6 === 0) {
controlsHTML += '</tr><tr>'
}
})
// Apply generated HTML and show controls
document.getElementById('big-graph-checkboxes').innerHTML = '<table><tr>' +
@ -66,7 +69,7 @@ document.addEventListener('DOMContentLoaded', function () {
return
}
const serverRegistration = app.serverRegistry.getServerRegistration(data.name)
const serverRegistration = app.serverRegistry.getServerRegistration(data.serverId)
if (serverRegistration) {
app.graphDisplayManager.addGraphPoint(serverRegistration.serverId, data.timestamp, data.playerCount)
@ -86,7 +89,7 @@ document.addEventListener('DOMContentLoaded', function () {
// The backend may send "update" events prior to receiving all "add" events
// A server has only been added once it's ServerRegistration is defined
// Checking undefined protects from this race condition
const serverRegistration = app.serverRegistry.getServerRegistration(data.info.name)
const serverRegistration = app.serverRegistry.getServerRegistration(data.serverId)
if (serverRegistration) {
serverRegistration.updateServerStatus(data, false, app.publicConfig.minecraftVersions)
@ -121,26 +124,6 @@ document.addEventListener('DOMContentLoaded', function () {
app.handleSyncComplete()
})
socket.on('updatePeak', function (data) {
const serverRegistration = app.serverRegistry.getServerRegistration(data.name)
if (serverRegistration) {
serverRegistration.updateServerPeak(data.timestamp, data.playerCount)
}
})
socket.on('peaks', function (data) {
Object.keys(data).forEach(function (serverName) {
const serverRegistration = app.serverRegistry.getServerRegistration(serverName)
if (serverRegistration) {
const graphPeak = data[serverName]
serverRegistration.updateServerPeak(graphPeak.timestamp, graphPeak.playerCount)
}
})
})
window.addEventListener('resize', function () {
app.percentageBar.redraw()

@ -48,8 +48,7 @@ export class ServerRegistry {
}
}
createServerRegistration (serverName) {
const serverId = this._serverIdsByName[serverName]
createServerRegistration (serverId) {
const serverData = this._serverDataById[serverId]
const serverRegistration = new ServerRegistration(this._app, serverId, serverData)
this._registeredServers[serverId] = serverRegistration
@ -139,7 +138,7 @@ export class ServerRegistration {
if (pushToGraph) {
// Only update graph for successful pings
// This intentionally pauses the server graph when pings begin to fail
this._graphData.push([payload.info.timestamp, this.playerCount])
this._graphData.push([payload.timestamp, this.playerCount])
// Trim graphData to within the max length by shifting out the leading elements
if (this._graphData.length > SERVER_GRAPH_DATA_MAX_LENGTH) {
@ -174,7 +173,7 @@ export class ServerRegistration {
document.getElementById('ranking_' + this.serverId).innerText = '#' + (rankIndex + 1)
}
updateServerPeak (time, playerCount) {
updateServerPeak (data) {
const peakLabelElement = document.getElementById('peak_' + this.serverId)
// Always set label once any peak data has been received
@ -182,13 +181,10 @@ export class ServerRegistration {
const peakValueElement = document.getElementById('peak-value_' + this.serverId)
peakValueElement.innerText = formatNumber(playerCount)
peakLabelElement.title = 'At ' + formatTimestamp(time)
peakValueElement.innerText = formatNumber(data.playerCount)
peakLabelElement.title = 'At ' + formatTimestamp(data.timestamp)
this.lastPeakData = {
timestamp: time,
playerCount: playerCount
}
this.lastPeakData = data
}
updateServerStatus (ping, isInitialUpdate, minecraftVersions) {
@ -229,6 +225,10 @@ export class ServerRegistration {
}
}
if (ping.graphPeakData) {
this.updateServerPeak(ping.graphPeakData)
}
const playerCountLabelElement = document.getElementById('player-count_' + this.serverId)
const errorElement = document.getElementById('error_' + this.serverId)

@ -39,24 +39,11 @@ class App {
client.on('requestHistoryGraph', () => {
// Send historical graphData built from all serverRegistrations
const graphData = {}
const graphPeaks = {}
this.serverRegistrations.forEach((serverRegistration) => {
graphData[serverRegistration.data.name] = serverRegistration.graphData
// Send current peak, if any
const graphPeak = serverRegistration.getGraphPeak()
if (graphPeak) {
graphPeaks[serverRegistration.data.name] = graphPeak
}
graphData[serverRegistration.serverId] = serverRegistration.graphData
})
// Send current peaks, if any
// Emit peaks first since graphData may take a while to receive
if (Object.keys(graphPeaks).length > 0) {
client.emit('peaks', graphPeaks)
}
client.emit('historyGraph', graphData)
})
}

@ -107,8 +107,6 @@ class PingController {
handlePing (serverRegistration, resp, err, version) {
const timestamp = new Date().getTime()
this._app.server.broadcast('update', serverRegistration.getUpdate(timestamp, resp, err, version))
serverRegistration.addPing(timestamp, resp)
if (config.logToDatabase) {
@ -119,24 +117,14 @@ class PingController {
if (serverRegistration.addGraphPoint(resp !== undefined, playerCount, timestamp)) {
this._app.server.broadcast('updateHistoryGraph', {
name: serverRegistration.data.name,
serverId: serverRegistration.serverId,
playerCount: playerCount,
timestamp: timestamp
})
}
// Update calculated graph peak regardless if the graph is being updated
// This can cause a (harmless) desync between live and stored data, but it allows it to be more accurate for long surviving processes
if (serverRegistration.findNewGraphPeak()) {
const graphPeak = serverRegistration.getGraphPeak()
this._app.server.broadcast('updatePeak', {
name: serverRegistration.data.name,
playerCount: graphPeak.playerCount,
timestamp: graphPeak.timestamp
})
}
}
this._app.server.broadcast('update', serverRegistration.getUpdate(timestamp, resp, err, version))
}
}

@ -2,22 +2,22 @@ const config = require('../config')
const minecraftVersions = require('../minecraft_versions')
class ServerRegistration {
serverId
lastFavicon
versions = []
recordData
graphData = []
constructor (data) {
constructor (serverId, data) {
this.serverId = serverId
this.data = data
this._pingHistory = []
}
getUpdate (timestamp, resp, err, version) {
const update = {
info: {
name: this.data.name,
timestamp: timestamp
}
serverId: this.serverId,
timestamp: timestamp
}
if (resp) {
@ -48,6 +48,14 @@ class ServerRegistration {
update.result = {
players: resp.players
}
if (config.logToDatabase) {
// Update calculated graph peak regardless if the graph is being updated
// This can cause a (harmless) desync between live and stored data, but it allows it to be more accurate for long surviving processes
if (this.findNewGraphPeak()) {
update.graphPeakData = this.getGraphPeak()
}
}
} else if (err) {
// Append a filtered copy of err
// This ensures any unintended data is not leaked
@ -94,15 +102,21 @@ class ServerRegistration {
const lastPing = this._pingHistory[this._pingHistory.length - 1]
const payload = {
info: {
name: this.data.name
},
serverId: this.serverId,
timestamp: lastPing.timestamp,
versions: this.versions,
recordData: this.recordData,
favicon: this.lastFavicon
}
// Only append graphPeakData if defined
// The value is lazy computed and conditional that config->logToDatabase == true
const graphPeakData = this.getGraphPeak()
if (graphPeakData) {
payload.graphPeakData = graphPeakData
}
// Conditionally append to avoid defining fields with undefined values
if (lastPing.result) {
payload.result = lastPing.result
@ -118,14 +132,12 @@ class ServerRegistration {
}
return [{
serverId: this.serverId,
timestamp: new Date().getTime(),
error: {
message: 'Waiting...',
placeholder: true
},
timestamp: new Date().getTime(),
info: {
name: this.data.name
},
recordData: this.recordData
}]
}

@ -8,7 +8,7 @@ const servers = require('./servers')
const app = new App()
servers.forEach(server => {
servers.forEach((server, serverId) => {
// Assign a generated color for each servers.json entry if not manually defined
// These will be passed to the frontend for use in rendering
if (!server.color) {
@ -22,7 +22,7 @@ servers.forEach(server => {
}
// Init a ServerRegistration instance of each entry in servers.json
app.serverRegistrations.push(new ServerRegistration(server))
app.serverRegistrations.push(new ServerRegistration(serverId, server))
})
if (!config.logToDatabase) {