centralize shift behavior into TimeTracker func
This commit is contained in:
20
lib/time.js
20
lib/time.js
@ -10,11 +10,7 @@ class TimeTracker {
|
||||
newPingTimestamp () {
|
||||
const timestamp = new Date().getTime()
|
||||
|
||||
this._points.push(timestamp)
|
||||
|
||||
if (this._points.length > TimeTracker.getMaxServerGraphDataLength()) {
|
||||
this._points.shift()
|
||||
}
|
||||
TimeTracker.pushAndShift(this._points, timestamp, TimeTracker.getMaxServerGraphDataLength())
|
||||
|
||||
// Flag each group as history graph additions each minute
|
||||
// This is sent to the frontend for graph updates
|
||||
@ -24,11 +20,7 @@ class TimeTracker {
|
||||
this._lastHistoryGraphUpdate = timestamp
|
||||
|
||||
// Push into timestamps array to update backend state
|
||||
this._historicalTimestamps.push(timestamp)
|
||||
|
||||
if (this._historicalTimestamps.length > TimeTracker.getMaxGraphDataLength()) {
|
||||
this._historicalTimestamps.shift()
|
||||
}
|
||||
TimeTracker.pushAndShift(this._historicalTimestamps, timestamp, TimeTracker.getMaxGraphDataLength())
|
||||
}
|
||||
|
||||
return {
|
||||
@ -60,6 +52,14 @@ class TimeTracker {
|
||||
static getMaxGraphDataLength () {
|
||||
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
|
||||
|
Reference in New Issue
Block a user