refactor(rule): add name field to request body of POST /rules
This commit is contained in:
parent
673a545aa2
commit
494c08f849
|
@ -44,6 +44,7 @@
|
|||
|
||||
-type rule() ::
|
||||
#{ id := rule_id()
|
||||
, name := binary()
|
||||
, sql := binary()
|
||||
, outputs := [output()]
|
||||
, enabled := boolean()
|
||||
|
|
|
@ -38,14 +38,11 @@ roots() ->
|
|||
].
|
||||
|
||||
fields("rule_creation") ->
|
||||
[ {"id", sc(binary(),
|
||||
#{ desc => "The Id of the rule", nullable => false
|
||||
, example => "my_rule_id"
|
||||
})}
|
||||
] ++ emqx_rule_engine_schema:fields("rules");
|
||||
emqx_rule_engine_schema:fields("rules");
|
||||
|
||||
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"})}
|
||||
, {"from", sc(hoconsc:array(binary()),
|
||||
#{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)]),
|
||||
#{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).
|
||||
ref(Field) -> hoconsc:ref(?MODULE, Field).
|
||||
|
|
|
@ -221,6 +221,7 @@ do_create_rule(Params = #{id := RuleId, sql := Sql, outputs := Outputs}) ->
|
|||
{ok, Select} ->
|
||||
Rule = #{
|
||||
id => RuleId,
|
||||
name => maps:get(name, Params, <<"">>),
|
||||
created_at => erlang:system_time(millisecond),
|
||||
enabled => maps:get(enabled, Params, true),
|
||||
sql => Sql,
|
||||
|
|
|
@ -59,9 +59,6 @@ error_schema(Code, Message) ->
|
|||
rule_creation_schema() ->
|
||||
ref(emqx_rule_api_schema, "rule_creation").
|
||||
|
||||
rule_update_schema() ->
|
||||
ref(emqx_rule_engine_schema, "rules").
|
||||
|
||||
rule_test_schema() ->
|
||||
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">>,
|
||||
summary => <<"Update a Rule">>,
|
||||
parameters => param_path_id(),
|
||||
requestBody => rule_update_schema(),
|
||||
requestBody => rule_creation_schema(),
|
||||
responses => #{
|
||||
400 => error_schema('BAD_ARGS', "Invalid Parameters"),
|
||||
200 => rule_info_schema()
|
||||
|
@ -167,7 +164,8 @@ param_path_id() ->
|
|||
Records = emqx_rule_engine:get_rules_ordered_by_ts(),
|
||||
{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],
|
||||
case emqx_rule_engine:get_rule(Id) of
|
||||
{ok, _Rule} ->
|
||||
|
@ -230,7 +228,8 @@ err_msg(Msg) ->
|
|||
format_rule_resp(Rules) when is_list(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,
|
||||
outputs := Output,
|
||||
sql := SQL,
|
||||
|
@ -238,6 +237,7 @@ format_rule_resp(#{ id := Id, created_at := CreatedAt,
|
|||
description := Descr}) ->
|
||||
NodeMetrics = get_rule_metrics(Id),
|
||||
#{id => Id,
|
||||
name => Name,
|
||||
from => Topics,
|
||||
outputs => format_output(Output),
|
||||
sql => SQL,
|
||||
|
|
|
@ -39,7 +39,8 @@ fields("rule_engine") ->
|
|||
];
|
||||
|
||||
fields("rules") ->
|
||||
[ {"sql", sc(binary(),
|
||||
[ rule_name()
|
||||
, {"sql", sc(binary(),
|
||||
#{ desc => """
|
||||
SQL query to transform the messages.<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() ->
|
||||
[ binary()
|
||||
, ref("builtin_output_republish")
|
||||
|
|
Loading…
Reference in New Issue