Преглед изворни кода

fix: monitor api query string param latest: legal & default value infinity

DDDHuang пре 3 година
родитељ
комит
03ca53d9b3

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

@@ -75,14 +75,7 @@ samplers() ->
     format(do_sample(all, infinity)).
 
 samplers(NodeOrCluster, Latest) ->
-    Time =
-        case Latest of
-            infinity ->
-                infinity;
-            Latest when is_integer(Latest) ->
-                Now = erlang:system_time(millisecond),
-                Now - (Latest * 1000)
-        end,
+    Time = latest2time(Latest),
     case format(do_sample(NodeOrCluster, Time)) of
         {badrpc, Reason} ->
             {badrpc, Reason};
@@ -90,6 +83,9 @@ samplers(NodeOrCluster, Latest) ->
             granularity_adapter(List)
     end.
 
+latest2time(infinity) -> infinity;
+latest2time(Latest) -> erlang:system_time(millisecond) - (Latest * 1000).
+
 %% When the number of samples exceeds 1000, it affects the rendering speed of dashboard UI.
 %% granularity_adapter is an oversampling of the samples.
 %% Use more granular data and reduce data density.

+ 2 - 2
apps/emqx_dashboard/src/emqx_dashboard_monitor_api.erl

@@ -91,7 +91,7 @@ parameter_latest() ->
         example => 5 * 60,
         description => <<"The latest N seconds data. Like 300 for 5 min.">>
     },
-    {latest, hoconsc:mk(integer(), Info)}.
+    {latest, hoconsc:mk(range(1, inf), Info)}.
 
 parameter_node() ->
     Info = #{
@@ -117,7 +117,7 @@ fields(sampler_current) ->
 %% API
 
 monitor(get, #{query_string := QS, bindings := Bindings}) ->
-    Latest = maps:get(<<"latest">>, QS, 1000),
+    Latest = maps:get(<<"latest">>, QS, infinity),
     Node = binary_to_atom(maps:get(node, Bindings, <<"all">>)),
     case emqx_dashboard_monitor:samplers(Node, Latest) of
         {badrpc, {Node, Reason}} ->

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

@@ -107,6 +107,10 @@ t_monitor_api_error(_) ->
         request(["monitor", "nodes", 'emqx@127.0.0.2']),
     {error, {400, #{<<"code">> := <<"BAD_RPC">>}}} =
         request(["monitor_current", "nodes", 'emqx@127.0.0.2']),
+    {error, {400, #{<<"code">> := <<"BAD_REQUEST">>}}} =
+        request(["monitor"], "latest=0"),
+    {error, {400, #{<<"code">> := <<"BAD_REQUEST">>}}} =
+        request(["monitor"], "latest=-1"),
     ok.
 
 request(Path) ->