From 05df7081e4e998870dee8150018340124f6205d6 Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Wed, 20 May 2020 19:56:10 -0500 Subject: [PATCH] fix potential null errors in recordData/graphPeakData --- lib/database.js | 26 +++++++++++++++++++++----- lib/servers.js | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/database.js b/lib/database.js index 21fb7b6..ee7481a 100644 --- a/lib/database.js +++ b/lib/database.js @@ -77,10 +77,12 @@ class Database { // Query recordData // When complete increment completeTasks to know when complete - this.getRecord(serverRegistration.data.ip, (playerCount, timestamp) => { - serverRegistration.recordData = { - playerCount, - timestamp: TimeTracker.toSeconds(timestamp) + this.getRecord(serverRegistration.data.ip, (hasRecord, playerCount, timestamp) => { + if (hasRecord) { + serverRegistration.recordData = { + playerCount, + timestamp: TimeTracker.toSeconds(timestamp) + } } // Check if completedTasks hit the finish value @@ -102,7 +104,21 @@ class Database { getRecord (ip, callback) { this._sql.all('SELECT MAX(playerCount), timestamp FROM pings WHERE ip = ?', [ ip - ], (_, data) => callback(data[0]['MAX(playerCount)'], data[0].timestamp)) + ], (_, data) => { + // For empty results, data will be length 1 with [null, null] + const playerCount = data[0]['MAX(playerCount)'] + const timestamp = data[0].timestamp + + // Allow null timestamps, the frontend will safely handle them + // This allows insertion of free standing records without a known timestamp + if (playerCount !== null) { + // eslint-disable-next-line standard/no-callback-literal + callback(true, playerCount, timestamp) + } else { + // eslint-disable-next-line standard/no-callback-literal + callback(false) + } + }) } insertPing (ip, timestamp, unsafePlayerCount) { diff --git a/lib/servers.js b/lib/servers.js index 060bd73..4568324 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -132,7 +132,7 @@ class ServerRegistration { let index = -1 for (let i = 0; i < this.graphData.length; i++) { const point = this.graphData[i] - if (index === -1 || point > this.graphData[index]) { + if (point !== null && (index === -1 || point > this.graphData[index])) { index = i } }