rule_engine.hrl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. , updated_at := integer() %% epoch in millisecond precision
  44. , from := list(topic())
  45. , is_foreach := boolean()
  46. , fields := list()
  47. , doeach := term()
  48. , incase := term()
  49. , conditions := tuple()
  50. }.
  51. %% Arithmetic operators
  52. -define(is_arith(Op), (Op =:= '+' orelse
  53. Op =:= '-' orelse
  54. Op =:= '*' orelse
  55. Op =:= '/' orelse
  56. Op =:= 'div')).
  57. %% Compare operators
  58. -define(is_comp(Op), (Op =:= '=' orelse
  59. Op =:= '=~' orelse
  60. Op =:= '>' orelse
  61. Op =:= '<' orelse
  62. Op =:= '<=' orelse
  63. Op =:= '>=' orelse
  64. Op =:= '<>' orelse
  65. Op =:= '!=')).
  66. %% Logical operators
  67. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  68. -define(RAISE(_EXP_, _ERROR_),
  69. ?RAISE(_EXP_, _ = do_nothing, _ERROR_)).
  70. -define(RAISE(_EXP_, _EXP_ON_FAIL_, _ERROR_),
  71. fun() ->
  72. try (_EXP_)
  73. catch _EXCLASS_:_EXCPTION_:_ST_ ->
  74. _EXP_ON_FAIL_,
  75. throw(_ERROR_)
  76. end
  77. end()).
  78. %% Tables
  79. -define(RULE_TAB, emqx_rule_engine).