fix: topic metrics api path & params (#5599)

This commit is contained in:
DDDHuang 2021-08-30 14:21:15 +08:00 committed by GitHub
parent 77aca28d87
commit 100e550491
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 61 deletions

View File

@ -226,7 +226,7 @@ handle_call({deregister, all}, _From, State) ->
handle_call({deregister, Topic}, _From, State = #state{speeds = Speeds}) -> handle_call({deregister, Topic}, _From, State = #state{speeds = Speeds}) ->
case is_registered(Topic) of case is_registered(Topic) of
false -> false ->
{reply, ok, State}; {reply, {error, topic_not_found}, State};
true -> true ->
true = ets:delete(?TAB, Topic), true = ets:delete(?TAB, Topic),
NSpeeds = lists:foldl(fun(Metric, Acc) -> NSpeeds = lists:foldl(fun(Metric, Acc) ->

View File

@ -20,6 +20,7 @@
-import(emqx_mgmt_util, [ properties/1 -import(emqx_mgmt_util, [ properties/1
, schema/1 , schema/1
, object_schema/1
, object_schema/2 , object_schema/2
, object_array_schema/2 , object_array_schema/2
, error_schema/2 , error_schema/2
@ -27,10 +28,8 @@
-export([api_spec/0]). -export([api_spec/0]).
-export([ list_topic_metrics/2 -export([ topic_metrics/2
, operate_topic_metrics/2 , operate_topic_metrics/2
, reset_all_topic_metrics/2
, reset_topic_metrics/2
]). ]).
-define(ERROR_TOPIC, 'ERROR_TOPIC'). -define(ERROR_TOPIC, 'ERROR_TOPIC').
@ -40,15 +39,10 @@
-define(BAD_REQUEST, 'BAD_REQUEST'). -define(BAD_REQUEST, 'BAD_REQUEST').
api_spec() -> api_spec() ->
{ {[
[ topic_metrics_api(),
list_topic_metrics_api(), operation_topic_metrics_api()
get_topic_metrics_api(), ],[]}.
reset_all_topic_metrics_api(),
reset_topic_metrics_api()
],
[]
}.
properties() -> properties() ->
properties([ properties([
@ -75,61 +69,57 @@ properties() ->
{'messages.qos2.out.rate', number}]} {'messages.qos2.out.rate', number}]}
]). ]).
list_topic_metrics_api() -> topic_metrics_api() ->
MetaData = #{ MetaData = #{
get => #{ get => #{
description => <<"List topic metrics">>, description => <<"List topic metrics">>,
responses => #{ responses => #{
<<"200">> => object_array_schema(properties(), <<"List topic metrics">>) <<"200">> => object_array_schema(properties(), <<"List topic metrics">>)
} }
} },
},
{"/mqtt/topic_metrics", MetaData, list_topic_metrics}.
get_topic_metrics_api() ->
MetaData = #{
get => #{
description => <<"List topic metrics">>,
parameters => [topic_param()],
responses => #{
<<"200">> => object_schema(properties(), <<"List topic metrics">>)}},
put => #{ put => #{
description => <<"Register topic metrics">>, description => <<"Reset topic metrics by topic name, or all">>,
parameters => [topic_param()], 'requestBody' => object_schema(properties([
{topic, string, <<"no topic will reset all">>},
{action, string, <<"Action, default reset">>, [reset]}
])),
responses => #{ responses => #{
<<"200">> => schema(<<"Register topic metrics">>), <<"200">> => schema(<<"Reset topic metrics success">>),
<<"404">> => error_schema(<<"Topic not found">>, [?ERROR_TOPIC])
}
},
post => #{
description => <<"Create topic metrics">>,
'requestBody' => object_schema(properties([{topic, string}])),
responses => #{
<<"200">> => schema(<<"Create topic metrics success">>),
<<"409">> => error_schema(<<"Topic metrics max limit">>, [?EXCEED_LIMIT]), <<"409">> => error_schema(<<"Topic metrics max limit">>, [?EXCEED_LIMIT]),
<<"400">> => error_schema(<<"Topic metrics already exist">>, [?BAD_REQUEST]) <<"400">> => error_schema(<<"Topic metrics already exist">>, [?BAD_REQUEST])
} }
}, }
},
{"/mqtt/topic_metrics", MetaData, topic_metrics}.
operation_topic_metrics_api() ->
MetaData = #{
get => #{
description => <<"Get topic metrics">>,
parameters => [topic_param()],
responses => #{
<<"200">> => object_schema(properties(), <<"Topic metrics">>),
<<"404">> => error_schema(<<"Topic not found">>, [?ERROR_TOPIC])
}},
delete => #{ delete => #{
description => <<"Deregister topic metrics">>, description => <<"Deregister topic metrics">>,
parameters => [topic_param()], parameters => [topic_param()],
responses => #{ <<"200">> => schema(<<"Deregister topic metrics">>)} responses => #{
<<"200">> => schema(<<"Deregister topic metrics">>),
<<"404">> => error_schema(<<"Topic not found">>, [?ERROR_TOPIC])
}
} }
}, },
{"/mqtt/topic_metrics/:topic", MetaData, operate_topic_metrics}. {"/mqtt/topic_metrics/:topic", MetaData, operate_topic_metrics}.
reset_all_topic_metrics_api() ->
MetaData = #{
put => #{
description => <<"Reset all topic metrics">>,
responses => #{<<"200">> => schema(<<"Reset all topic metrics">>)}
}
},
{"/mqtt/topic_metrics/reset", MetaData, reset_all_topic_metrics}.
reset_topic_metrics_api() ->
Path = "/mqtt/topic_metrics/:topic/reset",
MetaData = #{
put => #{
description => <<"Reset topic metrics">>,
parameters => [topic_param()],
responses => #{<<"200">> => schema(<<"Reset topic metrics">>)}
}
},
{Path, MetaData, reset_topic_metrics}.
topic_param() -> topic_param() ->
#{ #{
name => topic, name => topic,
@ -141,8 +131,14 @@ topic_param() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% api callback %% api callback
list_topic_metrics(get, _) -> topic_metrics(get, _) ->
list_metrics(). list_metrics();
topic_metrics(put, #{body := #{<<"topic">> := Topic, <<"action">> := <<"reset">>}}) ->
reset(Topic);
topic_metrics(put, #{body := #{<<"action">> := <<"reset">>}}) ->
reset();
topic_metrics(post, #{body := #{<<"topic">> := Topic}}) ->
register(Topic).
operate_topic_metrics(Method, #{bindings := #{topic := Topic0}}) -> operate_topic_metrics(Method, #{bindings := #{topic := Topic0}}) ->
Topic = decode_topic(Topic0), Topic = decode_topic(Topic0),
@ -155,13 +151,6 @@ operate_topic_metrics(Method, #{bindings := #{topic := Topic0}}) ->
deregister(Topic) deregister(Topic)
end. end.
reset_all_topic_metrics(put, _) ->
reset().
reset_topic_metrics(put, #{bindings := #{topic := Topic0}}) ->
Topic = decode_topic(Topic0),
reset(Topic).
decode_topic(Topic) -> decode_topic(Topic) ->
uri_string:percent_decode(Topic). uri_string:percent_decode(Topic).
@ -184,8 +173,13 @@ register(Topic) ->
end. end.
deregister(Topic) -> deregister(Topic) ->
_ = emqx_topic_metrics:deregister(Topic), case emqx_topic_metrics:deregister(Topic) of
{200}. {error, topic_not_found} ->
Message = list_to_binary(io_lib:format("Topic ~p not found", [Topic])),
{404, #{code => ?ERROR_TOPIC, message => Message}};
ok ->
{200}
end.
get_metrics(Topic) -> get_metrics(Topic) ->
case emqx_topic_metrics:metrics(Topic) of case emqx_topic_metrics:metrics(Topic) of