fix: list_rules support enable_paging
This commit is contained in:
parent
d1254faf6f
commit
9b433eb002
|
@ -272,11 +272,14 @@ update_rule(#{id := Id}, Params) ->
|
||||||
return({error, 400, ?ERR_BADARGS(Reason)})
|
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
list_rules(_Bindings, []) ->
|
|
||||||
return_all(emqx_rule_registry:get_rules_ordered_by_ts());
|
|
||||||
list_rules(_Bindings, Params) ->
|
list_rules(_Bindings, Params) ->
|
||||||
SortFun = fun(#{created_at := C1}, #{created_at := C2}) -> C1 > C2 end,
|
case proplists:get_value(<<"enable_paging">>, Params, true) of
|
||||||
return({ok, emqx_mgmt_api:node_query(node(), Params, ?RULE_QS_SCHEMA, {?MODULE, query}, SortFun)}).
|
true ->
|
||||||
|
SortFun = fun(#{created_at := C1}, #{created_at := C2}) -> C1 > C2 end,
|
||||||
|
return({ok, emqx_mgmt_api:node_query(node(), Params, ?RULE_QS_SCHEMA, {?MODULE, query}, SortFun)});
|
||||||
|
false ->
|
||||||
|
return_all(emqx_rule_registry:get_rules_ordered_by_ts())
|
||||||
|
end.
|
||||||
|
|
||||||
show_rule(#{id := Id}, _Params) ->
|
show_rule(#{id := Id}, _Params) ->
|
||||||
reply_with(fun emqx_rule_registry:get_rule/1, Id).
|
reply_with(fun emqx_rule_registry:get_rule/1, Id).
|
||||||
|
|
|
@ -528,29 +528,29 @@ t_list_rule_api(_Config) ->
|
||||||
end, lists:seq(1, 20)),
|
end, lists:seq(1, 20)),
|
||||||
|
|
||||||
{ok, #{code := 0, data := Rules11}} = emqx_rule_engine_api:list_rules(#{},
|
{ok, #{code := 0, data := Rules11}} = emqx_rule_engine_api:list_rules(#{},
|
||||||
[{<<"_limit">>,<<"10">>}, {<<"_page">>, <<"1">>}]),
|
[{<<"_limit">>,<<"10">>}, {<<"_page">>, <<"1">>}, {<<"enable_paging">>, true}]),
|
||||||
?assertEqual(10, length(Rules11)),
|
?assertEqual(10, length(Rules11)),
|
||||||
{ok, #{code := 0, data := Rules12}} = emqx_rule_engine_api:list_rules(#{},
|
{ok, #{code := 0, data := Rules12}} = emqx_rule_engine_api:list_rules(#{},
|
||||||
[{<<"_limit">>,<<"10">>}, {<<"_page">>, <<"2">>}]),
|
[{<<"_limit">>,<<"10">>}, {<<"_page">>, <<"2">>}, {<<"enable_paging">>, true}]),
|
||||||
?assertEqual(10, length(Rules12)),
|
?assertEqual(10, length(Rules12)),
|
||||||
Rules1 = Rules11 ++ Rules12,
|
Rules1 = Rules11 ++ Rules12,
|
||||||
|
|
||||||
[RuleID | _] = AddIds,
|
[RuleID | _] = AddIds,
|
||||||
{ok, #{code := 0}} = emqx_rule_engine_api:update_rule(#{id => RuleID},
|
{ok, #{code := 0}} = emqx_rule_engine_api:update_rule(#{id => RuleID},
|
||||||
[{<<"enabled">>, false}]),
|
[{<<"enabled">>, false}]),
|
||||||
Params1 = [{<<"enabled">>,<<"true">>}],
|
Params1 = [{<<"enabled">>,<<"true">>}, {<<"enable_paging">>, true}],
|
||||||
{ok, #{code := 0, data := Rules2}} = emqx_rule_engine_api:list_rules(#{}, Params1),
|
{ok, #{code := 0, data := Rules2}} = emqx_rule_engine_api:list_rules(#{}, Params1),
|
||||||
?assert(lists:all(fun(#{id := ID}) -> ID =/= RuleID end, Rules2)),
|
?assert(lists:all(fun(#{id := ID}) -> ID =/= RuleID end, Rules2)),
|
||||||
|
|
||||||
Params2 = [{<<"for">>, RuleID}],
|
Params2 = [{<<"for">>, RuleID}, {<<"enable_paging">>, true}],
|
||||||
{ok, #{code := 0, data := Rules3}} = emqx_rule_engine_api:list_rules(#{}, Params2),
|
{ok, #{code := 0, data := Rules3}} = emqx_rule_engine_api:list_rules(#{}, Params2),
|
||||||
?assert(lists:all(fun(#{id := ID}) -> ID =:= RuleID end, Rules3)),
|
?assert(lists:all(fun(#{id := ID}) -> ID =:= RuleID end, Rules3)),
|
||||||
|
|
||||||
Params3 = [{<<"_like_id">>,<<"rule:">>}],
|
Params3 = [{<<"_like_id">>,<<"rule:">>}, {<<"enable_paging">>, true}],
|
||||||
{ok, #{code := 0, data := Rules4}} = emqx_rule_engine_api:list_rules(#{}, Params3),
|
{ok, #{code := 0, data := Rules4}} = emqx_rule_engine_api:list_rules(#{}, Params3),
|
||||||
?assertEqual(length(Rules1), length(Rules4)),
|
?assertEqual(length(Rules1), length(Rules4)),
|
||||||
|
|
||||||
Params4 = [{<<"_like_for">>,<<"t/a/">>}],
|
Params4 = [{<<"_like_for">>,<<"t/a/">>}, {<<"enable_paging">>, true}],
|
||||||
{ok, #{code := 0, data := Rules5}} = emqx_rule_engine_api:list_rules(#{}, Params4),
|
{ok, #{code := 0, data := Rules5}} = emqx_rule_engine_api:list_rules(#{}, Params4),
|
||||||
?assertEqual(length(Rules1), length(Rules5)),
|
?assertEqual(length(Rules1), length(Rules5)),
|
||||||
{ok, #{code := 0}} = emqx_rule_engine_api:update_rule(#{id => RuleID},
|
{ok, #{code := 0}} = emqx_rule_engine_api:update_rule(#{id => RuleID},
|
||||||
|
@ -559,7 +559,7 @@ t_list_rule_api(_Config) ->
|
||||||
?assert(lists:all(fun(#{id := ID}) -> ID =/= RuleID end, Rules6)),
|
?assert(lists:all(fun(#{id := ID}) -> ID =/= RuleID end, Rules6)),
|
||||||
?assertEqual(1, length(Rules1) - length(Rules6)),
|
?assertEqual(1, length(Rules1) - length(Rules6)),
|
||||||
|
|
||||||
Params5 = [{<<"_match_for">>,<<"t/+/+">>}],
|
Params5 = [{<<"_match_for">>,<<"t/+/+">>}, {<<"enable_paging">>, true}],
|
||||||
{ok, #{code := 0, data := Rules7}} = emqx_rule_engine_api:list_rules(#{}, Params5),
|
{ok, #{code := 0, data := Rules7}} = emqx_rule_engine_api:list_rules(#{}, Params5),
|
||||||
?assertEqual(length(Rules1), length(Rules7)),
|
?assertEqual(length(Rules1), length(Rules7)),
|
||||||
{ok, #{code := 0}} = emqx_rule_engine_api:update_rule(#{id => RuleID},
|
{ok, #{code := 0}} = emqx_rule_engine_api:update_rule(#{id => RuleID},
|
||||||
|
@ -568,7 +568,7 @@ t_list_rule_api(_Config) ->
|
||||||
?assert(lists:all(fun(#{id := ID}) -> ID =/= RuleID end, Rules8)),
|
?assert(lists:all(fun(#{id := ID}) -> ID =/= RuleID end, Rules8)),
|
||||||
?assertEqual(1, length(Rules1) - length(Rules8)),
|
?assertEqual(1, length(Rules1) - length(Rules8)),
|
||||||
|
|
||||||
Params6 = [{<<"_like_description">>,<<"rule">>}],
|
Params6 = [{<<"_like_description">>,<<"rule">>}, {<<"enable_paging">>, true}],
|
||||||
{ok, #{code := 0, data := Rules9}} = emqx_rule_engine_api:list_rules(#{}, Params6),
|
{ok, #{code := 0, data := Rules9}} = emqx_rule_engine_api:list_rules(#{}, Params6),
|
||||||
?assertEqual(length(Rules1), length(Rules9)),
|
?assertEqual(length(Rules1), length(Rules9)),
|
||||||
{ok, #{code := 0}} = emqx_rule_engine_api:update_rule(#{id => RuleID},
|
{ok, #{code := 0}} = emqx_rule_engine_api:update_rule(#{id => RuleID},
|
||||||
|
@ -582,7 +582,6 @@ t_list_rule_api(_Config) ->
|
||||||
end, AddIds),
|
end, AddIds),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|
||||||
t_list_actions_api(_Config) ->
|
t_list_actions_api(_Config) ->
|
||||||
{ok, #{code := 0, data := Actions}} = emqx_rule_engine_api:list_actions(#{}, []),
|
{ok, #{code := 0, data := Actions}} = emqx_rule_engine_api:list_actions(#{}, []),
|
||||||
%ct:pal("RList : ~p", [Actions]),
|
%ct:pal("RList : ~p", [Actions]),
|
||||||
|
|
Loading…
Reference in New Issue