Skip to content

Commit

Permalink
feat(backend): db-table-filter-enable-multi-pattern TencentBlueKing#7380
Browse files Browse the repository at this point in the history
  • Loading branch information
xfwduke authored and iSecloud committed Oct 17, 2024
1 parent 9a6edc0 commit fe50313
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
17 changes: 15 additions & 2 deletions dbm-ui/backend/flow/utils/mysql/db_table_filter/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
specific language governing permissions and limitations under the License.
"""
import itertools
from typing import List
from typing import Dict, List, Tuple

from django.utils.translation import ugettext_lazy as _

from .exception import DbTableFilterValidateException
from .tools import build_exclude_regexp, build_include_regexp, glob_check, replace_glob
from .tools import build_exclude_regexp, build_include_regexp, glob_check, pattern_inclusion, replace_glob


class DbTableFilter(object):
Expand Down Expand Up @@ -100,3 +100,16 @@ def inject_system_dbs(self, system_dbs: List[str]):

self._build_db_filter_regexp()
self._build_table_filter_regexp()

def check_inclusion(self) -> Dict[str, List[Tuple[str, str]]]:
"""
模式包含关系检查
如果存在包含关系, List[Tuple[str, str]] 非空
类似 [('p%', 'p2???'), ('p%', 'p211'), ('p%', 'p4'), ('p%', 'p5')]
"""
return {
"include-db": pattern_inclusion(self.include_db_patterns),
"exclude-db": pattern_inclusion(self.exclude_db_patterns),
"include-table": pattern_inclusion(self.include_table_patterns),
"exclude-table": pattern_inclusion(self.exclude_table_patterns),
}
17 changes: 14 additions & 3 deletions dbm-ui/backend/flow/utils/mysql/db_table_filter/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
specific language governing permissions and limitations under the License.
"""
import re
from typing import List
from typing import List, Tuple

from django.utils.translation import ugettext_lazy as _

Expand All @@ -26,8 +26,8 @@ def glob_check(patterns: List[str]):

for p in patterns:
if contain_glob(p):
if len(patterns) > 1:
raise DbTableFilterValidateException(msg=_("使用通配符时, 只能有一个模式: {}").format(patterns))
# if len(patterns) > 1:
# raise DbTableFilterValidateException(msg=_("使用通配符时, 只能有一个模式: {}").format(patterns))

if ("%" in p or "?" in p) and r1.match(p):
raise DbTableFilterValidateException(msg=_("% ? 不能独立使用"))
Expand All @@ -36,6 +36,17 @@ def glob_check(patterns: List[str]):
raise DbTableFilterValidateException(msg=_("* 只能独立使用"))


def pattern_inclusion(patterns: List[str]) -> List[Tuple[str, str]]:
regex_list = [re.compile(replace_glob(f"^{p}$")) for p in patterns]

res = []
for i in range(len(patterns)):
for j in range(i + 1, len(patterns)):
if regex_list[i].match(patterns[j]) or regex_list[j].match(patterns[i]):
res.append((patterns[i], patterns[j]))
return res


def replace_glob(p: str) -> str:
return p.replace("*", ".*").replace("%", ".*").replace("?", ".")

Expand Down

0 comments on commit fe50313

Please sign in to comment.