diff --git a/big_tests/tests/graphql_stats_SUITE.erl b/big_tests/tests/graphql_stats_SUITE.erl index b068fe5ff3..374737758a 100644 --- a/big_tests/tests/graphql_stats_SUITE.erl +++ b/big_tests/tests/graphql_stats_SUITE.erl @@ -24,8 +24,10 @@ groups() -> [{admin_stats, [], admin_stats_handler()}]. admin_stats_handler() -> - [admin_stats_test, - admin_stats_domain_test]. + [admin_stats_global_test, + admin_stats_global_with_users_test, + admin_stats_domain_test, + admin_stats_domain_with_users_test]. init_per_suite(Config) -> escalus:init_per_suite(Config). @@ -43,22 +45,39 @@ init_per_testcase(CaseName, Config) -> escalus:init_per_testcase(CaseName, Config). end_per_testcase(CaseName, Config) -> + escalus_fresh:clean(), escalus:end_per_testcase(CaseName, Config). % Admin test cases -admin_stats_test(Config) -> +admin_stats_global_test(Config) -> GraphQlRequest = admin_get_stats(Config, #{}), - Result = ok_result(<<"stats">>, <<"stats">>, GraphQlRequest), + Result = ok_result(<<"stats">>, <<"globalStats">>, GraphQlRequest), #{<<"uptimeSeconds">> := UptimeSeconds, <<"registeredUsers">> := RegisteredUsers, <<"onlineUsersNode">> := OnlineUsersNode, <<"onlineUsers">> := OnlineUsers, <<"incomingS2S">> := IncomingS2S, <<"outgoingS2S">> := OutgoingS2S} = Result, - ?assertEqual(true, is_integer(UptimeSeconds)), + ?assertEqual(true, is_not_negative_integer(UptimeSeconds)), ?assertEqual(0, RegisteredUsers), ?assertEqual(0, OnlineUsersNode), ?assertEqual(0, OnlineUsers), - ?assertEqual(0, IncomingS2S), - ?assertEqual(0, OutgoingS2S). + ?assertEqual(true, is_not_negative_integer(IncomingS2S)), + ?assertEqual(true, is_not_negative_integer(OutgoingS2S)). + +admin_stats_global_with_users_test(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_stats_global_with_users_test/2). + +admin_stats_global_with_users_test(Config, _Alice) -> + GraphQlRequest = admin_get_stats(Config, #{}), + Result = ok_result(<<"stats">>, <<"globalStats">>, GraphQlRequest), + #{<<"uptimeSeconds">> := UptimeSeconds, <<"registeredUsers">> := RegisteredUsers, + <<"onlineUsersNode">> := OnlineUsersNode, <<"onlineUsers">> := OnlineUsers, + <<"incomingS2S">> := IncomingS2S, <<"outgoingS2S">> := OutgoingS2S} = Result, + ?assertEqual(true, is_not_negative_integer(UptimeSeconds)), + ?assertEqual(true, RegisteredUsers > 0), + ?assertEqual(1, OnlineUsersNode), + ?assertEqual(1, OnlineUsers), + ?assertEqual(true, is_not_negative_integer(IncomingS2S)), + ?assertEqual(true, is_not_negative_integer(OutgoingS2S)). admin_stats_domain_test(Config) -> Vars = #{<<"domain">> => domain()}, @@ -68,11 +87,22 @@ admin_stats_domain_test(Config) -> ?assertEqual(0, RegisteredUsers), ?assertEqual(0, OnlineUsers). +admin_stats_domain_with_users_test(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_stats_domain_with_users_test/2). + +admin_stats_domain_with_users_test(Config, _Alice) -> + Vars = #{<<"domain">> => domain()}, + GraphQlRequest = admin_get_stats_domain(Config, Vars), + Result = ok_result(<<"stats">>, <<"domainStats">>, GraphQlRequest), + #{<<"registeredUsers">> := RegisteredUsers, <<"onlineUsers">> := OnlineUsers} = Result, + ?assertEqual(1, RegisteredUsers), + ?assertEqual(1, OnlineUsers). + % Helpers admin_get_stats(Config, Vars) -> Query = <<"query Q1 - {stats{stats{uptimeSeconds registeredUsers onlineUsersNode + {stats{globalStats{uptimeSeconds registeredUsers onlineUsersNode onlineUsers incomingS2S outgoingS2S}}}">>, admin_send_mutation(Config, Vars, Query). @@ -90,3 +120,8 @@ error_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"errors">> := [Data]}}) - ok_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"data">> := Data}}) -> maps:get(What2, maps:get(What1, Data)). + +is_not_negative_integer(Number) when is_integer(Number), Number >= 0 -> + true; +is_not_negative_integer(_) -> + false. diff --git a/priv/graphql/schemas/admin/stats.gql b/priv/graphql/schemas/admin/stats.gql index e232c3ee99..c0658f09d5 100644 --- a/priv/graphql/schemas/admin/stats.gql +++ b/priv/graphql/schemas/admin/stats.gql @@ -1,14 +1,14 @@ "Allow admin to get statistics" type StatsAdminQuery @protected{ "allow admin to acquire all nodes' statistics" - stats: Stats + globalStats: GlobalStats @protected(type: GLOBAL) "allow admin to acquire domain's statistics" domainStats(domain: String!): DomainStats @protected(type: DOMAIN, args: ["domain"]) } -type Stats { +type GlobalStats { "uptime of the node" uptimeSeconds: Int "number of registered users" diff --git a/src/graphql/admin/mongoose_graphql_stats_admin_query.erl b/src/graphql/admin/mongoose_graphql_stats_admin_query.erl index 14c7aef51e..8706cf5f44 100644 --- a/src/graphql/admin/mongoose_graphql_stats_admin_query.erl +++ b/src/graphql/admin/mongoose_graphql_stats_admin_query.erl @@ -11,7 +11,7 @@ -include("mongoose.hrl"). -include("jlib.hrl"). -execute(_Ctx, stats, <<"stats">>, _Args) -> - {ok, stats}; +execute(_Ctx, stats, <<"globalStats">>, _Args) -> + {ok, globalStats}; execute(_Ctx, stats, <<"domainStats">>, #{<<"domain">> := Domain}) -> {ok, Domain}. diff --git a/src/graphql/admin/mongoose_graphql_stats.erl b/src/graphql/admin/mongoose_graphql_stats_global.erl similarity index 56% rename from src/graphql/admin/mongoose_graphql_stats.erl rename to src/graphql/admin/mongoose_graphql_stats_global.erl index 4d800d2246..0d2500e2ce 100644 --- a/src/graphql/admin/mongoose_graphql_stats.erl +++ b/src/graphql/admin/mongoose_graphql_stats_global.erl @@ -1,4 +1,4 @@ --module(mongoose_graphql_stats). +-module(mongoose_graphql_stats_global). -behaviour(mongoose_graphql). -export([execute/4]). @@ -11,15 +11,15 @@ -include("mongoose.hrl"). -include("jlib.hrl"). -execute(_Ctx, stats, <<"uptimeSeconds">>, _Args) -> +execute(_Ctx, globalStats, <<"uptimeSeconds">>, _Args) -> {ok, stats_api:stats(<<"uptimeseconds">>)}; -execute(_Ctx, stats, <<"registeredUsers">>, _Args) -> +execute(_Ctx, globalStats, <<"registeredUsers">>, _Args) -> {ok, stats_api:stats(<<"registeredusers">>)}; -execute(_Ctx, stats, <<"onlineUsersNode">>, _Args) -> +execute(_Ctx, globalStats, <<"onlineUsersNode">>, _Args) -> {ok, stats_api:stats(<<"onlineusersnode">>)}; -execute(_Ctx, stats, <<"onlineUsers">>, _Args) -> +execute(_Ctx, globalStats, <<"onlineUsers">>, _Args) -> {ok, stats_api:stats(<<"onlineusers">>)}; -execute(_Ctx, stats, <<"incomingS2S">>, _Args) -> +execute(_Ctx, globalStats, <<"incomingS2S">>, _Args) -> {ok, stats_api:incoming_s2s_number()}; -execute(_Ctx, stats, <<"outgoingS2S">>, _Args) -> +execute(_Ctx, globalStats, <<"outgoingS2S">>, _Args) -> {ok, stats_api:outgoing_s2s_number()}. diff --git a/src/graphql/mongoose_graphql.erl b/src/graphql/mongoose_graphql.erl index 9ea2401284..2cea470ae2 100644 --- a/src/graphql/mongoose_graphql.erl +++ b/src/graphql/mongoose_graphql.erl @@ -137,7 +137,7 @@ admin_mapping_rules() -> 'SessionAdminQuery' => mongoose_graphql_session_admin_query, 'StanzaAdminMutation' => mongoose_graphql_stanza_admin_mutation, 'StatsAdminQuery' => mongoose_graphql_stats_admin_query, - 'Stats' => mongoose_graphql_stats, + 'GlobalStats' => mongoose_graphql_stats_global, 'DomainStats' => mongoose_graphql_stats_domain, 'StanzaAdminQuery' => mongoose_graphql_stanza_admin_query, 'LastAdminMutation' => mongoose_graphql_last_admin_mutation,