comparison mod_measure_active_users/mod_measure_active_users.lua @ 5778:32d662015a84

mod_measure_active_users: Use the new mod_lastlog2 API
author Matthew Wild <mwild1@gmail.com>
date Wed, 06 Dec 2023 15:50:46 +0000
parents 34b46d157797
children 9d3d719db285
comparison
equal deleted inserted replaced
5777:34b46d157797 5778:32d662015a84
6 6
7 local is_enabled = require "core.usermanager".user_is_enabled; 7 local is_enabled = require "core.usermanager".user_is_enabled;
8 8
9 -- Exclude disabled user accounts from the counts if usermanager supports that API 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); 10 local count_disabled = not module:get_option_boolean("measure_active_users_count_disabled", is_enabled == nil);
11
12 local get_last_active = module:depends("lastlog2").get_last_active;
11 13
12 function update_calculations() 14 function update_calculations()
13 module:log("debug", "Calculating active users"); 15 module:log("debug", "Calculating active users");
14 local host = module.host; 16 local host = module.host;
15 local host_user_sessions = prosody.hosts[host].sessions; 17 local host_user_sessions = prosody.hosts[host].sessions;
19 if host_user_sessions[username] then 21 if host_user_sessions[username] then
20 -- Active now 22 -- Active now
21 active_d1, active_d7, active_d30 = 23 active_d1, active_d7, active_d30 =
22 active_d1 + 1, active_d7 + 1, active_d30 + 1; 24 active_d1 + 1, active_d7 + 1, active_d30 + 1;
23 elseif count_disabled or is_enabled(username, host) then 25 elseif count_disabled or is_enabled(username, host) then
24 local lastlog_data = store:get(username); 26 local last_active = get_last_active(username);
25 if lastlog_data then 27 if last_active then
26 -- Due to server restarts/crashes/etc. some events
27 -- may not always get recorded, so we'll just take the
28 -- latest as a sign of last activity
29 local last_active = math.max(
30 lastlog_data.login and lastlog_data.login.timestamp or 0,
31 lastlog_data.logout and lastlog_data.logout.timestamp or 0
32 );
33 if now - last_active < 86400 then 28 if now - last_active < 86400 then
34 active_d1 = active_d1 + 1; 29 active_d1 = active_d1 + 1;
35 end 30 end
36 if now - last_active < 86400*7 then 31 if now - last_active < 86400*7 then
37 active_d7 = active_d7 + 1; 32 active_d7 = active_d7 + 1;