2020-05-08 07:56:39 +00:00
|
|
|
const config = require('../config.json')
|
|
|
|
|
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
|
|
|
|
const updateHistoryGraph = config.logToDatabase && (!this._lastHistoryGraphUpdate || timestamp - this._lastHistoryGraphUpdate >= 60 * 1000)
|
|
|
|
|
|
|
|
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
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 () {
|
|
|
|
return Math.ceil(config.graphDuration / config.rates.pingAll)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TimeTracker
|