Mercurial > prosody-modules
comparison mod_nodeinfo2/mod_nodeinfo2.lua @ 3784:741a1e400de4
mod_nodeinfo2: Count messages per host
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 29 Dec 2019 21:09:03 +0100 |
parents | 908b5fd47634 |
children | 58e484426d13 |
comparison
equal
deleted
inserted
replaced
3783:908b5fd47634 | 3784:741a1e400de4 |
---|---|
5 | 5 |
6 module:depends("http"); | 6 module:depends("http"); |
7 module:depends("lastlog"); | 7 module:depends("lastlog"); |
8 module:depends("measure_message_e2ee"); | 8 module:depends("measure_message_e2ee"); |
9 | 9 |
10 local store = module:open_store("lastlog"); | 10 local main_store = module:open_store(); |
11 local lastlog_store = module:open_store("lastlog"); | |
11 | 12 |
12 local total_users = 0; | 13 local total_users = 0; |
13 local half_year_users = 0; | 14 local half_year_users = 0; |
14 local month_users = 0; | 15 local month_users = 0; |
15 local week_users = 0; | 16 local week_users = 0; |
16 for user in require "core.usermanager".users(module.host) do -- TODO refresh at some interval? | 17 for user in require "core.usermanager".users(module.host) do -- TODO refresh at some interval? |
17 total_users = total_users + 1; | 18 total_users = total_users + 1; |
18 local lastlog = store:get(user); | 19 local lastlog = lastlog_store:get(user); |
19 if lastlog and lastlog.timestamp then | 20 if lastlog and lastlog.timestamp then |
20 local delta = os_time() - lastlog.timestamp; | 21 local delta = os_time() - lastlog.timestamp; |
21 if delta < 6 * 30 * 24 * 60 * 60 then | 22 if delta < 6 * 30 * 24 * 60 * 60 then |
22 half_year_users = half_year_users + 1; | 23 half_year_users = half_year_users + 1; |
23 end | 24 end |
35 half_year_users = nil; | 36 half_year_users = nil; |
36 month_users = nil; | 37 month_users = nil; |
37 week_users = nil; | 38 week_users = nil; |
38 end | 39 end |
39 | 40 |
40 local message_count_store = module:open_store("message_count"); | 41 local data = main_store:get("nodeinfo2"); |
41 local message_count = message_count_store:get("message_count"); | |
42 | 42 |
43 module:provides("http", { | 43 module:provides("http", { |
44 default_path = "/.well-known/x-nodeinfo2"; | 44 default_path = "/.well-known/x-nodeinfo2"; |
45 route = { | 45 route = { |
46 GET = function (event) | 46 GET = function (event) |
47 local stats, changed_only, extras = get_stats(); | 47 local stats, changed_only, extras = get_stats(); |
48 for stat, _ in pairs(stats) do | 48 for stat, _ in pairs(stats) do |
49 if stat == "/*/mod_measure_message_e2ee/message:rate" then | 49 if stat == "/"..module.host.."/mod_measure_message_e2ee/message:rate" then |
50 local new_message_count = extras[stat].total; | 50 local new_message_count = extras[stat].total; |
51 if new_message_count ~= message_count[1] then | 51 if not data or new_message_count ~= data.message_count then |
52 message_count = { new_message_count }; | 52 data = { message_count = new_message_count }; |
53 message_count_store:set("message_count", message_count); | 53 main_store:set("nodeinfo2", data); |
54 end | 54 end |
55 end | 55 end |
56 end | 56 end |
57 | 57 |
58 event.response.headers.content_type = "application/json"; | 58 event.response.headers.content_type = "application/json"; |
89 total = total_users; | 89 total = total_users; |
90 activeHalfyear = half_year_users; | 90 activeHalfyear = half_year_users; |
91 activeMonth = month_users; | 91 activeMonth = month_users; |
92 activeWeek = week_users; | 92 activeWeek = week_users; |
93 }; | 93 }; |
94 localPosts = message_count; | 94 localPosts = data.message_count; |
95 -- TODO: also count PubSub replies here. | 95 -- TODO: also count PubSub replies here. |
96 localComments = 0; | 96 localComments = 0; |
97 }; | 97 }; |
98 }); | 98 }); |
99 end; | 99 end; |