chore(elvis): please the elvis

This commit is contained in:
Shawn 2022-03-08 10:47:48 +08:00
parent b20902ebfe
commit 8a0565a53b
9 changed files with 51 additions and 52 deletions

View File

@ -224,7 +224,8 @@ create(BridgeId, Conf) ->
create(Type, Name, Conf) -> create(Type, Name, Conf) ->
?SLOG(info, #{msg => "create bridge", type => Type, name => Name, ?SLOG(info, #{msg => "create bridge", type => Type, name => Name,
config => Conf}), config => Conf}),
case emqx_resource:create_local(resource_id(Type, Name), <<"emqx_bridge">>, emqx_bridge:resource_type(Type), case emqx_resource:create_local(resource_id(Type, Name), <<"emqx_bridge">>,
emqx_bridge:resource_type(Type),
parse_confs(Type, Name, Conf), #{async_create => true}) of parse_confs(Type, Name, Conf), #{async_create => true}) of
{ok, already_created} -> maybe_disable_bridge(Type, Name, Conf); {ok, already_created} -> maybe_disable_bridge(Type, Name, Conf);
{ok, _} -> maybe_disable_bridge(Type, Name, Conf); {ok, _} -> maybe_disable_bridge(Type, Name, Conf);

View File

@ -134,35 +134,31 @@ info_example(Type, Direction, Method) ->
maps:merge(info_example_basic(Type, Direction), maps:merge(info_example_basic(Type, Direction),
method_example(Type, Direction, Method)). method_example(Type, Direction, Method)).
method_example(Type, Direction, get) -> method_example(Type, Direction, Method) when Method == get; Method == post ->
SType = atom_to_list(Type), SType = atom_to_list(Type),
SDir = atom_to_list(Direction), SDir = atom_to_list(Direction),
SName = case Type of SName = case Type of
http -> "my_" ++ SType ++ "_bridge"; http -> "my_" ++ SType ++ "_bridge";
_ -> "my_" ++ SDir ++ "_" ++ SType ++ "_bridge" _ -> "my_" ++ SDir ++ "_" ++ SType ++ "_bridge"
end, end,
#{ TypeNameExamp = #{
type => bin(SType), type => bin(SType),
name => bin(SName), name => bin(SName)
},
maybe_with_metrics_example(TypeNameExamp, Method);
method_example(_Type, _Direction, put) ->
#{}.
maybe_with_metrics_example(TypeNameExamp, get) ->
TypeNameExamp#{
metrics => ?METRICS(0, 0, 0, 0, 0, 0), metrics => ?METRICS(0, 0, 0, 0, 0, 0),
node_metrics => [ node_metrics => [
#{node => node(), #{node => node(),
metrics => ?METRICS(0, 0, 0, 0, 0, 0)} metrics => ?METRICS(0, 0, 0, 0, 0, 0)}
] ]
}; };
method_example(Type, Direction, post) -> maybe_with_metrics_example(TypeNameExamp, _) ->
SType = atom_to_list(Type), TypeNameExamp.
SDir = atom_to_list(Direction),
SName = case Type of
http -> "my_" ++ SType ++ "_bridge";
_ -> "my_" ++ SDir ++ "_" ++ SType ++ "_bridge"
end,
#{
type => bin(SType),
name => bin(SName)
};
method_example(_Type, _Direction, put) ->
#{}.
info_example_basic(http, _) -> info_example_basic(http, _) ->
#{ #{
@ -203,7 +199,7 @@ info_example_basic(mqtt, egress) ->
schema("/bridges") -> schema("/bridges") ->
#{ #{
operationId => '/bridges', 'operationId' => '/bridges',
get => #{ get => #{
tags => [<<"bridges">>], tags => [<<"bridges">>],
summary => <<"List Bridges">>, summary => <<"List Bridges">>,
@ -218,7 +214,7 @@ schema("/bridges") ->
tags => [<<"bridges">>], tags => [<<"bridges">>],
summary => <<"Create Bridge">>, summary => <<"Create Bridge">>,
description => <<"Create a new bridge by type and name">>, description => <<"Create a new bridge by type and name">>,
requestBody => emqx_dashboard_swagger:schema_with_examples( 'requestBody' => emqx_dashboard_swagger:schema_with_examples(
emqx_bridge_schema:post_request(), emqx_bridge_schema:post_request(),
bridge_info_examples(post)), bridge_info_examples(post)),
responses => #{ responses => #{
@ -230,7 +226,7 @@ schema("/bridges") ->
schema("/bridges/:id") -> schema("/bridges/:id") ->
#{ #{
operationId => '/bridges/:id', 'operationId' => '/bridges/:id',
get => #{ get => #{
tags => [<<"bridges">>], tags => [<<"bridges">>],
summary => <<"Get Bridge">>, summary => <<"Get Bridge">>,
@ -246,7 +242,7 @@ schema("/bridges/:id") ->
summary => <<"Update Bridge">>, summary => <<"Update Bridge">>,
description => <<"Update a bridge by Id">>, description => <<"Update a bridge by Id">>,
parameters => [param_path_id()], parameters => [param_path_id()],
requestBody => emqx_dashboard_swagger:schema_with_examples( 'requestBody' => emqx_dashboard_swagger:schema_with_examples(
emqx_bridge_schema:put_request(), emqx_bridge_schema:put_request(),
bridge_info_examples(put)), bridge_info_examples(put)),
responses => #{ responses => #{
@ -268,7 +264,7 @@ schema("/bridges/:id") ->
schema("/bridges/:id/operation/:operation") -> schema("/bridges/:id/operation/:operation") ->
#{ #{
operationId => '/bridges/:id/operation/:operation', 'operationId' => '/bridges/:id/operation/:operation',
post => #{ post => #{
tags => [<<"bridges">>], tags => [<<"bridges">>],
summary => <<"Start/Stop/Restart Bridge">>, summary => <<"Start/Stop/Restart Bridge">>,

