rule_engine.hrl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -define(APP, emqx_rule_engine).
  17. -define(KV_TAB, '@rule_engine_db').
  18. -type option(T) :: T | undefined.
  19. -type rule_id() :: binary().
  20. -type rule_name() :: binary().
  21. -type mf() :: {Module :: atom(), Fun :: atom()}.
  22. -type hook() :: atom() | 'any'.
  23. -type topic() :: binary().
  24. -type selected_data() :: map().
  25. -type envs() :: map().
  26. -type builtin_action_func() :: republish | console.
  27. -type builtin_action_module() :: emqx_rule_actions.
  28. -type bridge_channel_id() :: binary().
  29. -type action_fun_args() :: map().
  30. -type action() ::
  31. #{
  32. mod := builtin_action_module() | module(),
  33. func := builtin_action_func() | atom(),
  34. args => action_fun_args()
  35. }
  36. | bridge_channel_id()
  37. | {bridge_v2, emqx_bridge_v2:bridge_v2_type(), emqx_bridge_v2:bridge_v2_name()}
  38. | {bridge, emqx_utils_maps:config_key(), emqx_utils_maps:config_key(), bridge_channel_id()}.
  39. -type rule() ::
  40. #{
  41. id := rule_id(),
  42. name := binary(),
  43. sql := binary(),
  44. actions := [action()],
  45. enable := boolean(),
  46. description => binary(),
  47. %% epoch in millisecond precision
  48. created_at := integer(),
  49. %% epoch in millisecond precision
  50. updated_at := integer(),
  51. from := list(topic()),
  52. is_foreach := boolean(),
  53. fields := list(),
  54. doeach := term(),
  55. incase := term(),
  56. conditions := tuple()
  57. }.
  58. %% Arithmetic operators
  59. -define(is_arith(Op),
  60. (Op =:= '+' orelse
  61. Op =:= '-' orelse
  62. Op =:= '*' orelse
  63. Op =:= '/' orelse
  64. Op =:= 'div' orelse
  65. Op =:= 'mod')
  66. ).
  67. %% Compare operators
  68. -define(is_comp(Op),
  69. (Op =:= '=' orelse
  70. Op =:= '=~' orelse
  71. Op =:= '>' orelse
  72. Op =:= '<' orelse
  73. Op =:= '<=' orelse
  74. Op =:= '>=' orelse
  75. Op =:= '<>' orelse
  76. Op =:= '!=')
  77. ).
  78. %% Logical operators
  79. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  80. -define(RAISE(EXP, ERROR),
  81. ?RAISE(EXP, _ = do_nothing, ERROR)
  82. ).
  83. -define(RAISE_BAD_SQL(Detail), throw(Detail)).
  84. -define(RAISE(EXP, EXP_ON_FAIL, ERROR),
  85. fun() ->
  86. try
  87. (EXP)
  88. catch
  89. EXCLASS:EXCPTION:ST ->
  90. EXP_ON_FAIL,
  91. throw(ERROR)
  92. end
  93. end()
  94. ).
  95. %% Tables
  96. -define(RULE_TAB, emqx_rule_engine).
  97. -define(RULE_TOPIC_INDEX, emqx_rule_engine_topic_index).
  98. %% Allowed sql function provider modules
  99. -define(DEFAULT_SQL_FUNC_PROVIDER, emqx_rule_funcs).
  100. -define(IS_VALID_SQL_FUNC_PROVIDER_MODULE_NAME(Name),
  101. (case Name of
  102. <<"emqx_rule_funcs", _/binary>> ->
  103. true;
  104. <<"EmqxRuleFuncs", _/binary>> ->
  105. true;
  106. _ ->
  107. false
  108. end)
  109. ).
  110. -define(KEY_PATH, [rule_engine, rules]).
  111. -define(RULE_PATH(RULE), [rule_engine, rules, RULE]).
  112. -define(TAG, "RULE").