unify TimeTracker code, explicitly mark nullable playerCounts as unsafe

This commit is contained in:
Nick Krecklow
2020-05-11 18:29:26 -05:00
parent 7136851123
commit 903343fbdf
6 changed files with 62 additions and 44 deletions

View File

@ -3,14 +3,14 @@ const config = require('../config.json')
class TimeTracker {
constructor (app) {
this._app = app
this._points = []
this._historicalTimestamps = []
this._serverGraphPoints = []
this._graphPoints = []
}
newPingTimestamp () {
newPointTimestamp () {
const timestamp = new Date().getTime()
TimeTracker.pushAndShift(this._points, timestamp, TimeTracker.getMaxServerGraphDataLength())
TimeTracker.pushAndShift(this._serverGraphPoints, timestamp, TimeTracker.getMaxServerGraphDataLength())
// Flag each group as history graph additions each minute
// This is sent to the frontend for graph updates
@ -20,7 +20,7 @@ class TimeTracker {
this._lastHistoryGraphUpdate = timestamp
// Push into timestamps array to update backend state
TimeTracker.pushAndShift(this._historicalTimestamps, timestamp, TimeTracker.getMaxGraphDataLength())
TimeTracker.pushAndShift(this._graphPoints, timestamp, TimeTracker.getMaxGraphDataLength())
}
return {
@ -29,20 +29,36 @@ class TimeTracker {
}
}
loadHistoricalTimestamps (timestamps) {
this._historicalTimestamps = timestamps
loadGraphPoints (startTime, timestamps) {
// This is a copy of ServerRegistration#loadGraphPoints
// relativeGraphData contains original timestamp data and needs to be filtered into minutes
let lastTimestamp = startTime
for (let i = 0; i < timestamps.length; i++) {
const timestamp = timestamps[i]
if (timestamp - lastTimestamp >= 60 * 1000) {
lastTimestamp = timestamp
this._graphPoints.push(timestamp)
}
}
}
getHistoricalPointsSeconds () {
return this._historicalTimestamps.map(value => Math.floor(value / 1000))
getGraphPointAt (i) {
return TimeTracker.toSeconds(this._graphPoints[i])
}
getHistoricalPointSeconds (index) {
return Math.floor(this._historicalTimestamps[index] / 1000)
getServerGraphPoints () {
return this._serverGraphPoints.map(TimeTracker.toSeconds)
}
getServerPointsSeconds () {
return this._points.map(value => Math.floor(value / 1000))
getGraphPoints () {
return this._graphPoints.map(TimeTracker.toSeconds)
}
static toSeconds = (timestamp) => {
return Math.floor(timestamp / 1000)
}
static getMaxServerGraphDataLength () {