Przeglądaj źródła

Merge pull request #6661 from HJianBo/fix-client-fuzzy-matching-for-5

chore: client searching support \[]()* chars
JianBo He 4 lat temu
rodzic
commit
19dd5a7c97

+ 6 - 14
apps/emqx_gateway/src/emqx_gateway_api_clients.erl

@@ -288,28 +288,20 @@ ms(lifetime, X) ->
 %% Fuzzy filter funcs
 
 fuzzy_filter_fun(Fuzzy) ->
-    REFuzzy = lists:map(fun({K, like, S}) ->
-                  {ok, RE} = re:compile(escape(S)),
-                  {K, like, RE}
-              end, Fuzzy),
     fun(MsRaws) when is_list(MsRaws) ->
-            lists:filter( fun(E) -> run_fuzzy_filter(E, REFuzzy) end
-                        , MsRaws)
+        lists:filter( fun(E) -> run_fuzzy_filter(E, Fuzzy) end
+                    , MsRaws)
     end.
 
-escape(B) when is_binary(B) ->
-    re:replace(B, <<"\\\\">>, <<"\\\\\\\\">>, [{return, binary}, global]).
-
 run_fuzzy_filter(_, []) ->
     true;
 run_fuzzy_filter(E = {_, #{clientinfo := ClientInfo}, _},
-                 [{Key, _, RE} | Fuzzy]) ->
-    Val = case maps:get(Key, ClientInfo, "") of
-              undefined -> "";
+                 [{Key, like, SubStr} | Fuzzy]) ->
+    Val = case maps:get(Key, ClientInfo, <<>>) of
+              undefined -> <<>>;
               V -> V
           end,
-    re:run(Val, RE, [{capture, none}]) == match
-      andalso run_fuzzy_filter(E, Fuzzy).
+    binary:match(Val, SubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
 
 %%--------------------------------------------------------------------
 %% format funcs

+ 6 - 13
apps/emqx_management/src/emqx_mgmt_api_clients.erl

@@ -710,26 +710,19 @@ ms(created_at, X) ->
 %% Match funcs
 
 fuzzy_filter_fun(Fuzzy) ->
-    REFuzzy = lists:map(fun({K, like, S}) ->
-                 {ok, RE} = re:compile(escape(S)),
-                 {K, like, RE}
-              end, Fuzzy),
     fun(MsRaws) when is_list(MsRaws) ->
-            lists:filter( fun(E) -> run_fuzzy_filter(E, REFuzzy) end
-                        , MsRaws)
+        lists:filter( fun(E) -> run_fuzzy_filter(E, Fuzzy) end
+                    , MsRaws)
     end.
 
-escape(B) when is_binary(B) ->
-    re:replace(B, <<"\\\\">>, <<"\\\\\\\\">>, [{return, binary}, global]).
-
 run_fuzzy_filter(_, []) ->
     true;
-run_fuzzy_filter(E = {_, #{clientinfo := ClientInfo}, _}, [{Key, _, RE} | Fuzzy]) ->
-    Val = case maps:get(Key, ClientInfo, "") of
-              undefined -> "";
+run_fuzzy_filter(E = {_, #{clientinfo := ClientInfo}, _}, [{Key, like, SubStr} | Fuzzy]) ->
+    Val = case maps:get(Key, ClientInfo, <<>>) of
+              undefined -> <<>>;
               V -> V
           end,
-    re:run(Val, RE, [{capture, none}]) == match andalso run_fuzzy_filter(E, Fuzzy).
+    binary:match(Val, SubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
 
 %%--------------------------------------------------------------------
 %% format funcs