DDDHuang 4 лет назад
Родитель
Сommit
103df6f06b

+ 2 - 0
apps/emqx_dashboard/include/emqx_dashboard.hrl

@@ -42,9 +42,11 @@
 -ifdef(TEST).
 -ifdef(TEST).
 %% for test
 %% for test
 -define(DEFAULT_SAMPLE_INTERVAL, 1).
 -define(DEFAULT_SAMPLE_INTERVAL, 1).
+-define(RPC_TIMEOUT, 50).
 -else.
 -else.
 %% dashboard monitor do sample interval, default 10s
 %% dashboard monitor do sample interval, default 10s
 -define(DEFAULT_SAMPLE_INTERVAL, 10).
 -define(DEFAULT_SAMPLE_INTERVAL, 10).
+-define(RPC_TIMEOUT, 5000).
 -endif.
 -endif.
 
 
 -define(DELTA_SAMPLER_LIST,
 -define(DELTA_SAMPLER_LIST,

+ 2 - 8
apps/emqx_dashboard/src/emqx_dashboard_monitor.erl

@@ -51,12 +51,6 @@
 %% 7 days = 7 * 24 * 60 * 60 * 1000 milliseconds
 %% 7 days = 7 * 24 * 60 * 60 * 1000 milliseconds
 -define(RETENTION_TIME, 7 * 24 * 60 * 60 * 1000).
 -define(RETENTION_TIME, 7 * 24 * 60 * 60 * 1000).
 
 
--ifdef(TEST).
--define(RPC_TIMEOUT, 50).
--else.
--define(RPC_TIMEOUT, 5000).
--endif.
-
 -record(state, {
 -record(state, {
     last
     last
     }).
     }).
@@ -140,7 +134,7 @@ current_rate(all) ->
 current_rate(Node) when Node == node() ->
 current_rate(Node) when Node == node() ->
     do_call(current_rate);
     do_call(current_rate);
 current_rate(Node) ->
 current_rate(Node) ->
-    case rpc:call(Node, ?MODULE, ?FUNCTION_NAME, [Node], ?RPC_TIMEOUT) of
+    case emqx_dashboard_proto_v1:current_rate(Node) of
         {badrpc, Reason} ->
         {badrpc, Reason} ->
             {badrpc, {Node, Reason}};
             {badrpc, {Node, Reason}};
         {ok, Rate} ->
         {ok, Rate} ->
@@ -201,7 +195,7 @@ do_sample(Node, Time) when Node == node() ->
     MS = match_spec(Time),
     MS = match_spec(Time),
     internal_format(ets:select(?TAB, MS));
     internal_format(ets:select(?TAB, MS));
 do_sample(Node, Time) ->
 do_sample(Node, Time) ->
-    case rpc:call(Node, ?MODULE, ?FUNCTION_NAME, [Node, Time], ?RPC_TIMEOUT) of
+    case emqx_dashboard_proto_v1:do_sample(Node, Time) of
         {badrpc, Reason} ->
         {badrpc, Reason} ->
             {badrpc, {Node, Reason}};
             {badrpc, {Node, Reason}};
         Res ->
         Res ->

+ 4 - 3
apps/emqx_dashboard/src/emqx_dashboard_monitor_api.erl

@@ -78,7 +78,8 @@ schema("/monitor_current/nodes/:node") ->
             description => <<"Node current status. Gauge and rate.">>,
             description => <<"Node current status. Gauge and rate.">>,
             parameters => [parameter_node()],
             parameters => [parameter_node()],
             responses => #{
             responses => #{
-                200 => hoconsc:mk(hoconsc:ref(sampler_current), #{})
+                200 => hoconsc:mk(hoconsc:ref(sampler_current), #{}),
+                400 => emqx_dashboard_swagger:error_codes(['BAD_RPC'], <<"Bad RPC">>)
             }
             }
         }
         }
     }.
     }.
@@ -86,7 +87,7 @@ schema("/monitor_current/nodes/:node") ->
 parameter_latest() ->
 parameter_latest() ->
     Info = #{
     Info = #{
         in => query,
         in => query,
-        nullable => true,
+        required => false,
         example => 5 * 60,
         example => 5 * 60,
         description => <<"The latest N seconds data. Like 300 for 5 min.">>
         description => <<"The latest N seconds data. Like 300 for 5 min.">>
     },
     },
@@ -95,7 +96,7 @@ parameter_latest() ->
 parameter_node() ->
 parameter_node() ->
     Info = #{
     Info = #{
         in => path,
         in => path,
-        nullable => false,
+        required => true,
         example => node(),
         example => node(),
         description => <<"EMQX node name.">>
         description => <<"EMQX node name.">>
     },
     },

+ 9 - 4
apps/emqx_dashboard/src/proto/emqx_dashboard_proto_v1.erl

@@ -19,7 +19,8 @@
 -behaviour(emqx_bpapi).
 -behaviour(emqx_bpapi).
 
 
 -export([ introduced_in/0
 -export([ introduced_in/0
-        , samplers/2
+        , do_sample/2
+        , current_rate/1
         ]).
         ]).
 
 
 -include("emqx_dashboard.hrl").
 -include("emqx_dashboard.hrl").
@@ -28,6 +29,10 @@
 introduced_in() ->
 introduced_in() ->
     "5.0.0".
     "5.0.0".
 
 
--spec samplers(node(), Latest:: pos_integer() | infinity) -> list(map()) | emqx_rpc:badrpc().
-samplers(Node, Latest) ->
-    rpc:call(Node, emqx_dashboard_monitor, samplers, [Node, Latest]).
+-spec do_sample(node(), Latest:: pos_integer() | infinity) -> list(map()) | emqx_rpc:badrpc().
+do_sample(Node, Latest) ->
+    rpc:call(Node, emqx_dashboard_monitor, do_sample, [Node, Latest], ?RPC_TIMEOUT).
+
+-spec current_rate(node()) -> {ok, map()} | emqx_rpc:badrpc().
+current_rate(Node) ->
+    rpc:call(Node, emqx_dashboard_monitor, current_rate, [Node], ?RPC_TIMEOUT).

+ 2 - 0
apps/emqx_dashboard/test/emqx_dashboard_monitor_SUITE.erl

@@ -131,8 +131,10 @@ do_request_api(Method, Request)->
             {error, socket_closed_remotely};
             {error, socket_closed_remotely};
         {ok, {{"HTTP/1.1", Code, _}, _, Return} }
         {ok, {{"HTTP/1.1", Code, _}, _, Return} }
             when Code >= 200 andalso Code =< 299 ->
             when Code >= 200 andalso Code =< 299 ->
+            ct:pal("Resp ~p ~p~n", [Code, Return]),
             {ok, emqx_json:decode(Return, [return_maps])};
             {ok, emqx_json:decode(Return, [return_maps])};
         {ok, {{"HTTP/1.1", Code, _}, _, Return} } ->
         {ok, {{"HTTP/1.1", Code, _}, _, Return} } ->
+            ct:pal("Resp ~p ~p~n", [Code, Return]),
             {error, {Code, emqx_json:decode(Return, [return_maps])}};
             {error, {Code, emqx_json:decode(Return, [return_maps])}};
         {error, Reason} ->
         {error, Reason} ->
             {error, Reason}
             {error, Reason}