refactor(emqx_st_statistics): optimize the code directory structure (#6128)
This commit is contained in:
parent
835ad52498
commit
9d4f2916c2
|
@ -1,6 +1,6 @@
|
||||||
{application, emqx_plugin_libs,
|
{application, emqx_plugin_libs,
|
||||||
[{description, "EMQ X Plugin utility libs"},
|
[{description, "EMQ X Plugin utility libs"},
|
||||||
{vsn, "4.3.1"},
|
{vsn, "4.3.2"},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
{applications, [kernel,stdlib]},
|
{applications, [kernel,stdlib]},
|
||||||
{env, []}
|
{env, []}
|
||||||
|
|
|
@ -14,9 +14,8 @@
|
||||||
%% limitations under the License.
|
%% limitations under the License.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-module(emqx_mod_st_statistics).
|
-module(emqx_st_statistics).
|
||||||
|
|
||||||
-behaviour(emqx_gen_mod).
|
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
-include_lib("include/emqx.hrl").
|
-include_lib("include/emqx.hrl").
|
||||||
|
@ -29,12 +28,6 @@
|
||||||
, disable/0, clear_history/0
|
, disable/0, clear_history/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
%% emqx_gen_mod callbacks
|
|
||||||
-export([ load/1
|
|
||||||
, unload/1
|
|
||||||
, description/0
|
|
||||||
]).
|
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([ init/1
|
||||||
, handle_call/3
|
, handle_call/3
|
||||||
|
@ -88,23 +81,6 @@
|
||||||
|
|
||||||
%% ets ordered_set is ascending by term order
|
%% 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
|
%% APIs
|
|
@ -41,7 +41,7 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
clear_history(_Bindings, _Params) ->
|
clear_history(_Bindings, _Params) ->
|
||||||
ok = emqx_mod_st_statistics:clear_history(),
|
ok = emqx_st_statistics:clear_history(),
|
||||||
return(ok).
|
return(ok).
|
||||||
|
|
||||||
get_history(_Bindings, Params) ->
|
get_history(_Bindings, Params) ->
|
|
@ -42,17 +42,16 @@ start_listeners() ->
|
||||||
lists:foreach(fun(Listener) -> start_listener(Listener) end, listeners()).
|
lists:foreach(fun(Listener) -> start_listener(Listener) end, listeners()).
|
||||||
|
|
||||||
%% Start HTTP Listener
|
%% 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"}},
|
Dispatch = [{"/", cowboy_static, {priv_file, emqx_dashboard, "www/index.html"}},
|
||||||
{"/static/[...]", cowboy_static, {priv_dir, emqx_dashboard, "www/static"}},
|
{"/static/[...]", cowboy_static, {priv_dir, emqx_dashboard, "www/static"}},
|
||||||
{"/api/v4/[...]", minirest, http_handlers()}],
|
{"/api/v4/[...]", minirest, http_handlers()}],
|
||||||
minirest:start_http(listener_name(Proto), ranch_opts(Port, Options), Dispatch);
|
Server = listener_name(Proto),
|
||||||
|
RanchOpts = ranch_opts(Port, Options),
|
||||||
start_listener({Proto, Port, Options}) when Proto == https ->
|
case Proto of
|
||||||
Dispatch = [{"/", cowboy_static, {priv_file, emqx_dashboard, "www/index.html"}},
|
http -> minirest:start_http(Server, RanchOpts, Dispatch);
|
||||||
{"/static/[...]", cowboy_static, {priv_dir, emqx_dashboard, "www/static"}},
|
https -> minirest:start_https(Server, RanchOpts, Dispatch)
|
||||||
{"/api/v4/[...]", minirest, http_handlers()}],
|
end.
|
||||||
minirest:start_https(listener_name(Proto), ranch_opts(Port, Options), Dispatch).
|
|
||||||
|
|
||||||
ranch_opts(Port, Options0) ->
|
ranch_opts(Port, Options0) ->
|
||||||
NumAcceptors = get_value(num_acceptors, Options0, 4),
|
NumAcceptors = get_value(num_acceptors, Options0, 4),
|
||||||
|
@ -89,7 +88,7 @@ listener_name(Proto) ->
|
||||||
http_handlers() ->
|
http_handlers() ->
|
||||||
Plugins = lists:map(fun(Plugin) -> Plugin#plugin.name end, emqx_plugins:list()),
|
Plugins = lists:map(fun(Plugin) -> Plugin#plugin.name end, emqx_plugins:list()),
|
||||||
[{"/api/v4/",
|
[{"/api/v4/",
|
||||||
minirest:handler(#{apps => Plugins ++ [emqx_modules],
|
minirest:handler(#{apps => Plugins ++ [emqx_modules, emqx_plugin_libs],
|
||||||
filter => fun ?MODULE:filter/1}),
|
filter => fun ?MODULE:filter/1}),
|
||||||
[{authorization, fun ?MODULE:is_authorized/1}]}].
|
[{authorization, fun ?MODULE:is_authorized/1}]}].
|
||||||
|
|
||||||
|
@ -116,6 +115,7 @@ is_authorized(_Path, Req) ->
|
||||||
_ -> false
|
_ -> false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
filter(#{app := emqx_plugin_libs}) -> true;
|
||||||
filter(#{app := emqx_modules}) -> true;
|
filter(#{app := emqx_modules}) -> true;
|
||||||
filter(#{app := App}) ->
|
filter(#{app := App}) ->
|
||||||
case emqx_plugins:find_plugin(App) of
|
case emqx_plugins:find_plugin(App) of
|
||||||
|
|
|
@ -54,6 +54,7 @@ groups() ->
|
||||||
].
|
].
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
|
application:load(emqx_plugin_libs),
|
||||||
emqx_ct_helpers:start_apps([emqx_modules, emqx_management, emqx_dashboard]),
|
emqx_ct_helpers:start_apps([emqx_modules, emqx_management, emqx_dashboard]),
|
||||||
Config.
|
Config.
|
||||||
|
|
||||||
|
|
|
@ -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".
|
|
@ -65,15 +65,6 @@ t_log_and_pub(_) ->
|
||||||
end,
|
end,
|
||||||
lists:seq(1, 10)),
|
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),
|
timer:sleep(2400),
|
||||||
|
|
||||||
?assert(ets:info(?LOG_TAB, size) =:= 0),
|
?assert(ets:info(?LOG_TAB, size) =:= 0),
|
||||||
|
|
|
@ -24,11 +24,11 @@
|
||||||
-include_lib("emqx/include/emqx.hrl").
|
-include_lib("emqx/include/emqx.hrl").
|
||||||
-include_lib("emqx/include/emqx_mqtt.hrl").
|
-include_lib("emqx/include/emqx_mqtt.hrl").
|
||||||
-include_lib("emqx_management/include/emqx_mgmt.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(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").
|
-define(API_VERSION, "v4").
|
||||||
|
|
||||||
|
@ -38,8 +38,9 @@ all() ->
|
||||||
emqx_ct:all(?MODULE).
|
emqx_ct:all(?MODULE).
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
application:load(emqx_modules),
|
emqx_ct_helpers:boot_modules(all),
|
||||||
emqx_ct_helpers:start_apps([emqx_management]),
|
application:load(emqx_plugin_libs),
|
||||||
|
emqx_ct_helpers:start_apps([emqx_modules, emqx_management, emqx_dashboard]),
|
||||||
Config.
|
Config.
|
||||||
|
|
||||||
end_per_suite(Config) ->
|
end_per_suite(Config) ->
|
||||||
|
@ -55,7 +56,7 @@ end_per_testcase(_, Config) ->
|
||||||
Config.
|
Config.
|
||||||
|
|
||||||
get(Key, ResponseBody) ->
|
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]) ->
|
lookup_alarm(Name, [#{<<"name">> := Name} | _More]) ->
|
||||||
true;
|
true;
|
||||||
|
|
Loading…
Reference in New Issue