From 14ba1a6dbba08ccf763a8a22e8bd138a6950feb0 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Tue, 29 Sep 2020 17:31:08 -0400 Subject: [PATCH] feat: analytics:maxCache setting in ACP I removed a TODO from core. Watch your head as flying pigs are abound --- .../en-GB/admin/settings/advanced.json | 6 ++++- src/analytics.js | 27 +++++++++---------- src/views/admin/settings/advanced.tpl | 15 +++++++++++ src/webserver.js | 2 ++ 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/public/language/en-GB/admin/settings/advanced.json b/public/language/en-GB/admin/settings/advanced.json index 5731a605d3b9..a08f863af1c7 100644 --- a/public/language/en-GB/admin/settings/advanced.json +++ b/public/language/en-GB/admin/settings/advanced.json @@ -32,5 +32,9 @@ "sockets.settings": "WebSocket Settings", "sockets.max-attempts": "Max Reconnection Attempts", "sockets.default-placeholder": "Default: %1", - "sockets.delay": "Reconnection Delay" + "sockets.delay": "Reconnection Delay", + + "analytics.settings": "Analytics Settings", + "analytics.max-cache": "Analytics Cache Max Value", + "analytics.max-cache-help": "On high-traffic installs, the cache could be exhausted continuously if there are more concurrent active users than the Max Cache value. (Restart required)" } \ No newline at end of file diff --git a/src/analytics.js b/src/analytics.js index 8f30eaf46c5e..1922f5d230ed 100644 --- a/src/analytics.js +++ b/src/analytics.js @@ -10,6 +10,7 @@ const LRU = require('lru-cache'); const db = require('./database'); const utils = require('./utils'); const plugins = require('./plugins'); +const meta = require('./meta'); const Analytics = module.exports; @@ -21,21 +22,19 @@ let pageViewsGuest = 0; let pageViewsBot = 0; let uniqueIPCount = 0; let uniquevisitors = 0; +let ipCache; -/** - * TODO: allow the cache's max value to be configurable. On high-traffic installs, - * the cache could be exhausted continuously if there are more than 500 concurrently - * active users - */ -var ipCache = new LRU({ - max: 500, - length: function () { return 1; }, - maxAge: 0, -}); - -new cronJob('*/10 * * * * *', function () { - Analytics.writeData(); -}, null, true); +Analytics.init = async function () { + ipCache = new LRU({ + max: parseInt(meta.config['analytics:maxCache'], 10) || 500, + length: function () { return 1; }, + maxAge: 0, + }); + + new cronJob('*/10 * * * * *', function () { + Analytics.writeData(); + }, null, true); +}; Analytics.increment = function (keys, callback) { keys = Array.isArray(keys) ? keys : [keys]; diff --git a/src/views/admin/settings/advanced.tpl b/src/views/admin/settings/advanced.tpl index e1258ba058f1..cf1934dfc90b 100644 --- a/src/views/admin/settings/advanced.tpl +++ b/src/views/admin/settings/advanced.tpl @@ -150,4 +150,19 @@ +
+
[[admin/settings/advanced:analytics.settings]]
+
+
+
+ + +

+ [[admin/settings/advanced:analytics.max-cache-help]] +

+
+
+
+
+ diff --git a/src/webserver.js b/src/webserver.js index 5de56f9241a2..5578bfe65f55 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -23,6 +23,7 @@ var helmet = require('helmet'); var Benchpress = require('benchpressjs'); var db = require('./database'); +var analytics = require('./analytics'); var file = require('./file'); var emailer = require('./emailer'); var meta = require('./meta'); @@ -105,6 +106,7 @@ async function initializeNodeBB() { await routes(app, middleware); await meta.blacklist.load(); await flags.init(); + await analytics.init(); } function setupExpressApp(app) {