View File

@ -27,8 +27,9 @@ is not allowed.
#{ desc =>""" #{ desc =>"""
The MQTT topic filter to be forwarded to the HTTP server. All MQTT 'PUBLISH' messages with the topic The MQTT topic filter to be forwarded to the HTTP server. All MQTT 'PUBLISH' messages with the topic
matching the local_topic will be forwarded.<br/> matching the local_topic will be forwarded.<br/>
NOTE: if this bridge is used as the output of a rule (EMQX rule engine), and also local_topic is configured, then both the data got from the rule and the MQTT messages that match NOTE: if this bridge is used as the output of a rule (EMQX rule engine), and also local_topic is
local_topic will be forwarded. configured, then both the data got from the rule and the MQTT messages that match local_topic
will be forwarded.
""" """
})} })}
, {method, mk(method(), , {method, mk(method(),

View File

@ -53,7 +53,8 @@ common_bridge_fields() ->
, desc =>""" , desc =>"""
The connector ID to be used for this bridge. Connector IDs must be of format: The connector ID to be used for this bridge. Connector IDs must be of format:
<code>{type}:{name}</code>.<br> <code>{type}:{name}</code>.<br>
In config files, you can find the corresponding config entry for a connector by such path: 'connectors.{type}.{name}'.<br> In config files, you can find the corresponding config entry for a connector by such path:
'connectors.{type}.{name}'.<br>
""" """
})} })}
]. ].
@ -63,7 +64,7 @@ metrics_status_fields() ->
, {"node_metrics", mk(hoconsc:array(ref(?MODULE, "node_metrics")), , {"node_metrics", mk(hoconsc:array(ref(?MODULE, "node_metrics")),
#{ desc => "The metrics of the bridge for each node" #{ desc => "The metrics of the bridge for each node"
})} })}
, {"status", mk(ref(?MODULE, "status"), #{desc => "The status of the bridge"})} , {"status", mk(status(), #{desc => "The status of the bridge"})}
, {"node_status", mk(hoconsc:array(ref(?MODULE, "node_status")), , {"node_status", mk(hoconsc:array(ref(?MODULE, "node_status")),
#{ desc => "The status of the bridge for each node" #{ desc => "The status of the bridge for each node"
})} })}
@ -103,21 +104,14 @@ fields("node_metrics") ->
, {"metrics", mk(ref(?MODULE, "metrics"), #{})} , {"metrics", mk(ref(?MODULE, "metrics"), #{})}
]; ];
fields("status") ->
[ {"matched", mk(integer(), #{desc => "Count of this bridge is queried"})}
, {"success", mk(integer(), #{desc => "Count of query success"})}
, {"failed", mk(integer(), #{desc => "Count of query failed"})}
, {"rate", mk(float(), #{desc => "The rate of matched, times/second"})}
, {"rate_max", mk(float(), #{desc => "The max rate of matched, times/second"})}
, {"rate_last5m", mk(float(),
#{desc => "The average rate of matched in the last 5 minutes, times/second"})}
];
fields("node_status") -> fields("node_status") ->
[ node_name() [ node_name()
, {"status", mk(ref(?MODULE, "status"), #{})} , {"status", mk(status(), #{})}
]. ].
status() ->
hoconsc:enum([connected, disconnected, connecting]).
node_name() -> node_name() ->
{"node", mk(binary(), #{desc => "The node name", example => "emqx@127.0.0.1"})}. {"node", mk(binary(), #{desc => "The node name", example => "emqx@127.0.0.1"})}.

View File

@ -55,7 +55,8 @@ init_per_suite(Config) ->
%% some testcases (may from other app) already get emqx_connector started %% some testcases (may from other app) already get emqx_connector started
_ = application:stop(emqx_resource), _ = application:stop(emqx_resource),
_ = application:stop(emqx_connector), _ = application:stop(emqx_connector),
ok = emqx_common_test_helpers:start_apps([emqx_bridge, emqx_dashboard], fun set_special_configs/1), ok = emqx_common_test_helpers:start_apps([emqx_bridge, emqx_dashboard],
fun set_special_configs/1),
ok = emqx_common_test_helpers:load_config(emqx_bridge_schema, ?CONF_DEFAULT), ok = emqx_common_test_helpers:load_config(emqx_bridge_schema, ?CONF_DEFAULT),
Config. Config.

View File

@ -122,13 +122,13 @@ param_path_id() ->
schema("/connectors_test") -> schema("/connectors_test") ->
#{ #{
operationId => '/connectors_test', 'operationId' => '/connectors_test',
post => #{ post => #{
tags => [<<"connectors">>], tags => [<<"connectors">>],
description => <<"Test creating a new connector by given Id <br>" description => <<"Test creating a new connector by given Id <br>"
"The ID must be of format '{type}:{name}'">>, "The ID must be of format '{type}:{name}'">>,
summary => <<"Test creating connector">>, summary => <<"Test creating connector">>,
requestBody => post_request_body_schema(), 'requestBody' => post_request_body_schema(),
responses => #{ responses => #{
204 => <<"Test connector OK">>, 204 => <<"Test connector OK">>,
400 => error_schema(['TEST_FAILED'], "connector test failed") 400 => error_schema(['TEST_FAILED'], "connector test failed")
@ -138,7 +138,7 @@ schema("/connectors_test") ->
schema("/connectors") -> schema("/connectors") ->
#{ #{
operationId => '/connectors', 'operationId' => '/connectors',
get => #{ get => #{
tags => [<<"connectors">>], tags => [<<"connectors">>],
description => <<"List all connectors">>, description => <<"List all connectors">>,
@ -153,7 +153,7 @@ schema("/connectors") ->
tags => [<<"connectors">>], tags => [<<"connectors">>],
description => <<"Create a new connector">>, description => <<"Create a new connector">>,
summary => <<"Create connector">>, summary => <<"Create connector">>,
requestBody => post_request_body_schema(), 'requestBody' => post_request_body_schema(),
responses => #{ responses => #{
201 => get_response_body_schema(), 201 => get_response_body_schema(),
400 => error_schema(['ALREADY_EXISTS'], "connector already exists") 400 => error_schema(['ALREADY_EXISTS'], "connector already exists")
@ -163,7 +163,7 @@ schema("/connectors") ->
schema("/connectors/:id") -> schema("/connectors/:id") ->
#{ #{
operationId => '/connectors/:id', 'operationId' => '/connectors/:id',
get => #{ get => #{
tags => [<<"connectors">>], tags => [<<"connectors">>],
description => <<"Get the connector by Id">>, description => <<"Get the connector by Id">>,
@ -179,7 +179,7 @@ schema("/connectors/:id") ->
description => <<"Update an existing connector by Id">>, description => <<"Update an existing connector by Id">>,
summary => <<"Update connector">>, summary => <<"Update connector">>,
parameters => param_path_id(), parameters => param_path_id(),
requestBody => put_request_body_schema(), 'requestBody' => put_request_body_schema(),
responses => #{ responses => #{
200 => get_response_body_schema(), 200 => get_response_body_schema(),
404 => error_schema(['NOT_FOUND'], "Connector not found") 404 => error_schema(['NOT_FOUND'], "Connector not found")

View File

@ -91,12 +91,14 @@ init_per_suite(Config) ->
ok = emqx_common_test_helpers:start_apps([emqx_rule_engine, emqx_connector, ok = emqx_common_test_helpers:start_apps([emqx_rule_engine, emqx_connector,
emqx_bridge, emqx_dashboard], fun set_special_configs/1), emqx_bridge, emqx_dashboard], fun set_special_configs/1),
ok = emqx_common_test_helpers:load_config(emqx_connector_schema, <<"connectors: {}">>), ok = emqx_common_test_helpers:load_config(emqx_connector_schema, <<"connectors: {}">>),
ok = emqx_common_test_helpers:load_config(emqx_rule_engine_schema, <<"rule_engine {rules {}}">>), ok = emqx_common_test_helpers:load_config(emqx_rule_engine_schema,
<<"rule_engine {rules {}}">>),
ok = emqx_common_test_helpers:load_config(emqx_bridge_schema, ?BRIDGE_CONF_DEFAULT), ok = emqx_common_test_helpers:load_config(emqx_bridge_schema, ?BRIDGE_CONF_DEFAULT),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
emqx_common_test_helpers:stop_apps([emqx_rule_engine, emqx_connector, emqx_bridge, emqx_dashboard]), emqx_common_test_helpers:stop_apps([emqx_rule_engine, emqx_connector, emqx_bridge,
emqx_dashboard]),
ok. ok.
set_special_configs(emqx_dashboard) -> set_special_configs(emqx_dashboard) ->

View File

@ -343,7 +343,8 @@ group_trace_file(ZipDir, TraceLog, TraceFiles) ->
_ -> Acc _ -> Acc
end; end;
{error, Node, Reason} -> {error, Node, Reason} ->
?SLOG(error, #{msg => "download_trace_log_error", node => Node, log => TraceLog, reason => Reason}), ?SLOG(error, #{msg => "download_trace_log_error", node => Node,
log => TraceLog, reason => Reason}),
Acc Acc
end end
end, [], TraceFiles). end, [], TraceFiles).
@ -375,7 +376,8 @@ stream_log_file(get, #{bindings := #{name := Name}, query_string := Query}) ->
{200, #{meta => Meta, items => <<"">>}}; {200, #{meta => Meta, items => <<"">>}};
{error, Reason} -> {error, Reason} ->
?SLOG(error, #{msg => "read_file_failed", ?SLOG(error, #{msg => "read_file_failed",
node => Node, name => Name, reason => Reason, position => Position, bytes => Bytes}), node => Node, name => Name, reason => Reason,
position => Position, bytes => Bytes}),
{400, #{code => 'READ_FILE_ERROR', message => Reason}}; {400, #{code => 'READ_FILE_ERROR', message => Reason}};
{badrpc, nodedown} -> {badrpc, nodedown} ->
{400, #{code => 'RPC_ERROR', message => "BadRpc node down"}} {400, #{code => 'RPC_ERROR', message => "BadRpc node down"}}

View File

@ -9,6 +9,8 @@
filter => "*.erl", filter => "*.erl",
ruleset => erl_files, ruleset => erl_files,
rules => [ rules => [
{elvis_style, macro_names, disable},
{elvis_style, function_naming_convention, disable},
{elvis_style, state_record_and_type, disable}, {elvis_style, state_record_and_type, disable},
{elvis_style, no_common_caveats_call, #{}}, {elvis_style, no_common_caveats_call, #{}},
{elvis_style, no_debug_call, #{ debug_functions => [ {ct, pal} {elvis_style, no_debug_call, #{ debug_functions => [ {ct, pal}
@ -19,15 +21,15 @@
{right, "||"}, {right, "||"},
{left, "||"}]}}, {left, "||"}]}},
{elvis_style, dont_repeat_yourself, #{ min_complexity => 20 }}, {elvis_style, dont_repeat_yourself, #{ min_complexity => 20 }},
{elvis_style, god_modules, #{ignore => [emqx_authentication, {elvis_style, god_modules, #{limit => 100}}
emqx_resource]}}
] ]
}, },
#{dirs => ["test", "apps/**/test"], #{dirs => ["test", "apps/**/test"],
filter => "*.erl", filter => "*.erl",
rules => [ rules => [
{elvis_text_style, line_length, #{ limit => 100 {elvis_text_style, line_length, #{ limit => 100
, skip_comments => false }}, , skip_comments => false
}},
{elvis_style, dont_repeat_yourself, #{ min_complexity => 100 }} {elvis_style, dont_repeat_yourself, #{ min_complexity => 100 }}
] ]
}, },