Skip to content

Commit

Permalink
fix(rules): ensure create_at unchanged after rule is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
terry-xiaoyu committed Mar 15, 2022
1 parent 6b4e438 commit 869dc1a
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions apps/emqx_rule_engine/src/emqx_rule_engine.erl
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,19 @@ load_rules() ->
-spec create_rule(map()) -> {ok, rule()} | {error, term()}.
create_rule(Params = #{id := RuleId}) when is_binary(RuleId) ->
case get_rule(RuleId) of
not_found -> do_create_rule(Params);
{ok, _} -> {error, {already_exists, RuleId}}
not_found -> parse_and_insert(Params, now_ms());
{ok, _} -> {error, already_exists}
end.

-spec update_rule(map()) -> {ok, rule()} | {error, term()}.
update_rule(Params = #{id := RuleId}) when is_binary(RuleId) ->
ok = delete_rule(RuleId),
do_create_rule(Params).
case get_rule(RuleId) of
not_found ->
{error, not_found};
{ok, #{created_at := CreatedAt}} ->
ok = delete_rule(RuleId),
parse_and_insert(Params, CreatedAt)
end.

-spec(delete_rule(RuleId :: rule_id()) -> ok).
delete_rule(RuleId) when is_binary(RuleId) ->
Expand Down Expand Up @@ -232,13 +237,14 @@ code_change(_OldVsn, State, _Extra) ->
%% Internal Functions
%%------------------------------------------------------------------------------

do_create_rule(Params = #{id := RuleId, sql := Sql, outputs := Outputs}) ->
parse_and_insert(Params = #{id := RuleId, sql := Sql, outputs := Outputs}, CreatedAt) ->
case emqx_rule_sqlparser:parse(Sql) of
{ok, Select} ->
Rule = #{
id => RuleId,
name => maps:get(name, Params, <<"">>),
created_at => erlang:system_time(millisecond),
created_at => CreatedAt,
updated_at => now_ms(),
enable => maps:get(enable, Params, true),
sql => Sql,
outputs => parse_outputs(Outputs),
Expand Down Expand Up @@ -287,5 +293,8 @@ get_all_records(Tab) ->
maps_foreach(Fun, Map) ->
lists:foreach(Fun, maps:to_list(Map)).

now_ms() ->
erlang:system_time(millisecond).

bin(A) when is_atom(A) -> atom_to_binary(A, utf8);
bin(B) when is_binary(B) -> B.

0 comments on commit 869dc1a

Please sign in to comment.