Merge pull request #7265 from DDDHuang/monitor_api_fix

fix: monitor api query string param latest: legal & default value inf…
This commit is contained in:
zhongwencool 2022-03-10 18:14:00 +08:00 committed by GitHub
commit 63b2501a1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 10 deletions

View File

@ -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.

View File

@ -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}} ->

View File

@ -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) ->