-
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.
- Loading branch information
Janusz Jakubiec
authored and
Janusz Jakubiec
committed
Jul 6, 2022
1 parent
bbdc781
commit ed6afad
Showing
11 changed files
with
211 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
-module(graphql_stats_SUITE). | ||
|
||
-compile([export_all, nowarn_export_all]). | ||
|
||
-import(distributed_helper, [require_rpc_nodes/1]). | ||
-import(domain_helper, [host_type/0, domain/0]). | ||
-import(graphql_helper, [execute_user/3, execute_auth/2, user_to_bin/1]). | ||
-import(config_parser_helper, [mod_config/2]). | ||
-import(mongooseimctl_helper, [mongooseimctl/3, rpc_call/3]). | ||
|
||
-include_lib("common_test/include/ct.hrl"). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
-include_lib("exml/include/exml.hrl"). | ||
-include_lib("escalus/include/escalus.hrl"). | ||
-include("../../include/mod_roster.hrl"). | ||
|
||
suite() -> | ||
require_rpc_nodes([mim]) ++ escalus:suite(). | ||
|
||
all() -> | ||
[{group, admin_stats}]. | ||
|
||
groups() -> | ||
[{admin_stats, [], admin_stats_handler()}]. | ||
|
||
admin_stats_handler() -> | ||
[admin_get_incoming_s2s_number_test, | ||
admin_get_outgoing_s2s_number_test, | ||
admin_stats_no_stat_name_test, | ||
admin_stats_domain_no_stat_name_test, | ||
admin_stats_test, | ||
admin_stats_domain_test]. | ||
|
||
init_per_suite(Config) -> | ||
escalus:init_per_suite(Config). | ||
|
||
end_per_suite(Config) -> | ||
escalus:end_per_suite(Config). | ||
|
||
init_per_group(_, Config) -> | ||
graphql_helper:init_admin_handler(Config). | ||
|
||
end_per_group(_, _Config) -> | ||
escalus_fresh:clean(). | ||
|
||
init_per_testcase(CaseName, Config) -> | ||
escalus:init_per_testcase(CaseName, Config). | ||
|
||
end_per_testcase(CaseName, Config) -> | ||
escalus:end_per_testcase(CaseName, Config). | ||
|
||
% Admin test cases | ||
|
||
admin_get_incoming_s2s_number_test(Config) -> | ||
GraphQlRequest = admin_get_incoming_s2s(Config, #{}), | ||
Number = ok_result(<<"stats">>, <<"getIncomingS2SNumber">>, GraphQlRequest), | ||
?assertEqual(0, Number). | ||
|
||
admin_get_outgoing_s2s_number_test(Config) -> | ||
GraphQlRequest = admin_get_outgoing_s2s(Config, #{}), | ||
Number = ok_result(<<"stats">>, <<"getOutgoingS2SNumber">>, GraphQlRequest), | ||
?assertEqual(0, Number). | ||
|
||
admin_stats_no_stat_name_test(Config) -> | ||
GraphQlRequest = admin_get_stats(Config, #{<<"statName">> => <<"AAA">>}), | ||
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest), | ||
?assertEqual(<<"no_command_error">>, ParsedResult). | ||
|
||
admin_stats_domain_no_stat_name_test(Config) -> | ||
Vars = #{<<"statName">> => <<"AAA">>, <<"domain">> => domain()}, | ||
GraphQlRequest = admin_get_stats_domain(Config, Vars), | ||
ParsedResult = error_result(<<"extensions">>, <<"code">>, GraphQlRequest), | ||
?assertEqual(<<"no_command_error">>, ParsedResult). | ||
|
||
admin_stats_test(Config) -> | ||
GraphQlRequest = admin_get_stats(Config, #{<<"statName">> => <<"onlineusers">>}), | ||
Number = ok_result(<<"stats">>, <<"stats">>, GraphQlRequest), | ||
?assertEqual(0, Number). | ||
|
||
admin_stats_domain_test(Config) -> | ||
Vars = #{<<"statName">> => <<"onlineusers">>, <<"domain">> => domain()}, | ||
GraphQlRequest = admin_get_stats(Config, Vars), | ||
Number = ok_result(<<"stats">>, <<"stats">>, GraphQlRequest), | ||
?assertEqual(0, Number). | ||
|
||
% Helpers | ||
|
||
admin_get_incoming_s2s(Config, Vars) -> | ||
Query = <<"query Q1 | ||
{stats{getIncomingS2SNumber}}">>, | ||
admin_send_mutation(Config, Vars, Query). | ||
|
||
admin_get_outgoing_s2s(Config, Vars) -> | ||
Query = <<"query Q1 | ||
{stats{getOutgoingS2SNumber}}">>, | ||
admin_send_mutation(Config, Vars, Query). | ||
|
||
admin_get_stats(Config, Vars) -> | ||
Query = <<"query Q1($statName: String!) | ||
{stats{stats(statName: $statName)}}">>, | ||
admin_send_mutation(Config, Vars, Query). | ||
|
||
admin_get_stats_domain(Config, Vars) -> | ||
Query = <<"query Q1($statName: String!, $domain: String) | ||
{stats{stats(statName: $statName, domain: $domain)}}">>, | ||
admin_send_mutation(Config, Vars, Query). | ||
|
||
admin_send_mutation(Config, Vars, Query) -> | ||
Body = #{query => Query, operationName => <<"Q1">>, variables => Vars}, | ||
execute_auth(Body, Config). | ||
|
||
error_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"errors">> := [Data]}}) -> | ||
maps:get(What2, maps:get(What1, Data)). | ||
|
||
ok_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"data">> := Data}}) -> | ||
maps:get(What2, maps:get(What1, Data)). |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"Allow admin to get statistics" | ||
type StatsAdminQuery @protected{ | ||
"Get number of incoming s2s connections" | ||
getIncomingS2SNumber: Int | ||
"Get number of outgoing s2s connections" | ||
getOutgoingS2SNumber: Int | ||
"Get specific stat" | ||
stats(statName: String!, domain: String): Int | ||
@protected(type: DOMAIN, args: ["domain"]) | ||
} |
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 |
---|---|---|
|
@@ -27,11 +27,7 @@ | |
-author('[email protected]'). | ||
|
||
|
||
-export([ | ||
commands/0, | ||
|
||
stats/1, stats/2 | ||
]). | ||
-export([commands/0]). | ||
|
||
-ignore_xref([commands/0, stats/1, stats/2]). | ||
|
||
|
@@ -48,46 +44,13 @@ commands() -> | |
#ejabberd_commands{name = stats, tags = [stats], | ||
desc = "Get statistical value:" | ||
" registeredusers onlineusers onlineusersnode uptimeseconds", | ||
module = ?MODULE, function = stats, | ||
module = stats_api, function = stats, | ||
args = [{name, binary}], | ||
result = {stat, integer}}, | ||
#ejabberd_commands{name = stats_host, tags = [stats], | ||
desc = "Get statistical value for this host:" | ||
" registeredusers onlineusers", | ||
module = ?MODULE, function = stats, | ||
module = stats_api, function = stats, | ||
args = [{name, binary}, {host, binary}], | ||
result = {stat, integer}} | ||
]. | ||
|
||
%%% | ||
%%% Stats | ||
%%% | ||
|
||
-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."} | ||
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."} | ||
end. |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-module(mongoose_graphql_stats_admin_query). | ||
-behaviour(mongoose_graphql). | ||
|
||
-export([execute/4]). | ||
|
||
-import(mongoose_graphql_helper, [make_error/2]). | ||
|
||
-ignore_xref([execute/4]). | ||
|
||
-include("../mongoose_graphql_types.hrl"). | ||
-include("mongoose.hrl"). | ||
-include("jlib.hrl"). | ||
|
||
execute(_Ctx, stats, <<"getIncomingS2SNumber">>, #{}) -> | ||
{ok, stats_api:incoming_s2s_number()}; | ||
execute(_Ctx, stats, <<"getOutgoingS2SNumber">>, #{}) -> | ||
{ok, stats_api:outgoing_s2s_number()}; | ||
execute(_Ctx, stats, <<"stats">>, #{<<"domain">> := null, <<"statName">> := StatName}) -> | ||
case stats_api:stats(StatName) of | ||
{error, String} -> | ||
make_error({no_command_error, String}, #{statName => StatName}); | ||
Result -> | ||
{ok, Result} | ||
end; | ||
execute(_Ctx, stats, <<"stats">>, #{<<"domain">> := Domain, <<"statName">> := StatName}) -> | ||
case stats_api:stats(StatName, Domain) of | ||
{error, String} -> | ||
make_error({no_command_error, String}, #{statName => StatName}); | ||
Result -> | ||
{ok, Result} | ||
end. |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
-module(stats_api). | ||
|
||
-export([incoming_s2s_number/0, outgoing_s2s_number/0, stats/1, stats/2]). | ||
|
||
-include("mongoose.hrl"). | ||
-include("ejabberd_commands.hrl"). | ||
|
||
-spec incoming_s2s_number() -> non_neg_integer(). | ||
incoming_s2s_number() -> | ||
length(supervisor:which_children(ejabberd_s2s_in_sup)). | ||
|
||
-spec outgoing_s2s_number() -> 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."} | ||
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."} | ||
end. |