centralize shift behavior into TimeTracker func
This commit is contained in:
parent
c2f6d04e72
commit
7136851123
@ -26,23 +26,13 @@ class ServerRegistration {
|
|||||||
const playerCount = resp ? resp.players.online : null
|
const playerCount = resp ? resp.players.online : null
|
||||||
|
|
||||||
// Store into in-memory ping data
|
// Store into in-memory ping data
|
||||||
this._pingHistory.push(playerCount)
|
TimeTracker.pushAndShift(this._pingHistory, playerCount, TimeTracker.getMaxServerGraphDataLength())
|
||||||
|
|
||||||
// Trim pingHistory to avoid memory leaks
|
|
||||||
if (this._pingHistory.length > TimeTracker.getMaxServerGraphDataLength()) {
|
|
||||||
this._pingHistory.shift()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only notify the frontend to append to the historical graph
|
// Only notify the frontend to append to the historical graph
|
||||||
// if both the graphing behavior is enabled and the backend agrees
|
// if both the graphing behavior is enabled and the backend agrees
|
||||||
// that the ping is eligible for addition
|
// that the ping is eligible for addition
|
||||||
if (updateHistoryGraph) {
|
if (updateHistoryGraph) {
|
||||||
this.graphData.push(playerCount)
|
TimeTracker.pushAndShift(this.graphData, playerCount, TimeTracker.getMaxGraphDataLength())
|
||||||
|
|
||||||
// Trim old graphPoints according to #getMaxGraphDataLength
|
|
||||||
if (this.graphData.length > TimeTracker.getMaxGraphDataLength()) {
|
|
||||||
this.graphData.shift()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delegate out update payload generation
|
// Delegate out update payload generation
|
||||||
|
20
lib/time.js
20
lib/time.js
@ -10,11 +10,7 @@ class TimeTracker {
|
|||||||
newPingTimestamp () {
|
newPingTimestamp () {
|
||||||
const timestamp = new Date().getTime()
|
const timestamp = new Date().getTime()
|
||||||
|
|
||||||
this._points.push(timestamp)
|
TimeTracker.pushAndShift(this._points, timestamp, TimeTracker.getMaxServerGraphDataLength())
|
||||||
|
|
||||||
if (this._points.length > TimeTracker.getMaxServerGraphDataLength()) {
|
|
||||||
this._points.shift()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Flag each group as history graph additions each minute
|
// Flag each group as history graph additions each minute
|
||||||
// This is sent to the frontend for graph updates
|
// This is sent to the frontend for graph updates
|
||||||
@ -24,11 +20,7 @@ class TimeTracker {
|
|||||||
this._lastHistoryGraphUpdate = timestamp
|
this._lastHistoryGraphUpdate = timestamp
|
||||||
|
|
||||||
// Push into timestamps array to update backend state
|
// Push into timestamps array to update backend state
|
||||||
this._historicalTimestamps.push(timestamp)
|
TimeTracker.pushAndShift(this._historicalTimestamps, timestamp, TimeTracker.getMaxGraphDataLength())
|
||||||
|
|
||||||
if (this._historicalTimestamps.length > TimeTracker.getMaxGraphDataLength()) {
|
|
||||||
this._historicalTimestamps.shift()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -60,6 +52,14 @@ class TimeTracker {
|
|||||||
static getMaxGraphDataLength () {
|
static getMaxGraphDataLength () {
|
||||||
return Math.ceil(config.graphDuration / config.rates.pingAll)
|
return Math.ceil(config.graphDuration / config.rates.pingAll)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static pushAndShift (array, value, maxLength) {
|
||||||
|
array.push(value)
|
||||||
|
|
||||||
|
if (array.length > maxLength) {
|
||||||
|
array.shift()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = TimeTracker
|
module.exports = TimeTracker
|
||||||
|
Loading…
Reference in New Issue
Block a user