refactor(emqx_st_statistics): optimize the code directory structure (#6128)

This commit is contained in:
lafirest 2021-11-11 23:49:54 +08:00 committed by GitHub
parent 835ad52498
commit 9d4f2916c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 68 additions and 50 deletions

View File

@ -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, []}

View File

@ -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

View File

@ -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) ->

View File

@ -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

View File

@ -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.

View File

@ -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".

View File

@ -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),

View File

@ -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) ->