fix potential null errors in recordData/graphPeakData

This commit is contained in:
Nick Krecklow 2020-05-20 19:56:10 -05:00
parent f4dadd9805
commit 05df7081e4
No known key found for this signature in database
GPG Key ID: 5F149FDE156FFA94
2 changed files with 22 additions and 6 deletions

View File

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

View File

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