From 024e605a413cb571eb77cdeafd2c0fe6a130940e Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Fri, 8 May 2020 02:56:39 -0500 Subject: [PATCH] make getMaxGraphDataLength/getMaxServerGraphDataLength methods static --- lib/app.js | 4 ++-- lib/ping.js | 8 -------- lib/servers.js | 9 +++++---- lib/time.js | 12 +++++++++++- main.js | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/app.js b/lib/app.js index a5dc9d5..7a330fc 100644 --- a/lib/app.js +++ b/lib/app.js @@ -69,8 +69,8 @@ class App { // Send configuration data for rendering the page return { graphDurationLabel: config.graphDurationLabel || (Math.floor(config.graphDuration / (60 * 60 * 1000)) + 'h'), - graphMaxLength: this.pingController.getMaxGraphDataLength(), - serverGraphMaxLength: this.pingController.getMaxServerGraphDataLength(), + graphMaxLength: TimeTracker.getMaxGraphDataLength(), + serverGraphMaxLength: TimeTracker.getMaxServerGraphDataLength(), servers: this.serverRegistrations.map(serverRegistration => serverRegistration.data), minecraftVersions: minecraftVersionNames, isGraphVisible: config.logToDatabase diff --git a/lib/ping.js b/lib/ping.js index b19a37d..a9dda14 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -151,14 +151,6 @@ class PingController { }, version.protocolId) } } - - getMaxServerGraphDataLength () { - return Math.ceil(config.serverGraphDuration / config.rates.pingAll) - } - - getMaxGraphDataLength () { - return Math.ceil(config.graphDuration / config.rates.pingAll) - } } module.exports = PingController diff --git a/lib/servers.js b/lib/servers.js index 7da838b..5db95dd 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -1,3 +1,5 @@ +const TimeTracker = require('./time') + const config = require('../config') const minecraftVersions = require('../minecraft_versions') @@ -8,8 +10,7 @@ class ServerRegistration { recordData graphData = [] - constructor (app, serverId, data) { - this._app = app + constructor (serverId, data) { this.serverId = serverId this.data = data this._pingHistory = [] @@ -22,7 +23,7 @@ class ServerRegistration { this._pingHistory.push(playerCount) // Trim pingHistory to avoid memory leaks - if (this._pingHistory.length > this._app.pingController.getMaxServerGraphDataLength()) { + if (this._pingHistory.length > TimeTracker.getMaxServerGraphDataLength()) { this._pingHistory.shift() } @@ -178,7 +179,7 @@ class ServerRegistration { this._lastGraphDataPush = timestamp // Trim old graphPoints according to #getMaxGraphDataLength - if (this.graphData.length > this._app.pingController.getMaxGraphDataLength()) { + if (this.graphData.length > TimeTracker.getMaxGraphDataLength()) { this.graphData.shift() } diff --git a/lib/time.js b/lib/time.js index 1269b46..966272a 100644 --- a/lib/time.js +++ b/lib/time.js @@ -1,3 +1,5 @@ +const config = require('../config.json') + class TimeTracker { constructor (app) { this._app = app @@ -9,7 +11,7 @@ class TimeTracker { this._points.push(timestamp) - if (this._points.length > this._app.pingController.getMaxServerGraphDataLength()) { + if (this._points.length > TimeTracker.getMaxServerGraphDataLength()) { this._points.shift() } @@ -19,6 +21,14 @@ class TimeTracker { getPoints () { return this._points } + + static getMaxServerGraphDataLength () { + return Math.ceil(config.serverGraphDuration / config.rates.pingAll) + } + + static getMaxGraphDataLength () { + return Math.ceil(config.graphDuration / config.rates.pingAll) + } } module.exports = TimeTracker diff --git a/main.js b/main.js index 094e496..5f4c56e 100644 --- a/main.js +++ b/main.js @@ -22,7 +22,7 @@ servers.forEach((server, serverId) => { } // Init a ServerRegistration instance of each entry in servers.json - app.serverRegistrations.push(new ServerRegistration(app, serverId, server)) + app.serverRegistrations.push(new ServerRegistration(serverId, server)) }) if (!config.serverGraphDuration) {