refactor(statsd): refactor statsd swagger schema

This commit is contained in:
Turtle 2021-08-21 14:12:26 +08:00 committed by turtleDeng
parent d4d4ba9ea4
commit 31f588671d
2 changed files with 17 additions and 45 deletions

View File

@ -20,10 +20,7 @@
-include("emqx_statsd.hrl"). -include("emqx_statsd.hrl").
-import(emqx_mgmt_util, [ response_schema/1 -import(emqx_mgmt_util, [response_schema/2, request_body_schema/1]).
, response_schema/2
, request_body_schema/1
]).
-export([api_spec/0]). -export([api_spec/0]).
@ -34,42 +31,22 @@ api_spec() ->
{statsd_api(), schemas()}. {statsd_api(), schemas()}.
schemas() -> schemas() ->
[#{statsd => #{ [#{statsd => emqx_mgmt_api_configs:gen_schema(emqx:get_raw_config([statsd]))}].
type => object,
properties => #{
server => #{
type => string,
description => <<"Statsd Server">>,
example => get_raw(<<"server">>, <<"127.0.0.1:8125">>)},
enable => #{
type => boolean,
description => <<"Statsd status">>,
example => get_raw(<<"enable">>, false)},
sample_time_interval => #{
type => string,
description => <<"Sample Time Interval">>,
example => get_raw(<<"sample_time_interval">>, <<"10s">>)},
flush_time_interval => #{
type => string,
description => <<"Flush Time Interval">>,
example => get_raw(<<"flush_time_interval">>, <<"10s">>)}
}
}}].
statsd_api() -> statsd_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => <<"Get statsd info">>, description => <<"Get statsd info">>,
responses => #{ responses => #{
<<"200">> => response_schema(<<"statsd">>) <<"200">> => response_schema(<<>>, statsd)
} }
}, },
put => #{ put => #{
description => <<"Update Statsd">>, description => <<"Update Statsd">>,
'requestBody' => request_body_schema(<<"statsd">>), 'requestBody' => request_body_schema(statsd),
responses => #{ responses => #{
<<"200">> => <<"200">> =>
response_schema(<<"Update Statsd successfully">>), response_schema(<<>>, statsd),
<<"400">> => <<"400">> =>
response_schema(<<"Bad Request">>, #{ response_schema(<<"Bad Request">>, #{
type => object, type => object,
@ -84,23 +61,18 @@ statsd_api() ->
[{"/statsd", Metadata, statsd}]. [{"/statsd", Metadata, statsd}].
statsd(get, _Request) -> statsd(get, _Request) ->
Response = emqx:get_raw_config([<<"statsd">>], #{}), {200, emqx:get_raw_config([<<"statsd">>], #{})};
{200, Response};
statsd(put, Request) -> statsd(put, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
Params = emqx_json:decode(Body, [return_maps]), Params = emqx_json:decode(Body, [return_maps]),
Enable = maps:get(<<"enable">>, Params), {ok, Config} = emqx:update_config([statsd], Params),
{ok, _} = emqx:update_config([statsd], Params), case maps:get(<<"enable">>, Params) of
enable_statsd(Enable). true ->
enable_statsd(true) ->
ok = emqx_statsd_sup:stop_child(?APP),
emqx_statsd_sup:start_child(?APP, emqx:get_config([statsd], #{})),
{200};
enable_statsd(false) ->
_ = emqx_statsd_sup:stop_child(?APP), _ = emqx_statsd_sup:stop_child(?APP),
{200}. emqx_statsd_sup:start_child(?APP, maps:get(config, Config));
false ->
get_raw(Key, Def) -> _ = emqx_statsd_sup:stop_child(?APP),
emqx:get_raw_config([<<"statsd">>]++ [Key], Def). ok
end,
{200, emqx:get_raw_config([<<"statsd">>], #{})}.

View File

@ -21,8 +21,8 @@ fields("statsd") ->
]. ].
server(type) -> emqx_schema:ip_port(); server(type) -> emqx_schema:ip_port();
server(default) -> "127.0.0.1:8125";
server(nullable) -> false; server(nullable) -> false;
server(default) -> "127.0.0.1:8125";
server(_) -> undefined. server(_) -> undefined.
duration_ms(type) -> emqx_schema:duration_ms(); duration_ms(type) -> emqx_schema:duration_ms();