Skip to content

Commit

Permalink
Merge pull request #11113 from terry-xiaoyu/random_uuid_sql_funcs
Browse files Browse the repository at this point in the history
feat: add new sql-funcs random/0 and uuid_v4/0
  • Loading branch information
terry-xiaoyu committed Jun 21, 2023
2 parents 7400644 + 20cf06c commit c9b029a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
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

0 comments on commit c9b029a

Please sign in to comment.