annotate mod_stats39/mod_stats39.lua @ 4249:64aa1d9d70ac

mod_rest: Catch and log errors in callback promise chain From the code it looks like it should be possible to reply to an error stanza, but it did not. Turns out I was saved by my local developer mode module which throws errors if an attempt is made to create an errror reply to an error stanza. However nothing collects this error from the promise, so all I got was confusion.
author Kim Alvefur <zash@zash.se>
date Sun, 15 Nov 2020 16:25:49 +0100
parents ffc64d285a96
children bac3dae031ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3839
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local statsman = require "core.statsmanager";
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local st = require "util.stanza";
3846
3941768916f1 mod_stats39: Format numbers with a bit more precision
Kim Alvefur <zash@zash.se>
parents: 3840
diff changeset
3 local s_format = string.format;
3839
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
3847
ffc64d285a96 mod_stats39: Advertise namespace as feature (not part of the XEP?)
Kim Alvefur <zash@zash.se>
parents: 3846
diff changeset
5 module:add_feature("http://jabber.org/protocol/stats");
ffc64d285a96 mod_stats39: Advertise namespace as feature (not part of the XEP?)
Kim Alvefur <zash@zash.se>
parents: 3846
diff changeset
6
3839
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 module:hook("iq/host/http://jabber.org/protocol/stats:query", function (event)
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local origin, stanza = event.origin, event.stanza;
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local stats, _, extra = statsman.get_stats();
3840
054898e84a04 mod_stats39: Use a more local reference (silence luacheck warning)
Kim Alvefur <zash@zash.se>
parents: 3839
diff changeset
10 local reply = st.reply(stanza);
3839
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 reply:tag("query", { xmlns = "http://jabber.org/protocol/stats" });
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 for stat, value in pairs(stats) do
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local unit = extra[stat] and extra[stat].units;
3846
3941768916f1 mod_stats39: Format numbers with a bit more precision
Kim Alvefur <zash@zash.se>
parents: 3840
diff changeset
14 reply:tag("stat", { name = stat, unit = unit, value = s_format("%.12g", value) }):up();
3839
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 end
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 origin.send(reply);
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 return true;
a4b05f34a790 mod_stats39: Provides statsmanager stats via XEP-0039
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end)