rule_engine.hrl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 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')
  63. ).
  64. %% Compare operators
  65. -define(is_comp(Op),
  66. (Op =:= '=' orelse
  67. Op =:= '=~' orelse
  68. Op =:= '>' orelse
  69. Op =:= '<' orelse
  70. Op =:= '<=' orelse
  71. Op =:= '>=' orelse
  72. Op =:= '<>' orelse
  73. Op =:= '!=')
  74. ).
  75. %% Logical operators
  76. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  77. -define(RAISE(EXP, ERROR),
  78. ?RAISE(EXP, _ = do_nothing, ERROR)
  79. ).
  80. -define(RAISE(EXP, EXP_ON_FAIL, ERROR),
  81. fun() ->
  82. try
  83. (EXP)
  84. catch
  85. EXCLASS:EXCPTION:ST ->
  86. EXP_ON_FAIL,
  87. throw(ERROR)
  88. end
  89. end()
  90. ).
  91. %% Tables
  92. -define(RULE_TAB, emqx_rule_engine).