Annotate

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
child 3068:380f92276e57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3067
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require "util.stanza";
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local dt = require "util.datetime";
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local pubsub = module:depends"pubsub";
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local actor = module.host .. "/modules/" .. module.name;
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local function publish_stats(stats, stats_extra)
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local id = "current";
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local xitem = st.stanza("item", { id = id })
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 :tag("query", { xmlns = "http://jabber.org/protocol/stats" });
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 for name, value in pairs(stats) do
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local stat_extra = stats_extra[name];
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local unit = stat_extra and stat_extra.units;
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 xitem:tag("stat", { name = name, unit = unit, value = tostring(value) }):up();
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local ok, err = pubsub.service:publish("stats", actor, id, xitem);
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 if not ok then
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 module:log("error", "Error publishing stats: %s", err);
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 function module.load()
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 pubsub.service:create("stats", true);
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 pubsub.service:set_affiliation("stats", true, actor, "publisher");
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 module:hook_global("stats-updated", function (event)
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 publish_stats(event.stats, event.stats_extra);
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end);
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 function module.unload()
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 pubsub.service:delete("stats", true);
b01ef74c9fc0 mod_pubsub_stats: Simple module that publishes stats in XEP-0039 format
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end