mod_nodeinfo2/mod_nodeinfo2.lua
changeset 3712 0a800463bc08
parent 3711 3248295e1b16
child 3714 7f5ce667d93e
equal deleted inserted replaced
3711:3248295e1b16 3712:0a800463bc08
     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 get_stats = require "core.statsmanager".get_stats;
     3 local get_stats = require "core.statsmanager".get_stats;
       
     4 local os_time = os.time;
     4 
     5 
     5 module:depends("http");
     6 module:depends("http");
       
     7 module:depends("lastlog");
     6 module:depends("measure_message_e2ee");
     8 module:depends("measure_message_e2ee");
     7 
     9 
       
    10 local store = module:open_store("lastlog");
       
    11 
     8 local total_users = 0;
    12 local total_users = 0;
     9 for _ in require "core.usermanager".users(module.host) do -- TODO refresh at some interval?
    13 local half_year_users = 0;
       
    14 local month_users = 0;
       
    15 local week_users = 0;
       
    16 for user in require "core.usermanager".users(module.host) do -- TODO refresh at some interval?
    10 	total_users = total_users + 1;
    17 	total_users = total_users + 1;
       
    18 	local lastlog = store:get(user);
       
    19 	if lastlog and lastlog.timestamp then
       
    20 		local delta = os_time() - lastlog.timestamp;
       
    21 		if delta < 6 * 30 * 24 * 60 * 60 then
       
    22 			half_year_users = half_year_users + 1;
       
    23 		end
       
    24 		if delta < 30 * 24 * 60 * 60 then
       
    25 			month_users = month_users + 1;
       
    26 		end
       
    27 		if delta < 7 * 24 * 60 * 60 then
       
    28 			week_users = week_users + 1;
       
    29 		end
       
    30 	end
       
    31 end
       
    32 
       
    33 -- Remove the properties if we couldn’t find a single active user.  It most likely means mod_lastlog isn’t in use.
       
    34 if half_year_users == 0 and month_users == 0 and week_users == 0 then
       
    35 	half_year_users = nil;
       
    36 	month_users = nil;
       
    37 	week_users = nil;
    11 end
    38 end
    12 
    39 
    13 module:provides("http", {
    40 module:provides("http", {
    14 	default_path = "/.well-known/x-nodeinfo2";
    41 	default_path = "/.well-known/x-nodeinfo2";
    15 	route = {
    42 	route = {
    16 		GET = function (event)
    43 		GET = function (event)
    17 			local stats, changed_only, extras = get_stats();
    44 			local stats, changed_only, extras = get_stats();
    18 			local message_count = nil;
    45 			local message_count = nil;
    19 			for stat, value in pairs(stats) do
    46 			for stat, _ in pairs(stats) do
    20 				if stat == "/*/mod_measure_message_e2ee/message:rate" then
    47 				if stat == "/*/mod_measure_message_e2ee/message:rate" then
    21 					message_count = extras[stat].total;
    48 					message_count = extras[stat].total;
    22 				end
    49 				end
    23 			end
    50 			end
    24 
    51 
    52 				--]]
    79 				--]]
    53 				openRegistrations = module:get_option_boolean("allow_registration", false);
    80 				openRegistrations = module:get_option_boolean("allow_registration", false);
    54 				usage = {
    81 				usage = {
    55 					users = {
    82 					users = {
    56 						total = total_users;
    83 						total = total_users;
    57 						-- TODO how would one calculate these?
    84 						activeHalfyear = half_year_users;
    58 						-- activeHalfyear = 1;
    85 						activeMonth = month_users;
    59 						-- activeMonth = 1;
    86 						activeWeek = week_users;
    60 						-- activeWeek = 1;
       
    61 					};
    87 					};
    62 					localPosts = message_count;
    88 					localPosts = message_count;
    63 					localComments = message_count;
    89 					localComments = message_count;
    64 				};
    90 				};
    65 			});
    91 			});