Software /
code /
prosody-modules
File
mod_measure_active_users/mod_measure_active_users.lua @ 5931:d194d1012fd3
Updating dox for mod_rest. Ideas expressed / clarified:
1) Making clear that mod_rest isn't to be installed under VirtualHosts AND as a component.
2) Understanding some of the implications of this choice:
A) Changes to user authentication
B) How it affects subdomains
3) More consistent use of domain names for clarity.
4) Using different heading sizes to show scope of section.
Essentially, I added all the tidbits I had to clarify in getting this to work in my
own example.
author | Ben Smith <bens@effortlessis.com> |
---|---|
date | Mon, 13 May 2024 13:25:13 -0700 |
parent | 5887:ca62f9984f4b |
line wrap: on
line source
local store = module:open_store("lastlog2"); local measure_d1 = module:measure("active_users_1d", "amount"); local measure_d7 = module:measure("active_users_7d", "amount"); local measure_d30 = module:measure("active_users_30d", "amount"); local is_enabled = require "core.usermanager".user_is_enabled; -- Exclude disabled user accounts from the counts if usermanager supports that API local count_disabled = module:get_option_boolean("measure_active_users_count_disabled", is_enabled == nil); local get_last_active = module:depends("lastlog2").get_last_active; function update_calculations() module:log("debug", "Calculating active users"); local host = module.host; local host_user_sessions = prosody.hosts[host].sessions; local active_d1, active_d7, active_d30 = 0, 0, 0; local now = os.time(); for username in store:users() do if host_user_sessions[username] then -- Active now active_d1, active_d7, active_d30 = active_d1 + 1, active_d7 + 1, active_d30 + 1; elseif count_disabled or is_enabled(username, host) then local last_active = get_last_active(username); if last_active then if now - last_active < 86400 then active_d1 = active_d1 + 1; end if now - last_active < 86400*7 then active_d7 = active_d7 + 1; end if now - last_active < 86400*30 then active_d30 = active_d30 + 1; end end end end module:log("debug", "Active users (took %ds): %d (24h), %d (7d), %d (30d)", os.time()-now, active_d1, active_d7, active_d30); measure_d1(active_d1); measure_d7(active_d7); measure_d30(active_d30); end -- Schedule at startup module:add_timer(15, update_calculations); -- Recalculate hourly module:hourly(update_calculations);