Просмотр исходного кода

chore(elvis): please the elvis

Shawn 3 лет назад
Родитель
Сommit
8a0565a53b

+ 2 - 1
apps/emqx_bridge/src/emqx_bridge.erl

@@ -224,7 +224,8 @@ create(BridgeId, Conf) ->
 create(Type, Name, Conf) ->
     ?SLOG(info, #{msg => "create bridge", type => Type, name => Name,
         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
         {ok, already_created} -> maybe_disable_bridge(Type, Name, Conf);
         {ok, _} -> maybe_disable_bridge(Type, Name, Conf);

+ 18 - 22
apps/emqx_bridge/src/emqx_bridge_api.erl

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

+ 3 - 2
apps/emqx_bridge/src/emqx_bridge_http_schema.erl

@@ -27,8 +27,9 @@ is not allowed.
           #{ desc =>"""
 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/>
-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
-local_topic will be forwarded.
+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 local_topic
+will be forwarded.
 """
            })}
     , {method, mk(method(),

+ 7 - 13
apps/emqx_bridge/src/emqx_bridge_schema.erl

@@ -53,7 +53,8 @@ common_bridge_fields() ->
             , desc =>"""
 The connector ID to be used for this bridge. Connector IDs must be of format:
 <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")),
         #{ 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")),
         #{ desc => "The status of the bridge for each node"
          })}
@@ -103,21 +104,14 @@ fields("node_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") ->
     [ node_name()
-    , {"status", mk(ref(?MODULE, "status"), #{})}
+    , {"status", mk(status(), #{})}
     ].
 
+status() ->
+    hoconsc:enum([connected, disconnected, connecting]).
+
 node_name() ->
     {"node", mk(binary(), #{desc => "The node name", example => "emqx@127.0.0.1"})}.
 

+ 2 - 1
apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl

@@ -55,7 +55,8 @@ init_per_suite(Config) ->
     %% some testcases (may from other app) already get emqx_connector started
     _ = application:stop(emqx_resource),
     _ = 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),
     Config.
 

+ 6 - 6
apps/emqx_connector/src/emqx_connector_api.erl

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

+ 4 - 2
apps/emqx_connector/test/emqx_connector_api_SUITE.erl

@@ -91,12 +91,14 @@ init_per_suite(Config) ->
     ok = emqx_common_test_helpers:start_apps([emqx_rule_engine, emqx_connector,
         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_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),
     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.
 
 set_special_configs(emqx_dashboard) ->

+ 4 - 2
apps/emqx_management/src/emqx_mgmt_api_trace.erl

@@ -343,7 +343,8 @@ group_trace_file(ZipDir, TraceLog, TraceFiles) ->
                     _ -> Acc
                 end;
             {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
         end
                 end, [], TraceFiles).
@@ -375,7 +376,8 @@ stream_log_file(get, #{bindings := #{name := Name}, query_string := Query}) ->
                     {200, #{meta => Meta, items => <<"">>}};
                 {error, Reason} ->
                     ?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}};
                 {badrpc, nodedown} ->
                     {400, #{code => 'RPC_ERROR', message => "BadRpc node down"}}

+ 5 - 3
elvis.config

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