From 9d4f2916c2445e1a015a2dc6ba81f576353173ef Mon Sep 17 00:00:00 2001 From: lafirest Date: Thu, 11 Nov 2021 23:49:54 +0800 Subject: [PATCH] refactor(emqx_st_statistics): optimize the code directory structure (#6128) --- .../include/emqx_st_statistics.hrl | 0 .../src/emqx_plugin_libs.app.src | 2 +- .../emqx_st_statistics/emqx_st_statistics.erl | 26 +--------- .../emqx_st_statistics_api.erl | 2 +- lib-ce/emqx_dashboard/src/emqx_dashboard.erl | 18 +++---- .../test/emqx_dashboard_SUITE.erl | 1 + .../src/emqx_mod_st_statistics.erl | 49 +++++++++++++++++++ .../test/emqx_st_statistics_SUITE.erl | 9 ---- .../test/emqx_st_statistics_api_SUITE.erl | 11 +++-- 9 files changed, 68 insertions(+), 50 deletions(-) rename {lib-ce/emqx_modules => apps/emqx_plugin_libs}/include/emqx_st_statistics.hrl (100%) rename lib-ce/emqx_modules/src/emqx_st_statistics/emqx_mod_st_statistics.erl => apps/emqx_plugin_libs/src/emqx_st_statistics/emqx_st_statistics.erl (95%) rename {lib-ce/emqx_modules => apps/emqx_plugin_libs}/src/emqx_st_statistics/emqx_st_statistics_api.erl (98%) create mode 100644 lib-ce/emqx_modules/src/emqx_mod_st_statistics.erl diff --git a/lib-ce/emqx_modules/include/emqx_st_statistics.hrl b/apps/emqx_plugin_libs/include/emqx_st_statistics.hrl similarity index 100% rename from lib-ce/emqx_modules/include/emqx_st_statistics.hrl rename to apps/emqx_plugin_libs/include/emqx_st_statistics.hrl diff --git a/apps/emqx_plugin_libs/src/emqx_plugin_libs.app.src b/apps/emqx_plugin_libs/src/emqx_plugin_libs.app.src index 82937d033..f72ffc229 100644 --- a/apps/emqx_plugin_libs/src/emqx_plugin_libs.app.src +++ b/apps/emqx_plugin_libs/src/emqx_plugin_libs.app.src @@ -1,6 +1,6 @@ {application, emqx_plugin_libs, [{description, "EMQ X Plugin utility libs"}, - {vsn, "4.3.1"}, + {vsn, "4.3.2"}, {modules, []}, {applications, [kernel,stdlib]}, {env, []} diff --git a/lib-ce/emqx_modules/src/emqx_st_statistics/emqx_mod_st_statistics.erl b/apps/emqx_plugin_libs/src/emqx_st_statistics/emqx_st_statistics.erl similarity index 95% rename from lib-ce/emqx_modules/src/emqx_st_statistics/emqx_mod_st_statistics.erl rename to apps/emqx_plugin_libs/src/emqx_st_statistics/emqx_st_statistics.erl index a66975767..ac1604c9b 100644 --- a/lib-ce/emqx_modules/src/emqx_st_statistics/emqx_mod_st_statistics.erl +++ b/apps/emqx_plugin_libs/src/emqx_st_statistics/emqx_st_statistics.erl @@ -14,9 +14,8 @@ %% limitations under the License. %%-------------------------------------------------------------------- --module(emqx_mod_st_statistics). +-module(emqx_st_statistics). --behaviour(emqx_gen_mod). -behaviour(gen_server). -include_lib("include/emqx.hrl"). @@ -29,12 +28,6 @@ , disable/0, clear_history/0 ]). -%% emqx_gen_mod callbacks --export([ load/1 - , unload/1 - , description/0 - ]). - %% gen_server callbacks -export([ init/1 , handle_call/3 @@ -88,23 +81,6 @@ %% ets ordered_set is ascending by term order -%%-------------------------------------------------------------------- -%% Load/Unload -%%-------------------------------------------------------------------- - --spec(load(list()) -> ok). -load(Env) -> - emqx_mod_sup:start_child(?MODULE, worker, [Env]), - ok. - --spec(unload(list()) -> ok). -unload(_Env) -> - _ = emqx_mod_sup:stop_child(?MODULE), - ok. - -description() -> - "EMQ X Slow Topic Statistics Module". - %%-------------------------------------------------------------------- %%-------------------------------------------------------------------- %% APIs diff --git a/lib-ce/emqx_modules/src/emqx_st_statistics/emqx_st_statistics_api.erl b/apps/emqx_plugin_libs/src/emqx_st_statistics/emqx_st_statistics_api.erl similarity index 98% rename from lib-ce/emqx_modules/src/emqx_st_statistics/emqx_st_statistics_api.erl rename to apps/emqx_plugin_libs/src/emqx_st_statistics/emqx_st_statistics_api.erl index 328fddf28..ba13d70e2 100644 --- a/lib-ce/emqx_modules/src/emqx_st_statistics/emqx_st_statistics_api.erl +++ b/apps/emqx_plugin_libs/src/emqx_st_statistics/emqx_st_statistics_api.erl @@ -41,7 +41,7 @@ %%-------------------------------------------------------------------- clear_history(_Bindings, _Params) -> - ok = emqx_mod_st_statistics:clear_history(), + ok = emqx_st_statistics:clear_history(), return(ok). get_history(_Bindings, Params) -> diff --git a/lib-ce/emqx_dashboard/src/emqx_dashboard.erl b/lib-ce/emqx_dashboard/src/emqx_dashboard.erl index 0390339d3..40a978a37 100644 --- a/lib-ce/emqx_dashboard/src/emqx_dashboard.erl +++ b/lib-ce/emqx_dashboard/src/emqx_dashboard.erl @@ -42,17 +42,16 @@ start_listeners() -> lists:foreach(fun(Listener) -> start_listener(Listener) end, listeners()). %% Start HTTP Listener -start_listener({Proto, Port, Options}) when Proto == http -> +start_listener({Proto, Port, Options}) -> Dispatch = [{"/", cowboy_static, {priv_file, emqx_dashboard, "www/index.html"}}, {"/static/[...]", cowboy_static, {priv_dir, emqx_dashboard, "www/static"}}, {"/api/v4/[...]", minirest, http_handlers()}], - minirest:start_http(listener_name(Proto), ranch_opts(Port, Options), Dispatch); - -start_listener({Proto, Port, Options}) when Proto == https -> - Dispatch = [{"/", cowboy_static, {priv_file, emqx_dashboard, "www/index.html"}}, - {"/static/[...]", cowboy_static, {priv_dir, emqx_dashboard, "www/static"}}, - {"/api/v4/[...]", minirest, http_handlers()}], - minirest:start_https(listener_name(Proto), ranch_opts(Port, Options), Dispatch). + Server = listener_name(Proto), + RanchOpts = ranch_opts(Port, Options), + case Proto of + http -> minirest:start_http(Server, RanchOpts, Dispatch); + https -> minirest:start_https(Server, RanchOpts, Dispatch) + end. ranch_opts(Port, Options0) -> NumAcceptors = get_value(num_acceptors, Options0, 4), @@ -89,7 +88,7 @@ listener_name(Proto) -> http_handlers() -> Plugins = lists:map(fun(Plugin) -> Plugin#plugin.name end, emqx_plugins:list()), [{"/api/v4/", - minirest:handler(#{apps => Plugins ++ [emqx_modules], + minirest:handler(#{apps => Plugins ++ [emqx_modules, emqx_plugin_libs], filter => fun ?MODULE:filter/1}), [{authorization, fun ?MODULE:is_authorized/1}]}]. @@ -116,6 +115,7 @@ is_authorized(_Path, Req) -> _ -> false end. +filter(#{app := emqx_plugin_libs}) -> true; filter(#{app := emqx_modules}) -> true; filter(#{app := App}) -> case emqx_plugins:find_plugin(App) of diff --git a/lib-ce/emqx_dashboard/test/emqx_dashboard_SUITE.erl b/lib-ce/emqx_dashboard/test/emqx_dashboard_SUITE.erl index ef2e747fa..550f1e9de 100644 --- a/lib-ce/emqx_dashboard/test/emqx_dashboard_SUITE.erl +++ b/lib-ce/emqx_dashboard/test/emqx_dashboard_SUITE.erl @@ -54,6 +54,7 @@ groups() -> ]. init_per_suite(Config) -> + application:load(emqx_plugin_libs), emqx_ct_helpers:start_apps([emqx_modules, emqx_management, emqx_dashboard]), Config. diff --git a/lib-ce/emqx_modules/src/emqx_mod_st_statistics.erl b/lib-ce/emqx_modules/src/emqx_mod_st_statistics.erl new file mode 100644 index 000000000..d6796122c --- /dev/null +++ b/lib-ce/emqx_modules/src/emqx_mod_st_statistics.erl @@ -0,0 +1,49 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + +-module(emqx_mod_st_statistics). + +-behaviour(emqx_gen_mod). + +-include_lib("include/emqx.hrl"). +-include_lib("include/logger.hrl"). + +-logger_header("[SLOW TOPICS]"). + +%% emqx_gen_mod callbacks +-export([ load/1 + , unload/1 + , description/0 + ]). + +-define(LIB, emqx_st_statistics). + +%%-------------------------------------------------------------------- +%% Load/Unload +%%-------------------------------------------------------------------- + +-spec(load(list()) -> ok). +load(Env) -> + emqx_mod_sup:start_child(?LIB, worker, [Env]), + ok. + +-spec(unload(list()) -> ok). +unload(_Env) -> + _ = emqx_mod_sup:stop_child(?LIB), + ok. + +description() -> + "EMQ X Slow Topic Statistics Module". diff --git a/lib-ce/emqx_modules/test/emqx_st_statistics_SUITE.erl b/lib-ce/emqx_modules/test/emqx_st_statistics_SUITE.erl index 36da813f2..8df07f8b8 100644 --- a/lib-ce/emqx_modules/test/emqx_st_statistics_SUITE.erl +++ b/lib-ce/emqx_modules/test/emqx_st_statistics_SUITE.erl @@ -65,15 +65,6 @@ t_log_and_pub(_) -> end, lists:seq(1, 10)), - timer:sleep(100), - - case ets:info(?LOG_TAB, size) of - 5 -> - ok; - _ -> - ?assert(ets:info(?TOPK_TAB, size) =/= 0) - end, - timer:sleep(2400), ?assert(ets:info(?LOG_TAB, size) =:= 0), diff --git a/lib-ce/emqx_modules/test/emqx_st_statistics_api_SUITE.erl b/lib-ce/emqx_modules/test/emqx_st_statistics_api_SUITE.erl index 8255cdd5f..3cf4dafd8 100644 --- a/lib-ce/emqx_modules/test/emqx_st_statistics_api_SUITE.erl +++ b/lib-ce/emqx_modules/test/emqx_st_statistics_api_SUITE.erl @@ -24,11 +24,11 @@ -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx_mqtt.hrl"). -include_lib("emqx_management/include/emqx_mgmt.hrl"). --include_lib("emqx_modules/include/emqx_st_statistics.hrl"). +-include_lib("emqx_plugin_libs/include/emqx_st_statistics.hrl"). -define(CONTENT_TYPE, "application/x-www-form-urlencoded"). --define(HOST, "http://127.0.0.1:8081/"). +-define(HOST, "http://127.0.0.1:18083/"). -define(API_VERSION, "v4"). @@ -38,8 +38,9 @@ all() -> emqx_ct:all(?MODULE). init_per_suite(Config) -> - application:load(emqx_modules), - emqx_ct_helpers:start_apps([emqx_management]), + emqx_ct_helpers:boot_modules(all), + application:load(emqx_plugin_libs), + emqx_ct_helpers:start_apps([emqx_modules, emqx_management, emqx_dashboard]), Config. end_per_suite(Config) -> @@ -55,7 +56,7 @@ end_per_testcase(_, Config) -> Config. get(Key, ResponseBody) -> - maps:get(Key, jiffy:decode(list_to_binary(ResponseBody), [return_maps])). + maps:get(Key, jiffy:decode(list_to_binary(ResponseBody), [return_maps])). lookup_alarm(Name, [#{<<"name">> := Name} | _More]) -> true;