Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new sql-funcs random/0 and uuid_v4/0 #11113

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/emqx_rule_engine/src/emqx_rule_engine.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{vsn, "4.4.19"}, % strict semver, bump manually!
{modules, []},
{registered, [emqx_rule_engine_sup, emqx_rule_registry, emqx_rule_engine_jwt_sup]},
{applications, [kernel,stdlib,rulesql,getopt,jose]},
{applications, [kernel,stdlib,rulesql,getopt,jose,uuid]},
{mod, {emqx_rule_engine_app, []}},
{env, []},
{licenses, ["Apache-2.0"]},
Expand Down
21 changes: 21 additions & 0 deletions apps/emqx_rule_engine/src/emqx_rule_funcs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@
, mongo_date/2
]).

%% Random Funcs
-export([ random/0
, uuid_v4/0
, uuid_v4_no_hyphen/0
]).

%% Proc Dict Func
-export([ proc_dict_get/1
, proc_dict_put/2
Expand Down Expand Up @@ -887,6 +893,18 @@ term_encode(Term) ->
term_decode(Data) when is_binary(Data) ->
erlang:binary_to_term(Data).

%%------------------------------------------------------------------------------
%% Random Funcs
%%------------------------------------------------------------------------------
random() ->
rand:uniform().

uuid_v4() ->
uuid_str(uuid:get_v4(), binary_standard).

uuid_v4_no_hyphen() ->
uuid_str(uuid:get_v4(), binary_nodash).

%%------------------------------------------------------------------------------
%% Dict Funcs
%%------------------------------------------------------------------------------
Expand Down Expand Up @@ -1039,3 +1057,6 @@ function_literal(Fun, [FArg | Args]) when is_atom(Fun), is_list(Args) ->
end, WithFirstArg, Args) ++ ")";
function_literal(Fun, Args) ->
{invalid_func, {Fun, Args}}.

uuid_str(UUID, DisplyOpt) ->
uuid:uuid_to_string(UUID, DisplyOpt).
6 changes: 6 additions & 0 deletions changes/v4.4.19-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@

Previously, this interface had a `connections` field, which represented the number of active connections on the current node that had not expired. This means that even if the MQTT connection has been disconnected, as long as the client has a persistent session, it would still be counted in the `connections` field. The newly added `live_connections` field specifically counts the number of clients with MQTT connections that have not been disconnected.

- Added 3 random SQL functions to the rule engine [#11113](https://github.com/emqx/emqx/pull/11113).

- random(): Generates a random number between 0 and 1 (0.0 =< X < 1.0).
- uuid_v4(): Generates a random UUID (version 4) string.
- uuid_v4_no_hyphen(): Generates a random UUID (version 4) string without hyphens.

## Bug fixes

- Fixed an issue where the rule engine was unable to access variables exported by `FOREACH` in the `DO` clause [#10620](https://github.com/emqx/emqx/pull/10620).
Expand Down
6 changes: 6 additions & 0 deletions changes/v4.4.19-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@

此前该接口中有一个 `connections` 字段,它代表当前节点上会话未过期的连接数量。这意味着即使 MQTT 连接已经断开,只要客户端保持了会话,它仍然会被统计在 `connections` 中。新增的 `live_connections` 字段则仅仅统计 MQTT 连接未断开的客户端数量。

- 规则引擎新增了三个随机函数 [#11113](https://github.com/emqx/emqx/pull/11113)。

- random():生成 0 到 1 之间的随机数 (0.0 =< X < 1.0)。
- uuid_v4():生成随机的 UUID (version4) 字符串。
- uuid_v4_no_hyphen():生成随机的不带连词符的 UUID (version4) 字符串。

## 修复

- 修复规则引擎无法在 `DO` 子句中访问 `FOREACH` 导出的变量的问题 [#10620](https://github.com/emqx/emqx/pull/10620)。
Expand Down
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
, {mysql, {git, "https://github.com/emqx/mysql-otp", {tag, "1.7.2"}}}
, {epgsql, {git, "https://github.com/emqx/epgsql.git", {tag, "4.6.0"}}}
, {grpc, {git, "https://github.com/emqx/grpc-erl", {tag, "0.6.7"}}}
, {uuid, {git, "https://github.com/okeuday/uuid.git", {tag, "v2.0.6"}}}
]}.

{xref_ignores,
Expand Down