Mercurial > prosody-modules
annotate mod_measure_active_users/mod_measure_active_users.lua @ 5571:ca3c2d11823c
mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
This should ensure that an entry that has a publish timestmap after the
previously oldest post, but before the time of the last poll check, is
published to the node.
Previously if an entry would be skipped if it was published at 13:00
with a timestamp of 12:30, where the last poll was at 12:45.
For feeds that lack a timestamp, it now looks for the first post that is
not published, assuming that the feed is in reverse chronological order,
then iterates back up from there.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 25 Jun 2023 16:27:55 +0200 |
parents | 1132f2888cd2 |
children | 34b46d157797 |
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); |