From 0c98930d4cb2aa7e5d4a14e6d2e411288a6b08b8 Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Mon, 11 May 2020 18:44:29 -0500 Subject: [PATCH] centralize timestamp/point filtering, move magic numbers into named constants --- lib/app.js | 2 +- lib/database.js | 2 +- lib/ping.js | 2 +- lib/servers.js | 15 ++------------- lib/time.js | 39 +++++++++++++++++++++++++-------------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/app.js b/lib/app.js index 8f8b924..c4419b3 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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') diff --git a/lib/database.js b/lib/database.js index c54bfb7..3e22245 100644 --- a/lib/database.js +++ b/lib/database.js @@ -1,6 +1,6 @@ const sqlite = require('sqlite3') -const TimeTracker = require('./time') +const { TimeTracker } = require('./time') class Database { constructor (app) { diff --git a/lib/ping.js b/lib/ping.js index a91e92a..a830207 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -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') diff --git a/lib/servers.js b/lib/servers.js index f400368..fc28cdd 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -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 () { diff --git a/lib/time.js b/lib/time.js index f810ac4..0165215 100644 --- a/lib/time.js +++ b/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 +}