Changeset

3708:0a800463bc08

mod_nodeinfo2: Fetch active users from mod_lastlog.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 10 Oct 2019 20:09:23 +0200
parents 3707:3248295e1b16
children 3709:d3d27f2558fb
files mod_nodeinfo2/mod_nodeinfo2.lua
diffstat 1 files changed, 32 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mod_nodeinfo2/mod_nodeinfo2.lua	Thu Oct 10 17:59:32 2019 +0200
+++ b/mod_nodeinfo2/mod_nodeinfo2.lua	Thu Oct 10 20:09:23 2019 +0200
@@ -1,13 +1,40 @@
 local json = require "util.json";
 local array = require "util.array";
 local get_stats = require "core.statsmanager".get_stats;
+local os_time = os.time;
 
 module:depends("http");
+module:depends("lastlog");
 module:depends("measure_message_e2ee");
 
+local store = module:open_store("lastlog");
+
 local total_users = 0;
-for _ in require "core.usermanager".users(module.host) do -- TODO refresh at some interval?
+local half_year_users = 0;
+local month_users = 0;
+local week_users = 0;
+for user in require "core.usermanager".users(module.host) do -- TODO refresh at some interval?
 	total_users = total_users + 1;
+	local lastlog = store:get(user);
+	if lastlog and lastlog.timestamp then
+		local delta = os_time() - lastlog.timestamp;
+		if delta < 6 * 30 * 24 * 60 * 60 then
+			half_year_users = half_year_users + 1;
+		end
+		if delta < 30 * 24 * 60 * 60 then
+			month_users = month_users + 1;
+		end
+		if delta < 7 * 24 * 60 * 60 then
+			week_users = week_users + 1;
+		end
+	end
+end
+
+-- Remove the properties if we couldn’t find a single active user.  It most likely means mod_lastlog isn’t in use.
+if half_year_users == 0 and month_users == 0 and week_users == 0 then
+	half_year_users = nil;
+	month_users = nil;
+	week_users = nil;
 end
 
 module:provides("http", {
@@ -16,7 +43,7 @@
 		GET = function (event)
 			local stats, changed_only, extras = get_stats();
 			local message_count = nil;
-			for stat, value in pairs(stats) do
+			for stat, _ in pairs(stats) do
 				if stat == "/*/mod_measure_message_e2ee/message:rate" then
 					message_count = extras[stat].total;
 				end
@@ -54,10 +81,9 @@
 				usage = {
 					users = {
 						total = total_users;
-						-- TODO how would one calculate these?
-						-- activeHalfyear = 1;
-						-- activeMonth = 1;
-						-- activeWeek = 1;
+						activeHalfyear = half_year_users;
+						activeMonth = month_users;
+						activeWeek = week_users;
 					};
 					localPosts = message_count;
 					localComments = message_count;