Minetrack/lib/time.js

113 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2023-12-30 23:03:54 +00:00
const config = require("../config.json");
2023-12-30 23:03:54 +00:00
const GRAPH_UPDATE_TIME_GAP = 60 * 1000; // 60 seconds
class TimeTracker {
2023-12-30 23:03:54 +00:00
constructor(app) {
this._app = app;
this._serverGraphPoints = [];
this._graphPoints = [];
}
2023-12-30 23:03:54 +00:00
newPointTimestamp() {
const timestamp = TimeTracker.getEpochMillis();
2023-12-30 23:03:54 +00:00
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
2023-12-30 23:03:54 +00:00
const updateHistoryGraph =
config.logToDatabase &&
(!this._lastHistoryGraphUpdate ||
timestamp - this._lastHistoryGraphUpdate >= GRAPH_UPDATE_TIME_GAP);
if (updateHistoryGraph) {
2023-12-30 23:03:54 +00:00
this._lastHistoryGraphUpdate = timestamp;
// Push into timestamps array to update backend state
2023-12-30 23:03:54 +00:00
TimeTracker.pushAndShift(
this._graphPoints,
timestamp,
TimeTracker.getMaxGraphDataLength()
);
}
return {
timestamp,
2023-12-30 23:03:54 +00:00
updateHistoryGraph,
};
}
2023-12-30 23:03:54 +00:00
loadGraphPoints(startTime, timestamps) {
// This is a copy of ServerRegistration#loadGraphPoints
// timestamps contains original timestamp data and needs to be filtered into minutes
2023-12-30 23:03:54 +00:00
this._graphPoints = TimeTracker.everyN(
timestamps,
startTime,
GRAPH_UPDATE_TIME_GAP,
(i) => timestamps[i]
);
}
2023-12-30 23:03:54 +00:00
getGraphPointAt(i) {
return TimeTracker.toSeconds(this._graphPoints[i]);
}
2023-12-30 23:03:54 +00:00
getServerGraphPoints() {
return this._serverGraphPoints.map(TimeTracker.toSeconds);
}
2023-12-30 23:03:54 +00:00
getGraphPoints() {
return this._graphPoints.map(TimeTracker.toSeconds);
}
static toSeconds = (timestamp) => {
2023-12-30 23:03:54 +00:00
return Math.floor(timestamp / 1000);
};
2023-12-30 23:03:54 +00:00
static getEpochMillis() {
return new Date().getTime();
}
2023-12-30 23:03:54 +00:00
static getMaxServerGraphDataLength() {
return Math.ceil(config.serverGraphDuration / config.rates.pingAll);
}
2023-12-30 23:03:54 +00:00
static getMaxGraphDataLength() {
return Math.ceil(config.graphDuration / GRAPH_UPDATE_TIME_GAP);
}
2023-12-30 23:03:54 +00:00
static everyN(array, start, diff, adapter) {
const selected = [];
let lastPoint = start;
for (let i = 0; i < array.length; i++) {
2023-12-30 23:03:54 +00:00
const point = array[i];
if (point - lastPoint >= diff) {
2023-12-30 23:03:54 +00:00
lastPoint = point;
selected.push(adapter(i));
}
}
2023-12-30 23:03:54 +00:00
return selected;
}
2023-12-30 23:03:54 +00:00
static pushAndShift(array, value, maxLength) {
array.push(value);
if (array.length > maxLength) {
2023-12-30 23:03:54 +00:00
array.splice(0, array.length - maxLength);
}
}
}
module.exports = {
GRAPH_UPDATE_TIME_GAP,
2023-12-30 23:03:54 +00:00
TimeTracker,
};