refactor(api): statsd api swagger spec
This commit is contained in:
parent
5411f6ff80
commit
824e7c4662
|
@ -20,36 +20,61 @@
|
||||||
|
|
||||||
-include("emqx_statsd.hrl").
|
-include("emqx_statsd.hrl").
|
||||||
|
|
||||||
-import(emqx_mgmt_util, [ schema/1
|
-include_lib("typerefl/include/types.hrl").
|
||||||
, bad_request/0]).
|
|
||||||
|
|
||||||
-export([api_spec/0]).
|
-import(hoconsc, [mk/2, ref/2]).
|
||||||
|
|
||||||
-export([ statsd/2
|
-export([statsd/2]).
|
||||||
|
|
||||||
|
-export([ api_spec/0
|
||||||
|
, paths/0
|
||||||
|
, schema/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
-define(API_TAG_STATSD, [<<"statsd">>]).
|
||||||
|
-define(SCHEMA_MODULE, emqx_statsd_schema).
|
||||||
|
|
||||||
|
-define(INTERNAL_ERROR, 'INTERNAL_ERROR').
|
||||||
|
|
||||||
|
|
||||||
api_spec() ->
|
api_spec() ->
|
||||||
{statsd_api(), []}.
|
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
|
||||||
|
|
||||||
conf_schema() ->
|
paths() ->
|
||||||
emqx_mgmt_api_configs:gen_schema(emqx:get_raw_config([statsd])).
|
["/statsd"].
|
||||||
|
|
||||||
statsd_api() ->
|
schema("/statsd") ->
|
||||||
Metadata = #{
|
#{ 'operationId' => statsd
|
||||||
get => #{
|
, get =>
|
||||||
description => <<"Get statsd info">>,
|
#{ description => <<"Get statsd config">>
|
||||||
responses => #{<<"200">> => schema(conf_schema())}
|
, tags => ?API_TAG_STATSD
|
||||||
},
|
, responses =>
|
||||||
put => #{
|
#{200 => statsd_config_schema()}
|
||||||
description => <<"Update Statsd">>,
|
|
||||||
'requestBody' => schema(conf_schema()),
|
|
||||||
responses => #{
|
|
||||||
<<"200">> => schema(conf_schema()),
|
|
||||||
<<"400">> => bad_request()
|
|
||||||
}
|
}
|
||||||
|
, put =>
|
||||||
|
#{ description => <<"Set statsd config">>
|
||||||
|
, tags => ?API_TAG_STATSD
|
||||||
|
, 'requestBody' => statsd_config_schema()
|
||||||
|
, responses =>
|
||||||
|
#{200 => statsd_config_schema()}
|
||||||
}
|
}
|
||||||
},
|
}.
|
||||||
[{"/statsd", Metadata, statsd}].
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% Helper funcs
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
statsd_config_schema() ->
|
||||||
|
emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(?SCHEMA_MODULE, "statsd"),
|
||||||
|
statsd_example()).
|
||||||
|
|
||||||
|
statsd_example() ->
|
||||||
|
#{ enable => true
|
||||||
|
, flush_time_interval => "32s"
|
||||||
|
, sample_time_interval => "32s"
|
||||||
|
, server => "127.0.0.1:8125"
|
||||||
|
}.
|
||||||
|
|
||||||
statsd(get, _Params) ->
|
statsd(get, _Params) ->
|
||||||
{200, emqx:get_raw_config([<<"statsd">>], #{})};
|
{200, emqx:get_raw_config([<<"statsd">>], #{})};
|
||||||
|
@ -60,5 +85,5 @@ statsd(put, #{body := Body}) ->
|
||||||
{200, NewConfig};
|
{200, NewConfig};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
Message = list_to_binary(io_lib:format("Update config failed ~p", [Reason])),
|
Message = list_to_binary(io_lib:format("Update config failed ~p", [Reason])),
|
||||||
{500, 'INTERNAL_ERROR', Message}
|
{500, ?INTERNAL_ERROR, Message}
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% Copyright (c) 2021-2022 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_statsd_schema).
|
-module(emqx_statsd_schema).
|
||||||
|
|
||||||
-include_lib("typerefl/include/types.hrl").
|
-include_lib("typerefl/include/types.hrl").
|
||||||
|
@ -17,7 +33,7 @@ namespace() -> "statsd".
|
||||||
roots() -> ["statsd"].
|
roots() -> ["statsd"].
|
||||||
|
|
||||||
fields("statsd") ->
|
fields("statsd") ->
|
||||||
[ {enable, hoconsc:mk(boolean(), #{default => false})}
|
[ {enable, hoconsc:mk(boolean(), #{default => false, nullable => false})}
|
||||||
, {server, fun server/1}
|
, {server, fun server/1}
|
||||||
, {sample_time_interval, fun duration_ms/1}
|
, {sample_time_interval, fun duration_ms/1}
|
||||||
, {flush_time_interval, fun duration_ms/1}
|
, {flush_time_interval, fun duration_ms/1}
|
||||||
|
|
Loading…
Reference in New Issue