1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% @author Piotr Nosek <piotr.nosek@erlang-solutions.com> |
3 |
|
%%% @doc MAM hooks for general metrics |
4 |
|
%%% |
5 |
|
%%% @end |
6 |
|
%%% Created : 13 Feb 2017 by Piotr Nosek |
7 |
|
%%%------------------------------------------------------------------- |
8 |
|
-module(mongoose_metrics_mam_hooks). |
9 |
|
|
10 |
|
-include("mongoose.hrl"). |
11 |
|
-include("jlib.hrl"). |
12 |
|
|
13 |
|
-export([get_mam_hooks/1, |
14 |
|
get_mam_muc_hooks/1]). |
15 |
|
|
16 |
|
%%------------------- |
17 |
|
%% Internal exports |
18 |
|
%%------------------- |
19 |
|
-export([mam_get_prefs/3, |
20 |
|
mam_set_prefs/3, |
21 |
|
mam_remove_archive/3, |
22 |
|
mam_lookup_messages/3, |
23 |
|
mam_archive_message/3, |
24 |
|
mam_flush_messages/3, |
25 |
|
mam_muc_get_prefs/3, |
26 |
|
mam_muc_set_prefs/3, |
27 |
|
mam_muc_remove_archive/3, |
28 |
|
mam_muc_lookup_messages/3, |
29 |
|
mam_muc_archive_message/3, |
30 |
|
mam_muc_flush_messages/3]). |
31 |
|
|
32 |
|
%%------------------- |
33 |
|
%% Implementation |
34 |
|
%%------------------- |
35 |
|
|
36 |
|
%% @doc Here will be declared which hooks should be registered when mod_mam_pm is enabled. |
37 |
|
-spec get_mam_hooks(mongooseim:host_type()) -> gen_hook:hook_list(). |
38 |
|
get_mam_hooks(HostType) -> |
39 |
:-( |
[ |
40 |
|
{mam_set_prefs, HostType, fun ?MODULE:mam_set_prefs/3, #{}, 50}, |
41 |
|
{mam_get_prefs, HostType, fun ?MODULE:mam_get_prefs/3, #{}, 50}, |
42 |
|
{mam_archive_message, HostType, fun ?MODULE:mam_archive_message/3, #{}, 50}, |
43 |
|
{mam_remove_archive, HostType, fun ?MODULE:mam_remove_archive/3, #{}, 50}, |
44 |
|
{mam_lookup_messages, HostType, fun ?MODULE:mam_lookup_messages/3, #{}, 100}, |
45 |
|
{mam_flush_messages, HostType, fun ?MODULE:mam_flush_messages/3, #{}, 50} |
46 |
|
]. |
47 |
|
|
48 |
|
%% @doc Here will be declared which hooks should be registered when mod_mam_muc is enabled. |
49 |
|
-spec get_mam_muc_hooks(mongooseim:host_type()) -> gen_hook:hook_list(). |
50 |
|
get_mam_muc_hooks(HostType) -> |
51 |
:-( |
[ |
52 |
|
{mam_muc_set_prefs, HostType, fun ?MODULE:mam_muc_set_prefs/3, #{}, 50}, |
53 |
|
{mam_muc_get_prefs, HostType, fun ?MODULE:mam_muc_get_prefs/3, #{}, 50}, |
54 |
|
{mam_muc_archive_message, HostType, fun ?MODULE:mam_muc_archive_message/3, #{}, 50}, |
55 |
|
{mam_muc_remove_archive, HostType, fun ?MODULE:mam_muc_remove_archive/3, #{}, 50}, |
56 |
|
{mam_muc_lookup_messages, HostType, fun ?MODULE:mam_muc_lookup_messages/3, #{}, 100}, |
57 |
|
{mam_muc_flush_messages, HostType, fun ?MODULE:mam_muc_flush_messages/3, #{}, 50} |
58 |
|
]. |
59 |
|
|
60 |
|
-spec mam_get_prefs(Acc, Params, Extra) -> {ok, Acc} when |
61 |
|
Acc :: mod_mam:preference() | {error, Reason :: term()}, |
62 |
|
Params :: map(), |
63 |
|
Extra :: gen_hook:extra(). |
64 |
|
mam_get_prefs(Result, _Params, #{host_type := HostType}) -> |
65 |
:-( |
mongoose_metrics:update(HostType, modMamPrefsGets, 1), |
66 |
:-( |
{ok, Result}. |
67 |
|
|
68 |
|
-spec mam_set_prefs(Acc, Params, Extra) -> {ok, Acc} when |
69 |
|
Acc :: term(), |
70 |
|
Params :: map(), |
71 |
|
Extra :: gen_hook:extra(). |
72 |
|
mam_set_prefs(Result, _Params, #{host_type := HostType}) -> |
73 |
:-( |
mongoose_metrics:update(HostType, modMamPrefsSets, 1), |
74 |
:-( |
{ok, Result}. |
75 |
|
|
76 |
|
-spec mam_remove_archive(Acc, Params, Extra) -> {ok, Acc} when |
77 |
|
Acc :: term(), |
78 |
|
Params :: #{archive_id := mod_mam:archive_id() | undefined, owner := jid:jid()}, |
79 |
|
Extra :: gen_hook:extra(). |
80 |
|
mam_remove_archive(Acc, _Params, #{host_type := HostType}) -> |
81 |
:-( |
mongoose_metrics:update(HostType, modMamArchiveRemoved, 1), |
82 |
:-( |
{ok, Acc}. |
83 |
|
|
84 |
|
-spec mam_lookup_messages(Acc, Params, Extra) -> {ok, Acc} when |
85 |
|
Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, |
86 |
|
Params :: mam_iq:lookup_params(), |
87 |
|
Extra :: gen_hook:extra(). |
88 |
|
mam_lookup_messages({ok, {_TotalCount, _Offset, MessageRows}} = Result, |
89 |
|
#{is_simple := IsSimple}, |
90 |
|
#{host_type := HostType}) -> |
91 |
:-( |
mongoose_metrics:update(HostType, modMamForwarded, length(MessageRows)), |
92 |
:-( |
mongoose_metrics:update(HostType, modMamLookups, 1), |
93 |
:-( |
case IsSimple of |
94 |
|
true -> |
95 |
:-( |
mongoose_metrics:update(HostType, [modMamLookups, simple], 1); |
96 |
|
_ -> |
97 |
:-( |
ok |
98 |
|
end, |
99 |
:-( |
{ok, Result}; |
100 |
|
mam_lookup_messages(Result = {error, _}, _Params, _Extra) -> |
101 |
:-( |
{ok, Result}. |
102 |
|
|
103 |
|
-spec mam_archive_message(Acc, Params, Extra) -> {ok, Acc} when |
104 |
|
Acc :: ok | {error, timeout}, |
105 |
|
Params :: mod_mam:archive_message_params(), |
106 |
|
Extra :: gen_hook:extra(). |
107 |
|
mam_archive_message(Result, _Params, #{host_type := HostType}) -> |
108 |
:-( |
mongoose_metrics:update(HostType, modMamArchived, 1), |
109 |
:-( |
{ok, Result}. |
110 |
|
|
111 |
|
-spec mam_flush_messages(Acc, Params, Extra) -> {ok, Acc} when |
112 |
|
Acc :: ok, |
113 |
|
Params :: #{count := integer()}, |
114 |
|
Extra :: gen_hook:extra(). |
115 |
|
mam_flush_messages(Acc, #{count := MessageCount}, #{host_type := HostType}) -> |
116 |
:-( |
mongoose_metrics:update(HostType, modMamFlushed, MessageCount), |
117 |
:-( |
{ok, Acc}. |
118 |
|
|
119 |
|
%% ---------------------------------------------------------------------------- |
120 |
|
%% mod_mam_muc |
121 |
|
|
122 |
|
-spec mam_muc_get_prefs(Acc, Params, Extra) -> {ok, Acc} when |
123 |
|
Acc :: mod_mam:preference() | {error, Reason :: term()}, |
124 |
|
Params :: map(), |
125 |
|
Extra :: gen_hook:extra(). |
126 |
|
mam_muc_get_prefs(Result, _Params, #{host_type := HostType}) -> |
127 |
:-( |
mongoose_metrics:update(HostType, modMucMamPrefsGets, 1), |
128 |
:-( |
{ok, Result}. |
129 |
|
|
130 |
|
-spec mam_muc_set_prefs(Acc, Params, Extra) -> {ok, Acc} when |
131 |
|
Acc :: term(), |
132 |
|
Params :: map(), |
133 |
|
Extra :: gen_hook:extra(). |
134 |
|
mam_muc_set_prefs(Result, _Params, #{host_type := HostType}) -> |
135 |
:-( |
mongoose_metrics:update(HostType, modMucMamPrefsSets, 1), |
136 |
:-( |
{ok, Result}. |
137 |
|
|
138 |
|
-spec mam_muc_remove_archive(Acc, Params, Extra) -> {ok, Acc} when |
139 |
|
Acc :: term(), |
140 |
|
Params :: #{archive_id := mod_mam:archive_id() | undefined, room := jid:jid()}, |
141 |
|
Extra :: gen_hook:extra(). |
142 |
|
mam_muc_remove_archive(Acc, _Params, #{host_type := HostType}) -> |
143 |
:-( |
mongoose_metrics:update(HostType, modMucMamArchiveRemoved, 1), |
144 |
:-( |
{ok, Acc}. |
145 |
|
|
146 |
|
-spec mam_muc_lookup_messages(Acc, Params, Extra) -> {ok, Acc} when |
147 |
|
Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, |
148 |
|
Params :: mam_iq:lookup_params(), |
149 |
|
Extra :: gen_hook:extra(). |
150 |
|
mam_muc_lookup_messages({ok, {_TotalCount, _Offset, MessageRows}} = Result, |
151 |
|
_Params, |
152 |
|
#{host_type := HostType}) -> |
153 |
:-( |
mongoose_metrics:update(HostType, modMucMamForwarded, length(MessageRows)), |
154 |
:-( |
mongoose_metrics:update(HostType, modMucMamLookups, 1), |
155 |
:-( |
{ok, Result}; |
156 |
|
mam_muc_lookup_messages(Result = {error, _}, _Params, _Extra) -> |
157 |
:-( |
{ok, Result}. |
158 |
|
|
159 |
|
-spec mam_muc_archive_message(Acc, Params, Extra) -> {ok, Acc} when |
160 |
|
Acc :: ok | {error, timeout}, |
161 |
|
Params :: mod_mam:archive_message_params(), |
162 |
|
Extra :: gen_hook:extra(). |
163 |
|
mam_muc_archive_message(Result, _Params, #{host_type := HostType}) -> |
164 |
:-( |
mongoose_metrics:update(HostType, modMucMamArchived, 1), |
165 |
:-( |
{ok, Result}. |
166 |
|
|
167 |
|
-spec mam_muc_flush_messages(Acc, Params, Extra) -> {ok, Acc} when |
168 |
|
Acc :: ok, |
169 |
|
Params :: map(), |
170 |
|
Extra :: gen_hook:extra(). |
171 |
|
mam_muc_flush_messages(Acc, #{count := MessageCount}, #{host_type := HostType}) -> |
172 |
:-( |
mongoose_metrics:update(HostType, modMucMamFlushed, MessageCount), |
173 |
:-( |
{ok, Acc}. |