comparison mod_measure_active_users/mod_measure_active_users.lua @ 5777:34b46d157797

mod_measure_active_users: Exclude disabled user accounts from counts ...if usermanager exposes that API (it's in trunk, not 0.12).
author Matthew Wild <mwild1@gmail.com>
date Wed, 06 Dec 2023 15:45:44 +0000
parents 1132f2888cd2
children 32d662015a84
comparison
equal deleted inserted replaced
5776:5239ed05bd71 5777:34b46d157797
2 2
3 local measure_d1 = module:measure("active_users_1d", "amount"); 3 local measure_d1 = module:measure("active_users_1d", "amount");
4 local measure_d7 = module:measure("active_users_7d", "amount"); 4 local measure_d7 = module:measure("active_users_7d", "amount");
5 local measure_d30 = module:measure("active_users_30d", "amount"); 5 local measure_d30 = module:measure("active_users_30d", "amount");
6 6
7 local is_enabled = require "core.usermanager".user_is_enabled;
8
9 -- Exclude disabled user accounts from the counts if usermanager supports that API
10 local count_disabled = not module:get_option_boolean("measure_active_users_count_disabled", is_enabled == nil);
11
7 function update_calculations() 12 function update_calculations()
8 module:log("debug", "Calculating active users"); 13 module:log("debug", "Calculating active users");
9 local host_user_sessions = prosody.hosts[module.host].sessions; 14 local host = module.host;
15 local host_user_sessions = prosody.hosts[host].sessions;
10 local active_d1, active_d7, active_d30 = 0, 0, 0; 16 local active_d1, active_d7, active_d30 = 0, 0, 0;
11 local now = os.time(); 17 local now = os.time();
12 for username in store:users() do 18 for username in store:users() do
13 if host_user_sessions[username] then 19 if host_user_sessions[username] then
14 -- Active now 20 -- Active now
15 active_d1, active_d7, active_d30 = 21 active_d1, active_d7, active_d30 =
16 active_d1 + 1, active_d7 + 1, active_d30 + 1; 22 active_d1 + 1, active_d7 + 1, active_d30 + 1;
17 else 23 elseif count_disabled or is_enabled(username, host) then
18 local lastlog_data = store:get(username); 24 local lastlog_data = store:get(username);
19 if lastlog_data then 25 if lastlog_data then
20 -- Due to server restarts/crashes/etc. some events 26 -- Due to server restarts/crashes/etc. some events
21 -- may not always get recorded, so we'll just take the 27 -- may not always get recorded, so we'll just take the
22 -- latest as a sign of last activity 28 -- latest as a sign of last activity