rule_engine.hrl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2021 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. , sql := binary()
  38. , outputs := [output()]
  39. , enabled := boolean()
  40. , description => binary()
  41. , created_at := integer() %% epoch in millisecond precision
  42. , from := list(topic())
  43. , is_foreach := boolean()
  44. , fields := list()
  45. , doeach := term()
  46. , incase := term()
  47. , conditions := tuple()
  48. }.
  49. %% Arithmetic operators
  50. -define(is_arith(Op), (Op =:= '+' orelse
  51. Op =:= '-' orelse
  52. Op =:= '*' orelse
  53. Op =:= '/' orelse
  54. Op =:= 'div')).
  55. %% Compare operators
  56. -define(is_comp(Op), (Op =:= '=' orelse
  57. Op =:= '=~' orelse
  58. Op =:= '>' orelse
  59. Op =:= '<' orelse
  60. Op =:= '<=' orelse
  61. Op =:= '>=' orelse
  62. Op =:= '<>' orelse
  63. Op =:= '!=')).
  64. %% Logical operators
  65. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  66. -define(RAISE(_EXP_, _ERROR_),
  67. ?RAISE(_EXP_, _ = do_nothing, _ERROR_)).
  68. -define(RAISE(_EXP_, _EXP_ON_FAIL_, _ERROR_),
  69. fun() ->
  70. try (_EXP_)
  71. catch _EXCLASS_:_EXCPTION_:_ST_ ->
  72. _EXP_ON_FAIL_,
  73. throw(_ERROR_)
  74. end
  75. end()).
  76. %% Tables
  77. -define(RULE_TAB, emqx_rule_engine).