emqx_rule_engine_cli.erl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022-2024 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. -module(emqx_rule_engine_cli).
  17. %% API:
  18. -export([load/0, unload/0]).
  19. -export([cmd/1]).
  20. %%================================================================================
  21. %% API functions
  22. %%================================================================================
  23. load() ->
  24. ok = emqx_ctl:register_command(rules, {?MODULE, cmd}, []).
  25. unload() ->
  26. ok = emqx_ctl:unregister_command(rules).
  27. %%================================================================================
  28. %% Internal exports
  29. %%================================================================================
  30. cmd(["list"]) ->
  31. lists:foreach(
  32. fun pretty_print_rule_summary/1,
  33. emqx_rule_engine:get_rules_ordered_by_ts()
  34. );
  35. cmd(["show", ID]) ->
  36. pretty_print_rule(ID);
  37. cmd(_) ->
  38. emqx_ctl:usage(
  39. [
  40. {"rules list", "List rules"},
  41. {"rules show <RuleID>", "Show a rule"}
  42. ]
  43. ).
  44. %%================================================================================
  45. %% Internal functions
  46. %%================================================================================
  47. pretty_print_rule_summary(#{id := Id, name := Name, enable := Enable, description := Desc}) ->
  48. emqx_ctl:print("Rule{id=~ts, name=~ts, enabled=~ts, descr=~ts}\n", [
  49. Id, Name, Enable, Desc
  50. ]).
  51. %% erlfmt-ignore
  52. pretty_print_rule(ID) ->
  53. case emqx_rule_engine:get_rule(list_to_binary(ID)) of
  54. {ok, #{id := Id, name := Name, description := Descr, enable := Enable,
  55. sql := SQL, created_at := CreatedAt, updated_at := UpdatedAt,
  56. actions := Actions}} ->
  57. emqx_ctl:print(
  58. "Id:\n ~ts\n"
  59. "Name:\n ~ts\n"
  60. "Description:\n ~ts\n"
  61. "Enabled:\n ~ts\n"
  62. "SQL:\n ~ts\n"
  63. "Created at:\n ~ts\n"
  64. "Updated at:\n ~ts\n"
  65. "Actions:\n ~s\n"
  66. ,[Id, Name, left_pad(Descr), Enable, left_pad(SQL),
  67. emqx_utils_calendar:epoch_to_rfc3339(CreatedAt, millisecond),
  68. emqx_utils_calendar:epoch_to_rfc3339(UpdatedAt, millisecond),
  69. [left_pad(format_action(A)) || A <- Actions]
  70. ]
  71. );
  72. _ ->
  73. ok
  74. end.
  75. %% erlfmt-ignore
  76. format_action(#{mod := Mod, func := Func, args := Args}) ->
  77. Name = emqx_rule_engine_api:printable_function_name(Mod, Func),
  78. io_lib:format("- Name: ~s\n"
  79. " Type: function\n"
  80. " Args: ~p\n"
  81. ,[Name, maps:without([preprocessed_tmpl], Args)]
  82. );
  83. format_action(BridgeChannelId) when is_binary(BridgeChannelId) ->
  84. io_lib:format("- Name: ~s\n"
  85. " Type: data-bridge\n"
  86. ,[BridgeChannelId]
  87. ).
  88. left_pad(Str) ->
  89. re:replace(Str, "\n", "\n ", [global]).