-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3870 from esl/api-modules/stat
Improve error handling in stat
- Loading branch information
Showing
4 changed files
with
64 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,52 @@ | ||
-module(stats_api). | ||
|
||
-export([incoming_s2s_number/0, outgoing_s2s_number/0, stats/1, stats/2]). | ||
-export([stats_mongooseimctl/1, stats_mongooseimctl/2]). | ||
|
||
-ignore_xref([stats_mongooseimctl/1, stats_mongooseimctl/2]). | ||
|
||
-include("mongoose.hrl"). | ||
-include("ejabberd_commands.hrl"). | ||
|
||
-spec incoming_s2s_number() -> non_neg_integer(). | ||
-spec incoming_s2s_number() -> {ok, non_neg_integer()}. | ||
incoming_s2s_number() -> | ||
length(supervisor:which_children(ejabberd_s2s_in_sup)). | ||
{ok, length(supervisor:which_children(ejabberd_s2s_in_sup))}. | ||
|
||
-spec outgoing_s2s_number() -> non_neg_integer(). | ||
-spec outgoing_s2s_number() -> {ok, non_neg_integer()}. | ||
outgoing_s2s_number() -> | ||
length(supervisor:which_children(ejabberd_s2s_out_sup)). | ||
|
||
-spec stats(binary()) -> integer() | {error, string()}. | ||
stats(Name) -> | ||
case Name of | ||
<<"uptimeseconds">> -> | ||
trunc(element(1, erlang:statistics(wall_clock))/1000); | ||
<<"registeredusers">> -> | ||
Domains = lists:flatmap(fun mongoose_domain_api:get_domains_by_host_type/1, | ||
?ALL_HOST_TYPES), | ||
lists:sum([ejabberd_auth:get_vh_registered_users_number(Domain) || Domain <- Domains]); | ||
<<"onlineusersnode">> -> | ||
ejabberd_sm:get_node_sessions_number(); | ||
<<"onlineusers">> -> | ||
ejabberd_sm:get_total_sessions_number(); | ||
_ -> | ||
{error, "Wrong command name."} | ||
{ok, length(supervisor:which_children(ejabberd_s2s_out_sup))}. | ||
|
||
-spec stats(binary()) -> {ok, integer()} | {not_found, string()}. | ||
stats(<<"uptimeseconds">>) -> | ||
{ok, trunc(element(1, erlang:statistics(wall_clock)) / 1000)}; | ||
stats(<<"registeredusers">>) -> | ||
Domains = lists:flatmap(fun mongoose_domain_api:get_domains_by_host_type/1, | ||
?ALL_HOST_TYPES), | ||
{ok, lists:sum([ejabberd_auth:get_vh_registered_users_number(Domain) || Domain <- Domains])}; | ||
stats(<<"onlineusersnode">>) -> | ||
{ok, ejabberd_sm:get_node_sessions_number()}; | ||
stats(<<"onlineusers">>) -> | ||
{ok, ejabberd_sm:get_total_sessions_number()}; | ||
stats(_Name) -> | ||
{not_found, "Stats not found"}. | ||
|
||
-spec stats(binary(), jid:server()) -> {ok, integer()} | {not_found, string()}. | ||
stats(<<"registeredusers">>, Host) -> | ||
{ok, ejabberd_auth:get_vh_registered_users_number(Host)}; | ||
stats(<<"onlineusers">>, Host) -> | ||
{ok, ejabberd_sm:get_vh_session_number(Host)}; | ||
stats(_Name, _Host) -> | ||
{not_found, "Stats not found"}. | ||
|
||
-spec stats_mongooseimctl(binary()) -> {ok | not_found, string()}. | ||
stats_mongooseimctl(Name) -> | ||
case stats(Name) of | ||
{ok, Stat} -> {ok, integer_to_list(Stat)}; | ||
Error -> Error | ||
end. | ||
|
||
-spec stats(binary(), jid:server()) -> integer() | {error, string()}. | ||
stats(Name, Host) -> | ||
case Name of | ||
<<"registeredusers">> -> | ||
ejabberd_auth:get_vh_registered_users_number(Host); | ||
<<"onlineusers">> -> | ||
ejabberd_sm:get_vh_session_number(Host); | ||
_ -> | ||
{error, "Wrong command name."} | ||
-spec stats_mongooseimctl(binary(), binary()) -> {ok | not_found, string()}. | ||
stats_mongooseimctl(Name, Host) -> | ||
case stats(Name, Host) of | ||
{ok, Stat} -> {ok, integer_to_list(Stat)}; | ||
Error -> Error | ||
end. |