centralize timestamp/point filtering, move magic numbers into named constants

This commit is contained in:
Nick Krecklow
2020-05-11 18:44:29 -05:00
parent 3998b688ab
commit 0c98930d4c
5 changed files with 30 additions and 30 deletions

View File

@ -1,5 +1,7 @@
const config = require('../config.json')
const GRAPH_UPDATE_TIME_GAP = 60 * 1000 // 60 seconds
class TimeTracker {
constructor (app) {
this._app = app
@ -14,7 +16,7 @@ class TimeTracker {
// Flag each group as history graph additions each minute
// This is sent to the frontend for graph updates
const updateHistoryGraph = config.logToDatabase && (!this._lastHistoryGraphUpdate || timestamp - this._lastHistoryGraphUpdate >= 60 * 1000)
const updateHistoryGraph = config.logToDatabase && (!this._lastHistoryGraphUpdate || timestamp - this._lastHistoryGraphUpdate >= GRAPH_UPDATE_TIME_GAP)
if (updateHistoryGraph) {
this._lastHistoryGraphUpdate = timestamp
@ -31,18 +33,8 @@ class TimeTracker {
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)
}
}
// timestamps contains original timestamp data and needs to be filtered into minutes
this._graphPoints = TimeTracker.everyN(timestamps, startTime, GRAPH_UPDATE_TIME_GAP, (i) => timestamps[i])
}
getGraphPointAt (i) {
@ -69,6 +61,22 @@ class TimeTracker {
return Math.ceil(config.graphDuration / config.rates.pingAll)
}
static everyN (array, start, diff, adapter) {
const selected = []
let lastPoint = start
for (let i = 0; i < array.length; i++) {
const point = array[i]
if (point - lastPoint >= diff) {
lastPoint = point
selected.push(adapter(i))
}
}
return selected
}
static pushAndShift (array, value, maxLength) {
array.push(value)
@ -78,4 +86,7 @@ class TimeTracker {
}
}
module.exports = TimeTracker
module.exports = {
GRAPH_UPDATE_TIME_GAP,
TimeTracker
}