From 2f1c9c1dce12bf308ffa12c0e7631718c7e84071 Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Fri, 8 May 2020 02:06:39 -0500 Subject: [PATCH] use config.graphDuration when querying database, not LIMIT --- lib/app.js | 2 +- lib/database.js | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/app.js b/lib/app.js index a94e238..a5dc9d5 100644 --- a/lib/app.js +++ b/lib/app.js @@ -24,7 +24,7 @@ class App { // Setup database instance this.database.ensureIndexes() - this.database.loadGraphPoints(this.pingController.getMaxGraphDataLength(), () => { + this.database.loadGraphPoints(config.graphDuration, () => { this.database.loadRecords(callback) }) } diff --git a/lib/database.js b/lib/database.js index aef9af9..f94979c 100644 --- a/lib/database.js +++ b/lib/database.js @@ -14,8 +14,12 @@ class Database { }) } - loadGraphPoints (length, callback) { - this.getRecentPings(length, pingData => { + loadGraphPoints (graphDuration, callback) { + // Query recent pings + const endTime = new Date().getTime() + const startTime = endTime - graphDuration + + this.getRecentPings(startTime, endTime, length, pingData => { const graphPointsByIp = [] for (const row of pingData) { @@ -72,9 +76,10 @@ class Database { }) } - getRecentPings (length, callback) { - this._sql.all('SELECT * FROM pings WHERE 1 LIMIT ?', [ - length + getRecentPings (startTime, endTime, callback) { + this._sql.all('SELECT * FROM pings WHERE timestamp >= ? AND timestamp <= ?', [ + startTime, + endTime ], (_, data) => callback(data)) }