refactor(rule): add name field to request body of POST /rules

This commit is contained in:
Shawn 2021-12-18 05:54:22 +08:00
parent 673a545aa2
commit 494c08f849
5 changed files with 26 additions and 13 deletions

View File

@ -44,6 +44,7 @@
-type rule() :: -type rule() ::
#{ id := rule_id() #{ id := rule_id()
, name := binary()
, sql := binary() , sql := binary()
, outputs := [output()] , outputs := [output()]
, enabled := boolean() , enabled := boolean()

View File

@ -38,14 +38,11 @@ roots() ->
]. ].
fields("rule_creation") -> fields("rule_creation") ->
[ {"id", sc(binary(), emqx_rule_engine_schema:fields("rules");
#{ desc => "The Id of the rule", nullable => false
, example => "my_rule_id"
})}
] ++ emqx_rule_engine_schema:fields("rules");
fields("rule_info") -> fields("rule_info") ->
[ {"metrics", sc(ref("metrics"), #{desc => "The metrics of the rule"})} [ rule_id()
, {"metrics", sc(ref("metrics"), #{desc => "The metrics of the rule"})}
, {"node_metrics", sc(ref("node_metrics"), #{desc => "The metrics of the rule"})} , {"node_metrics", sc(ref("node_metrics"), #{desc => "The metrics of the rule"})}
, {"from", sc(hoconsc:array(binary()), , {"from", sc(hoconsc:array(binary()),
#{desc => "The topics of the rule", example => "t/#"})} #{desc => "The topics of the rule", example => "t/#"})}
@ -182,5 +179,11 @@ qos() ->
{"qos", sc(hoconsc:union([typerefl:integer(0), typerefl:integer(1), typerefl:integer(2)]), {"qos", sc(hoconsc:union([typerefl:integer(0), typerefl:integer(1), typerefl:integer(2)]),
#{desc => "The Message QoS"})}. #{desc => "The Message QoS"})}.
rule_id() ->
{"id", sc(binary(),
#{ desc => "The Id of the rule", nullable => false
, example => "293fb66f"
})}.
sc(Type, Meta) -> hoconsc:mk(Type, Meta). sc(Type, Meta) -> hoconsc:mk(Type, Meta).
ref(Field) -> hoconsc:ref(?MODULE, Field). ref(Field) -> hoconsc:ref(?MODULE, Field).

View File

@ -221,6 +221,7 @@ do_create_rule(Params = #{id := RuleId, sql := Sql, outputs := Outputs}) ->
{ok, Select} -> {ok, Select} ->
Rule = #{ Rule = #{
id => RuleId, id => RuleId,
name => maps:get(name, Params, <<"">>),
created_at => erlang:system_time(millisecond), created_at => erlang:system_time(millisecond),
enabled => maps:get(enabled, Params, true), enabled => maps:get(enabled, Params, true),
sql => Sql, sql => Sql,

View File

@ -59,9 +59,6 @@ error_schema(Code, Message) ->
rule_creation_schema() -> rule_creation_schema() ->
ref(emqx_rule_api_schema, "rule_creation"). ref(emqx_rule_api_schema, "rule_creation").
rule_update_schema() ->
ref(emqx_rule_engine_schema, "rules").
rule_test_schema() -> rule_test_schema() ->
ref(emqx_rule_api_schema, "rule_test"). ref(emqx_rule_api_schema, "rule_test").
@ -120,7 +117,7 @@ schema("/rules/:id") ->
description => <<"Update a rule by given Id to all nodes in the cluster">>, description => <<"Update a rule by given Id to all nodes in the cluster">>,
summary => <<"Update a Rule">>, summary => <<"Update a Rule">>,
parameters => param_path_id(), parameters => param_path_id(),
requestBody => rule_update_schema(), requestBody => rule_creation_schema(),
responses => #{ responses => #{
400 => error_schema('BAD_ARGS', "Invalid Parameters"), 400 => error_schema('BAD_ARGS', "Invalid Parameters"),
200 => rule_info_schema() 200 => rule_info_schema()
@ -167,7 +164,8 @@ param_path_id() ->
Records = emqx_rule_engine:get_rules_ordered_by_ts(), Records = emqx_rule_engine:get_rules_ordered_by_ts(),
{200, format_rule_resp(Records)}; {200, format_rule_resp(Records)};
'/rules'(post, #{body := #{<<"id">> := Id} = Params}) -> '/rules'(post, #{body := Params}) ->
Id = maps:get(<<"id">>, Params, list_to_binary(emqx_misc:gen_id(8))),
ConfPath = emqx_rule_engine:config_key_path() ++ [Id], ConfPath = emqx_rule_engine:config_key_path() ++ [Id],
case emqx_rule_engine:get_rule(Id) of case emqx_rule_engine:get_rule(Id) of
{ok, _Rule} -> {ok, _Rule} ->
@ -230,7 +228,8 @@ err_msg(Msg) ->
format_rule_resp(Rules) when is_list(Rules) -> format_rule_resp(Rules) when is_list(Rules) ->
[format_rule_resp(R) || R <- Rules]; [format_rule_resp(R) || R <- Rules];
format_rule_resp(#{ id := Id, created_at := CreatedAt, format_rule_resp(#{ id := Id, name := Name,
created_at := CreatedAt,
from := Topics, from := Topics,
outputs := Output, outputs := Output,
sql := SQL, sql := SQL,
@ -238,6 +237,7 @@ format_rule_resp(#{ id := Id, created_at := CreatedAt,
description := Descr}) -> description := Descr}) ->
NodeMetrics = get_rule_metrics(Id), NodeMetrics = get_rule_metrics(Id),
#{id => Id, #{id => Id,
name => Name,
from => Topics, from => Topics,
outputs => format_output(Output), outputs => format_output(Output),
sql => SQL, sql => SQL,

View File

@ -39,7 +39,8 @@ fields("rule_engine") ->
]; ];
fields("rules") -> fields("rules") ->
[ {"sql", sc(binary(), [ rule_name()
, {"sql", sc(binary(),
#{ desc => """ #{ desc => """
SQL query to transform the messages.<br> SQL query to transform the messages.<br>
Example: <code>SELECT * FROM \"test/topic\" WHERE payload.x = 1</code><br> Example: <code>SELECT * FROM \"test/topic\" WHERE payload.x = 1</code><br>
@ -177,6 +178,13 @@ of the rule, then the string \"undefined\" is used.
})} })}
]. ].
rule_name() ->
{"name", sc(binary(),
#{ desc => "The name of the rule"
, default => ""
, example => "foo"
})}.
outputs() -> outputs() ->
[ binary() [ binary()
, ref("builtin_output_republish") , ref("builtin_output_republish")