2020-05-08 07:56:39 +00:00
|
|
|
const config = require('../config.json')
|
|
|
|
|
2020-05-11 23:44:29 +00:00
|
|
|
const GRAPH_UPDATE_TIME_GAP = 60 * 1000 // 60 seconds
|
|
|
|
|
2020-05-08 06:22:07 +00:00
|
|
|
class TimeTracker {
|
2020-05-08 06:54:04 +00:00
|
|
|
constructor (app) {
|
|
|
|
this._app = app
|
2020-05-11 23:29:26 +00:00
|
|
|
this._serverGraphPoints = []
|
|
|
|
this._graphPoints = []
|
2020-05-08 06:22:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
newPointTimestamp () {
|
2020-05-08 06:22:07 +00:00
|
|
|
const timestamp = new Date().getTime()
|
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
TimeTracker.pushAndShift(this._serverGraphPoints, timestamp, TimeTracker.getMaxServerGraphDataLength())
|
2020-05-08 06:22:07 +00:00
|
|
|
|
2020-05-11 23:12:29 +00:00
|
|
|
// Flag each group as history graph additions each minute
|
|
|
|
// This is sent to the frontend for graph updates
|
2020-05-11 23:44:29 +00:00
|
|
|
const updateHistoryGraph = config.logToDatabase && (!this._lastHistoryGraphUpdate || timestamp - this._lastHistoryGraphUpdate >= GRAPH_UPDATE_TIME_GAP)
|
2020-05-11 23:12:29 +00:00
|
|
|
|
|
|
|
if (updateHistoryGraph) {
|
|
|
|
this._lastHistoryGraphUpdate = timestamp
|
|
|
|
|
|
|
|
// Push into timestamps array to update backend state
|
2020-05-11 23:29:26 +00:00
|
|
|
TimeTracker.pushAndShift(this._graphPoints, timestamp, TimeTracker.getMaxGraphDataLength())
|
2020-05-11 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
timestamp,
|
|
|
|
updateHistoryGraph
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
loadGraphPoints (startTime, timestamps) {
|
|
|
|
// This is a copy of ServerRegistration#loadGraphPoints
|
2020-05-11 23:44:29 +00:00
|
|
|
// 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])
|
2020-05-11 23:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getGraphPointAt (i) {
|
|
|
|
return TimeTracker.toSeconds(this._graphPoints[i])
|
2020-05-11 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
getServerGraphPoints () {
|
|
|
|
return this._serverGraphPoints.map(TimeTracker.toSeconds)
|
2020-05-11 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
getGraphPoints () {
|
|
|
|
return this._graphPoints.map(TimeTracker.toSeconds)
|
2020-05-08 06:22:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
static toSeconds = (timestamp) => {
|
|
|
|
return Math.floor(timestamp / 1000)
|
2020-05-08 06:22:07 +00:00
|
|
|
}
|
2020-05-08 07:56:39 +00:00
|
|
|
|
|
|
|
static getMaxServerGraphDataLength () {
|
|
|
|
return Math.ceil(config.serverGraphDuration / config.rates.pingAll)
|
|
|
|
}
|
|
|
|
|
|
|
|
static getMaxGraphDataLength () {
|
2020-05-13 03:10:38 +00:00
|
|
|
return Math.ceil(config.graphDuration / GRAPH_UPDATE_TIME_GAP)
|
2020-05-08 07:56:39 +00:00
|
|
|
}
|
2020-05-11 23:16:41 +00:00
|
|
|
|
2020-05-11 23:44:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-11 23:16:41 +00:00
|
|
|
static pushAndShift (array, value, maxLength) {
|
|
|
|
array.push(value)
|
|
|
|
|
|
|
|
if (array.length > maxLength) {
|
2020-05-11 23:36:22 +00:00
|
|
|
array.splice(0, array.length - maxLength)
|
2020-05-11 23:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-08 06:22:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 23:44:29 +00:00
|
|
|
module.exports = {
|
|
|
|
GRAPH_UPDATE_TIME_GAP,
|
|
|
|
TimeTracker
|
|
|
|
}
|