forked from Cryptkeeper/Minetrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.js
96 lines (73 loc) · 2.42 KB
/
time.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const config = require('../config.json')
const GRAPH_UPDATE_TIME_GAP = 60 * 1000 // 60 seconds
class TimeTracker {
constructor (app) {
this._app = app
this._serverGraphPoints = []
this._graphPoints = []
}
newPointTimestamp () {
const timestamp = TimeTracker.getEpochMillis()
TimeTracker.pushAndShift(this._serverGraphPoints, timestamp, TimeTracker.getMaxServerGraphDataLength())
// 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 >= GRAPH_UPDATE_TIME_GAP)
if (updateHistoryGraph) {
this._lastHistoryGraphUpdate = timestamp
// Push into timestamps array to update backend state
TimeTracker.pushAndShift(this._graphPoints, timestamp, TimeTracker.getMaxGraphDataLength())
}
return {
timestamp,
updateHistoryGraph
}
}
loadGraphPoints (startTime, timestamps) {
// This is a copy of ServerRegistration#loadGraphPoints
// 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) {
return TimeTracker.toSeconds(this._graphPoints[i])
}
getServerGraphPoints () {
return this._serverGraphPoints.map(TimeTracker.toSeconds)
}
getGraphPoints () {
return this._graphPoints.map(TimeTracker.toSeconds)
}
static toSeconds = (timestamp) => {
return Math.floor(timestamp / 1000)
}
static getEpochMillis () {
return new Date().getTime()
}
static getMaxServerGraphDataLength () {
return Math.ceil(config.serverGraphDuration / config.rates.pingAll)
}
static getMaxGraphDataLength () {
return Math.ceil(config.graphDuration / GRAPH_UPDATE_TIME_GAP)
}
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)
if (array.length > maxLength) {
array.splice(0, array.length - maxLength)
}
}
}
module.exports = {
GRAPH_UPDATE_TIME_GAP,
TimeTracker
}