Annotate

mod_nodeinfo2/mod_nodeinfo2.lua @ 3708:0a800463bc08

mod_nodeinfo2: Fetch active users from mod_lastlog.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 10 Oct 2019 20:09:23 +0200
parent 3707:3248295e1b16
child 3710:7f5ce667d93e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local json = require "util.json";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local array = require "util.array";
3707
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
3 local get_stats = require "core.statsmanager".get_stats;
3708
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
4 local os_time = os.time;
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 module:depends("http");
3708
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
7 module:depends("lastlog");
3707
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
8 module:depends("measure_message_e2ee");
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
3708
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
10 local store = module:open_store("lastlog");
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
11
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local total_users = 0;
3708
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
13 local half_year_users = 0;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
14 local month_users = 0;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
15 local week_users = 0;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
16 for user in require "core.usermanager".users(module.host) do -- TODO refresh at some interval?
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 total_users = total_users + 1;
3708
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
18 local lastlog = store:get(user);
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
19 if lastlog and lastlog.timestamp then
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
20 local delta = os_time() - lastlog.timestamp;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
21 if delta < 6 * 30 * 24 * 60 * 60 then
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
22 half_year_users = half_year_users + 1;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
23 end
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
24 if delta < 30 * 24 * 60 * 60 then
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
25 month_users = month_users + 1;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
26 end
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
27 if delta < 7 * 24 * 60 * 60 then
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
28 week_users = week_users + 1;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
29 end
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
30 end
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
31 end
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
32
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
33 -- Remove the properties if we couldn’t find a single active user. It most likely means mod_lastlog isn’t in use.
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
34 if half_year_users == 0 and month_users == 0 and week_users == 0 then
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
35 half_year_users = nil;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
36 month_users = nil;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
37 week_users = nil;
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 module:provides("http", {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 default_path = "/.well-known/x-nodeinfo2";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 route = {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 GET = function (event)
3707
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
44 local stats, changed_only, extras = get_stats();
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
45 local message_count = nil;
3708
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
46 for stat, _ in pairs(stats) do
3707
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
47 if stat == "/*/mod_measure_message_e2ee/message:rate" then
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
48 message_count = extras[stat].total;
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
49 end
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
50 end
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
51
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 event.response.headers.content_type = "application/json";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 return json.encode({
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 version = "1.0";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 server = {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 baseUrl = module:http_url("","/");
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 name = module.host;
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 software = "Prosody";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 version = prosody.version;
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 };
3706
36b645e94325 mod_nodeinfo2: Add some TODOs
Kim Alvefur <zash@zash.se>
parents: 3705
diff changeset
61 --[[ TODO re-use data from mod_server_contact_info ?
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 organization = {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 name = "";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 contact = "";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 account = "";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 };
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 --]]
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 protocols = array {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 "xmpp",
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 };
3706
36b645e94325 mod_nodeinfo2: Add some TODOs
Kim Alvefur <zash@zash.se>
parents: 3705
diff changeset
71 --[[ TODO would be cool to identify local transports
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 services = {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 inbound = array {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 "irc";
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 };
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 outbound = array {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 };
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 };
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 --]]
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 openRegistrations = module:get_option_boolean("allow_registration", false);
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 usage = {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 users = {
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 total = total_users;
3708
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
84 activeHalfyear = half_year_users;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
85 activeMonth = month_users;
0a800463bc08 mod_nodeinfo2: Fetch active users from mod_lastlog.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3707
diff changeset
86 activeWeek = week_users;
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 };
3707
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
88 localPosts = message_count;
3248295e1b16 mod_nodeinfo2: Retrieve message count from mod_measure_message_e2ee for localPosts and localComments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3706
diff changeset
89 localComments = message_count;
3705
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 };
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 });
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end;
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 }
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 });
5d3d8b75dee9 mod_nodeinfo2: Expose service metadata per NodeInfo2
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95