Mercurial > prosody-modules
comparison mod_nodeinfo2/mod_nodeinfo2.lua @ 3790:352f3efe1b67
mod_nodeinfo2: Refresh the number of recent users daily
| author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
|---|---|
| date | Mon, 30 Dec 2019 22:39:17 +0100 |
| parents | e3b673df3906 |
| children | 34a8f9f996ec |
comparison
equal
deleted
inserted
replaced
| 3789:e3b673df3906 | 3790:352f3efe1b67 |
|---|---|
| 1 local json = require "util.json"; | 1 local json = require "util.json"; |
| 2 local array = require "util.array"; | 2 local array = require "util.array"; |
| 3 local add_task = require "util.timer".add_task; | |
| 3 local get_stats = require "core.statsmanager".get_stats; | 4 local get_stats = require "core.statsmanager".get_stats; |
| 5 local list_users = require "core.usermanager".users; | |
| 4 local os_time = os.time; | 6 local os_time = os.time; |
| 5 | 7 |
| 6 module:depends("http"); | 8 module:depends("http"); |
| 7 module:depends("lastlog"); | 9 module:depends("lastlog"); |
| 8 | 10 |
| 12 end | 14 end |
| 13 | 15 |
| 14 local main_store = module:open_store(); | 16 local main_store = module:open_store(); |
| 15 local lastlog_store = module:open_store("lastlog"); | 17 local lastlog_store = module:open_store("lastlog"); |
| 16 | 18 |
| 17 local total_users = 0; | |
| 18 local half_year_users = 0; | |
| 19 local month_users = 0; | |
| 20 local week_users = 0; | |
| 21 for user in require "core.usermanager".users(module.host) do -- TODO refresh at some interval? | |
| 22 total_users = total_users + 1; | |
| 23 local lastlog = lastlog_store:get(user); | |
| 24 if lastlog and lastlog.timestamp then | |
| 25 local delta = os_time() - lastlog.timestamp; | |
| 26 if delta < 6 * 30 * 24 * 60 * 60 then | |
| 27 half_year_users = half_year_users + 1; | |
| 28 end | |
| 29 if delta < 30 * 24 * 60 * 60 then | |
| 30 month_users = month_users + 1; | |
| 31 end | |
| 32 if delta < 7 * 24 * 60 * 60 then | |
| 33 week_users = week_users + 1; | |
| 34 end | |
| 35 end | |
| 36 end | |
| 37 | |
| 38 -- Remove the properties if we couldn’t find a single active user. It most likely means mod_lastlog isn’t in use. | |
| 39 if half_year_users == 0 and month_users == 0 and week_users == 0 then | |
| 40 half_year_users = nil; | |
| 41 month_users = nil; | |
| 42 week_users = nil; | |
| 43 end | |
| 44 | |
| 45 local data; | 19 local data; |
| 46 if expose_posts then | 20 if expose_posts then |
| 47 data = main_store:get("nodeinfo2") or { message_count = 0 }; | 21 data = main_store:get("nodeinfo2") or { message_count = 0 }; |
| 48 end | 22 end |
| 23 | |
| 24 local total_users = 0; | |
| 25 local week_users = 0; | |
| 26 local month_users = 0; | |
| 27 local half_year_users = 0; | |
| 28 | |
| 29 local function update_user_list() | |
| 30 for user in list_users(module.host) do | |
| 31 total_users = total_users + 1; | |
| 32 local lastlog = lastlog_store:get(user); | |
| 33 if lastlog and lastlog.timestamp then | |
| 34 local delta = os_time() - lastlog.timestamp; | |
| 35 if delta < 7 * 86400 then | |
| 36 week_users = week_users + 1; | |
| 37 end | |
| 38 if delta < 30 * 86400 then | |
| 39 month_users = month_users + 1; | |
| 40 end | |
| 41 if delta < 6 * 30 * 86400 then | |
| 42 half_year_users = half_year_users + 1; | |
| 43 end | |
| 44 end | |
| 45 end | |
| 46 | |
| 47 -- Remove the properties if we couldn’t find a single active user. It most likely means mod_lastlog isn’t in use. | |
| 48 if half_year_users == 0 and month_users == 0 and week_users == 0 then | |
| 49 week_users = nil; | |
| 50 month_users = nil; | |
| 51 half_year_users = nil; | |
| 52 end | |
| 53 end | |
| 54 | |
| 55 add_task(86400, update_user_list); | |
| 56 update_user_list(); | |
| 49 | 57 |
| 50 module:provides("http", { | 58 module:provides("http", { |
| 51 default_path = "/.well-known/x-nodeinfo2"; | 59 default_path = "/.well-known/x-nodeinfo2"; |
| 52 route = { | 60 route = { |
| 53 GET = function (event) | 61 GET = function (event) |
| 54 local usage = { | 62 local usage = { |
| 55 users = { | 63 users = { |
| 56 total = total_users; | 64 total = total_users; |
| 65 activeWeek = week_users; | |
| 66 activeMonth = month_users; | |
| 57 activeHalfyear = half_year_users; | 67 activeHalfyear = half_year_users; |
| 58 activeMonth = month_users; | |
| 59 activeWeek = week_users; | |
| 60 }; | 68 }; |
| 61 }; | 69 }; |
| 62 | 70 |
| 63 if expose_posts then | 71 if expose_posts then |
| 64 local stats, changed_only, extras = get_stats(); | 72 local stats, changed_only, extras = get_stats(); |
