rule_engine.hrl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2023 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 maybe(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. -type rule() ::
  38. #{
  39. id := rule_id(),
  40. name := binary(),
  41. sql := binary(),
  42. actions := [action()],
  43. enable := boolean(),
  44. description => binary(),
  45. %% epoch in millisecond precision
  46. created_at := integer(),
  47. %% epoch in millisecond precision
  48. updated_at := integer(),
  49. from := list(topic()),
  50. is_foreach := boolean(),
  51. fields := list(),
  52. doeach := term(),
  53. incase := term(),
  54. conditions := tuple()
  55. }.
  56. %% Arithmetic operators
  57. -define(is_arith(Op),
  58. (Op =:= '+' orelse
  59. Op =:= '-' orelse
  60. Op =:= '*' orelse
  61. Op =:= '/' orelse
  62. Op =:= 'div' orelse
  63. Op =:= 'mod')
  64. ).
  65. %% Compare operators
  66. -define(is_comp(Op),
  67. (Op =:= '=' orelse
  68. Op =:= '=~' orelse
  69. Op =:= '>' orelse
  70. Op =:= '<' orelse
  71. Op =:= '<=' orelse
  72. Op =:= '>=' orelse
  73. Op =:= '<>' orelse
  74. Op =:= '!=')
  75. ).
  76. %% Logical operators
  77. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  78. -define(RAISE(EXP, ERROR),
  79. ?RAISE(EXP, _ = do_nothing, ERROR)
  80. ).
  81. -define(RAISE_BAD_SQL(Detail), throw(Detail)).
  82. -define(RAISE(EXP, EXP_ON_FAIL, ERROR),
  83. fun() ->
  84. try
  85. (EXP)
  86. catch
  87. EXCLASS:EXCPTION:ST ->
  88. EXP_ON_FAIL,
  89. throw(ERROR)
  90. end
  91. end()
  92. ).
  93. %% Tables
  94. -define(RULE_TAB, emqx_rule_engine).
  95. -define(RULE_TOPIC_INDEX, emqx_rule_engine_topic_index).
  96. %% Allowed sql function provider modules
  97. -define(DEFAULT_SQL_FUNC_PROVIDER, emqx_rule_funcs).
  98. -define(IS_VALID_SQL_FUNC_PROVIDER_MODULE_NAME(Name),
  99. (case Name of
  100. <<"emqx_rule_funcs", _/binary>> ->
  101. true;
  102. <<"EmqxRuleFuncs", _/binary>> ->
  103. true;
  104. _ ->
  105. false
  106. end)
  107. ).
  108. -define(KEY_PATH, [rule_engine, rules]).
  109. -define(RULE_PATH(RULE), [rule_engine, rules, RULE]).