rule_engine.hrl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_output_func() :: republish | console.
  27. -type builtin_output_module() :: emqx_rule_outputs.
  28. -type bridge_channel_id() :: binary().
  29. -type output_fun_args() :: map().
  30. -type output() :: #{
  31. mod := builtin_output_module() | module(),
  32. func := builtin_output_func() | atom(),
  33. args => output_fun_args()
  34. } | bridge_channel_id().
  35. -type rule() ::
  36. #{ id := rule_id()
  37. , name := binary()
  38. , sql := binary()
  39. , outputs := [output()]
  40. , enable := boolean()
  41. , description => binary()
  42. , created_at := integer() %% epoch in millisecond precision
  43. , from := list(topic())
  44. , is_foreach := boolean()
  45. , fields := list()
  46. , doeach := term()
  47. , incase := term()
  48. , conditions := tuple()
  49. }.
  50. %% Arithmetic operators
  51. -define(is_arith(Op), (Op =:= '+' orelse
  52. Op =:= '-' orelse
  53. Op =:= '*' orelse
  54. Op =:= '/' orelse
  55. Op =:= 'div')).
  56. %% Compare operators
  57. -define(is_comp(Op), (Op =:= '=' orelse
  58. Op =:= '=~' orelse
  59. Op =:= '>' orelse
  60. Op =:= '<' orelse
  61. Op =:= '<=' orelse
  62. Op =:= '>=' orelse
  63. Op =:= '<>' orelse
  64. Op =:= '!=')).
  65. %% Logical operators
  66. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  67. -define(RAISE(_EXP_, _ERROR_),
  68. ?RAISE(_EXP_, _ = do_nothing, _ERROR_)).
  69. -define(RAISE(_EXP_, _EXP_ON_FAIL_, _ERROR_),
  70. fun() ->
  71. try (_EXP_)
  72. catch _EXCLASS_:_EXCPTION_:_ST_ ->
  73. _EXP_ON_FAIL_,
  74. throw(_ERROR_)
  75. end
  76. end()).
  77. %% Tables
  78. -define(RULE_TAB, emqx_rule_engine).