centralize timestamp/point filtering, move magic numbers into named constants
This commit is contained in:
parent
3998b688ab
commit
0c98930d4c
@ -2,7 +2,7 @@ const Database = require('./database')
|
||||
const MojangUpdater = require('./mojang')
|
||||
const PingController = require('./ping')
|
||||
const Server = require('./server')
|
||||
const TimeTracker = require('./time')
|
||||
const { TimeTracker } = require('./time')
|
||||
const MessageOf = require('./message')
|
||||
|
||||
const config = require('../config')
|
||||
|
@ -1,6 +1,6 @@
|
||||
const sqlite = require('sqlite3')
|
||||
|
||||
const TimeTracker = require('./time')
|
||||
const { TimeTracker } = require('./time')
|
||||
|
||||
class Database {
|
||||
constructor (app) {
|
||||
|
@ -3,7 +3,7 @@ const minecraftBedrockPing = require('mcpe-ping-fixed')
|
||||
|
||||
const logger = require('./logger')
|
||||
const MessageOf = require('./message')
|
||||
const TimeTracker = require('./time')
|
||||
const { TimeTracker } = require('./time')
|
||||
|
||||
const { getPlayerCountOrNull } = require('./util')
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
const crypto = require('crypto')
|
||||
const dns = require('dns')
|
||||
|
||||
const TimeTracker = require('./time')
|
||||
const { GRAPH_UPDATE_TIME_GAP, TimeTracker } = require('./time')
|
||||
const Server = require('./server')
|
||||
|
||||
const { getPlayerCountOrNull } = require('./util')
|
||||
@ -124,18 +124,7 @@ class ServerRegistration {
|
||||
}
|
||||
|
||||
loadGraphPoints (startTime, timestamps, points) {
|
||||
// Filter pings so each result is a minute apart
|
||||
let lastTimestamp = startTime
|
||||
|
||||
for (let i = 0; i < timestamps.length; i++) {
|
||||
const timestamp = timestamps[i]
|
||||
|
||||
if (timestamp - lastTimestamp >= 60 * 1000) {
|
||||
lastTimestamp = timestamp
|
||||
|
||||
this.graphData.push(points[i])
|
||||
}
|
||||
}
|
||||
this.graphData = TimeTracker.everyN(timestamps, startTime, GRAPH_UPDATE_TIME_GAP, (i) => points[i])
|
||||
}
|
||||
|
||||
findNewGraphPeak () {
|
||||
|
39
lib/time.js
39
lib/time.js
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user