comparison mod_pubsub_stats/mod_pubsub_stats.lua @ 3067:b01ef74c9fc0

mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
author Kim Alvefur <zash@zash.se>
date Tue, 29 May 2018 05:31:56 +0200
parents
children 380f92276e57
comparison
equal deleted inserted replaced
3066:cefc375d0929 3067:b01ef74c9fc0
1 local st = require "util.stanza";
2 local dt = require "util.datetime";
3
4 local pubsub = module:depends"pubsub";
5
6 local actor = module.host .. "/modules/" .. module.name;
7
8 local function publish_stats(stats, stats_extra)
9 local id = "current";
10 local xitem = st.stanza("item", { id = id })
11 :tag("query", { xmlns = "http://jabber.org/protocol/stats" });
12
13 for name, value in pairs(stats) do
14 local stat_extra = stats_extra[name];
15 local unit = stat_extra and stat_extra.units;
16 xitem:tag("stat", { name = name, unit = unit, value = tostring(value) }):up();
17 end
18
19 local ok, err = pubsub.service:publish("stats", actor, id, xitem);
20 if not ok then
21 module:log("error", "Error publishing stats: %s", err);
22 end
23 end
24
25 function module.load()
26 pubsub.service:create("stats", true);
27 pubsub.service:set_affiliation("stats", true, actor, "publisher");
28 end
29
30 module:hook_global("stats-updated", function (event)
31 publish_stats(event.stats, event.stats_extra);
32 end);
33
34 function module.unload()
35 pubsub.service:delete("stats", true);
36 end