annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4774
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local store = module:open_store("lastlog2");
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 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
4 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
5 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
6
5777
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
7 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
8
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
9 -- Exclude disabled user accounts from the counts if usermanager supports that API
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
10 local count_disabled = not module:get_option_boolean("measure_active_users_count_disabled", is_enabled == nil);
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
11
4774
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function update_calculations()
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 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
14 local host = module.host;
34b46d157797 mod_measure_active_users: Exclude disabled user accounts from counts
Matthew Wild <mwild1@gmail.com>
parents: 4774
diff changeset
15 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
16 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
17 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
18 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
19 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
20 -- Active now
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 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
22 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
23 elseif count_disabled or is_enabled(username, host) then
4774
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local lastlog_data = store:get(username);
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 if lastlog_data then
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 -- Due to server restarts/crashes/etc. some events
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 -- may not always get recorded, so we'll just take the
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 -- latest as a sign of last activity
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local last_active = math.max(
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 lastlog_data.login and lastlog_data.login.timestamp or 0,
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 lastlog_data.logout and lastlog_data.logout.timestamp or 0
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 );
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 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
34 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
35 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 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
37 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
38 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 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
40 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
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 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 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
46 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
47 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
48 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
49
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return 3600 + (300*math.random());
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 module:add_timer(15, update_calculations);