rule_engine.hrl 5.1 KB

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