view mod_pubsub_stats/mod_pubsub_stats.lua @ 3123:f4e86d2662ec

mod_minimix: Handle having received no presence (should not happen)
author Kim Alvefur <zash@zash.se>
date Tue, 19 Jun 2018 22:00:17 +0200
parents 380f92276e57
children 8a870e0319db
line wrap: on
line source

local st = require "util.stanza";
local dt = require "util.datetime";

local pubsub = module:depends"pubsub";

local actor = module.host .. "/modules/" .. module.name;

local node = module:get_option_string(module.name .. "_node", "stats");

local function publish_stats(stats, stats_extra)
	local id = "current";
	local xitem = st.stanza("item", { id = id })
		:tag("query", { xmlns = "http://jabber.org/protocol/stats" });

	for name, value in pairs(stats) do
		local stat_extra = stats_extra[name];
		local unit = stat_extra and stat_extra.units;
		xitem:tag("stat", { name = name, unit = unit, value = tostring(value) }):up();
	end

	local ok, err = pubsub.service:publish(node, actor, id, xitem);
	if not ok then
		module:log("error", "Error publishing stats: %s", err);
	end
end

function module.load()
	pubsub.service:create(node, true);
	pubsub.service:set_affiliation(node, true, actor, "publisher");
end

module:hook_global("stats-updated", function (event)
	publish_stats(event.stats, event.stats_extra);
end);

function module.unload()
	pubsub.service:delete(node, true);
end