annotate mod_measure_active_users/mod_measure_active_users.lua @ 6513:5fb466693e85

mod_storage_xmlarchive: Ensure list index files are removed using os.remove() on the list files leaves the new index files behind since util.datamanager does not know they are removed. Thanks Link Mauve
author Kim Alvefur <zash@zash.se>
date Tue, 07 Apr 2026 21:10:54 +0200
parents 908d44cfb693
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6350
908d44cfb693 mod_measure_active_users: Add support for mod_account_activity in Prosody 13.0
Matthew Wild <mwild1@gmail.com>
parents: 5863
diff changeset
1 local is_prosody_13 = prosody.features:contains("loader"); -- proxy for mod_account_activity
908d44cfb693 mod_measure_active_users: Add support for mod_account_activity in Prosody 13.0
Matthew Wild <mwild1@gmail.com>
parents: 5863
diff changeset
2
908d44cfb693 mod_measure_active_users: Add support for mod_account_activity in Prosody 13.0
Matthew Wild <mwild1@gmail.com>
parents: 5863
diff changeset
3 local activity_mod = is_prosody_13 and "account_activity" or "lastlog2";
908d44cfb693 mod_measure_active_users: Add support for mod_account_activity in Prosody 13.0
Matthew Wild <mwild1@gmail.com>
parents: 5863
diff changeset
4
908d44cfb693 mod_measure_active_users: Add support for mod_account_activity in Prosody 13.0
Matthew Wild <mwild1@gmail.com>
parents: 5863
diff changeset
5 local store = module:open_store(activity_mod);
4774
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local measure_d1 = module:measure("active_users_1d", "amount");
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local measure_d7 = module:measure("active_users_7d", "amount");
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local measure_d30 = module:measure("active_users_30d", "amount");
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
5777
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
11 local is_enabled = require "core.usermanager".user_is_enabled;
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
12
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
13 -- Exclude disabled user accounts from the counts if usermanager supports that API
5791
9d3d719db285 mod_measure_active_users: Fix inverted logic (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 5778
diff changeset
14 local count_disabled = module:get_option_boolean("measure_active_users_count_disabled", is_enabled == nil);
5777
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
15
6350
908d44cfb693 mod_measure_active_users: Add support for mod_account_activity in Prosody 13.0
Matthew Wild <mwild1@gmail.com>
parents: 5863
diff changeset
16 local get_last_active = module:depends(activity_mod).get_last_active;
5778
32d662015a84 mod_measure_active_users: Use the new mod_lastlog2 API
Matthew Wild <mwild1@gmail.com>
parents: 5777
diff changeset
17
4774
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 function update_calculations()
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 module:log("debug", "Calculating active users");
5777
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
20 local host = module.host;
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
21 local host_user_sessions = prosody.hosts[host].sessions;
4774
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local active_d1, active_d7, active_d30 = 0, 0, 0;
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local now = os.time();
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 for username in store:users() do
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 if host_user_sessions[username] then
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 -- Active now
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 active_d1, active_d7, active_d30 =
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 active_d1 + 1, active_d7 + 1, active_d30 + 1;
5777
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
29 elseif count_disabled or is_enabled(username, host) then
5778
32d662015a84 mod_measure_active_users: Use the new mod_lastlog2 API
Matthew Wild <mwild1@gmail.com>
parents: 5777
diff changeset
30 local last_active = get_last_active(username);
32d662015a84 mod_measure_active_users: Use the new mod_lastlog2 API
Matthew Wild <mwild1@gmail.com>
parents: 5777
diff changeset
31 if last_active then
4774
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if now - last_active < 86400 then
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 active_d1 = active_d1 + 1;
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 if now - last_active < 86400*7 then
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 active_d7 = active_d7 + 1;
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if now - last_active < 86400*30 then
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 active_d30 = active_d30 + 1;
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 module:log("debug", "Active users (took %ds): %d (24h), %d (7d), %d (30d)", os.time()-now, active_d1, active_d7, active_d30);
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 measure_d1(active_d1);
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 measure_d7(active_d7);
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 measure_d30(active_d30);
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
5863
ca62f9984f4b mod_measure_active_users: Switch to mod_cron for scheduling
Matthew Wild <mwild1@gmail.com>
parents: 5791
diff changeset
50 -- Schedule at startup
4774
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 module:add_timer(15, update_calculations);
5863
ca62f9984f4b mod_measure_active_users: Switch to mod_cron for scheduling
Matthew Wild <mwild1@gmail.com>
parents: 5791
diff changeset
52
ca62f9984f4b mod_measure_active_users: Switch to mod_cron for scheduling
Matthew Wild <mwild1@gmail.com>
parents: 5791
diff changeset
53 -- Recalculate hourly
ca62f9984f4b mod_measure_active_users: Switch to mod_cron for scheduling
Matthew Wild <mwild1@gmail.com>
parents: 5791
diff changeset
54 module:hourly(update_calculations);