annotate mod_measure_active_users/mod_measure_active_users.lua @ 5604:aab0023b51c9

editorconfig: Document established conventions
author Kim Alvefur <zash@zash.se>
date Sat, 15 Jul 2023 10:45:26 +0200
parents 1132f2888cd2
children 34b46d157797
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
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 function update_calculations()
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 module:log("debug", "Calculating active users");
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local host_user_sessions = prosody.hosts[module.host].sessions;
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 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
11 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
12 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
13 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
14 -- Active now
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 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
16 active_d1 + 1, active_d7 + 1, active_d30 + 1;
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 else
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 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
19 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
20 -- 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
21 -- 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
22 -- 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
23 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
24 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
25 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
26 );
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 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
28 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
29 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 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
31 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
32 end
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*30 then
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 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
35 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
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 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 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
40 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
41 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
42 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
43
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 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
45 end
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
1132f2888cd2 mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 module:add_timer(15, update_calculations);