rule_engine.hrl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020 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. -type(maybe(T) :: T | undefined).
  18. -type(rule_id() :: binary()).
  19. -type(rule_name() :: binary()).
  20. -type(resource_id() :: binary()).
  21. -type(action_instance_id() :: binary()).
  22. -type(action_name() :: atom()).
  23. -type(resource_type_name() :: atom()).
  24. -type(category() :: data_persist| data_forward | offline_msgs | debug | other).
  25. -type(descr() :: #{en := binary(), zh => binary()}).
  26. -type(mf() :: {Module::atom(), Fun::atom()}).
  27. -type(hook() :: atom() | 'any').
  28. -type(topic() :: binary()).
  29. -type(resource_status() :: #{ alive := boolean()
  30. , atom() => binary() | atom() | list(binary()|atom())
  31. }).
  32. -define(descr, #{en => <<>>, zh => <<>>}).
  33. -record(action,
  34. { name :: action_name()
  35. , category :: category()
  36. , for :: hook()
  37. , app :: atom()
  38. , types = [] :: list(resource_type_name())
  39. , module :: module()
  40. , on_create :: mf()
  41. , on_destroy :: maybe(mf())
  42. , hidden = false :: boolean()
  43. , params_spec :: #{atom() => term()} %% params specs
  44. , title = ?descr :: descr()
  45. , description = ?descr :: descr()
  46. }).
  47. -record(action_instance,
  48. { id :: action_instance_id()
  49. , name :: action_name()
  50. , fallbacks :: list(#action_instance{})
  51. , args :: #{binary() => term()} %% the args got from API for initializing action_instance
  52. }).
  53. -record(rule,
  54. { id :: rule_id()
  55. , for :: list(topic())
  56. , rawsql :: binary()
  57. , is_foreach :: boolean()
  58. , fields :: list()
  59. , doeach :: term()
  60. , incase :: list()
  61. , conditions :: tuple()
  62. , on_action_failed :: continue | stop
  63. , actions :: list(#action_instance{})
  64. , enabled :: boolean()
  65. , description :: binary()
  66. }).
  67. -record(resource,
  68. { id :: resource_id()
  69. , type :: resource_type_name()
  70. , config :: #{} %% the configs got from API for initializing resource
  71. , created_at :: integer() %% epoch in millisecond precision
  72. , description :: binary()
  73. }).
  74. -record(resource_type,
  75. { name :: resource_type_name()
  76. , provider :: atom()
  77. , params_spec :: #{atom() => term()} %% params specs
  78. , on_create :: mf()
  79. , on_status :: mf()
  80. , on_destroy :: mf()
  81. , title = ?descr :: descr()
  82. , description = ?descr :: descr()
  83. }).
  84. -record(rule_hooks,
  85. { hook :: atom()
  86. , rule_id :: rule_id()
  87. }).
  88. -record(resource_params,
  89. { id :: resource_id()
  90. , params :: #{} %% the params got after initializing the resource
  91. , status = #{is_alive => false} :: #{is_alive := boolean(), atom() => term()}
  92. }).
  93. -record(action_instance_params,
  94. { id :: action_instance_id()
  95. %% the params got after initializing the action
  96. , params :: #{}
  97. %% the Func/Bindings got after initializing the action
  98. , apply :: fun((Data::map(), Envs::map()) -> any())
  99. | #{mod := module(), bindings := #{atom() => term()}}
  100. }).
  101. %% Arithmetic operators
  102. -define(is_arith(Op), (Op =:= '+' orelse
  103. Op =:= '-' orelse
  104. Op =:= '*' orelse
  105. Op =:= '/' orelse
  106. Op =:= 'div')).
  107. %% Compare operators
  108. -define(is_comp(Op), (Op =:= '=' orelse
  109. Op =:= '=~' orelse
  110. Op =:= '>' orelse
  111. Op =:= '<' orelse
  112. Op =:= '<=' orelse
  113. Op =:= '>=' orelse
  114. Op =:= '<>' orelse
  115. Op =:= '!=')).
  116. %% Logical operators
  117. -define(is_logical(Op), (Op =:= 'and' orelse Op =:= 'or')).
  118. -define(RAISE(_EXP_, _ERROR_),
  119. ?RAISE(_EXP_, _ = do_nothing, _ERROR_)).
  120. -define(RAISE(_EXP_, _EXP_ON_FAIL_, _ERROR_),
  121. fun() ->
  122. try (_EXP_)
  123. catch _EXCLASS_:_EXCPTION_:_ST_ ->
  124. _EXP_ON_FAIL_,
  125. throw(_ERROR_)
  126. end
  127. end()).
  128. %% Tables
  129. -define(RULE_TAB, emqx_rule).
  130. -define(ACTION_TAB, emqx_rule_action).
  131. -define(ACTION_INST_PARAMS_TAB, emqx_action_instance_params).
  132. -define(RES_TAB, emqx_resource).
  133. -define(RES_PARAMS_TAB, emqx_resource_params).
  134. -define(RULE_HOOKS, emqx_rule_hooks).
  135. -define(RES_TYPE_TAB, emqx_resource_type).