diff --git a/apps/core/ipchooser/tools/base.py b/apps/core/ipchooser/tools/base.py index abc31cfbe..03a518811 100644 --- a/apps/core/ipchooser/tools/base.py +++ b/apps/core/ipchooser/tools/base.py @@ -67,11 +67,11 @@ def fuzzy_cond( @staticmethod def handle_plugin_conditions( - params: typing.Dict, plugin_names: typing.List[str], return_sql: bool = True - ) -> typing.Union[typing.List[int], str]: + params: typing.Dict, plugin_names: typing.List[str] + ) -> typing.Tuple[bool, typing.Optional[QuerySet]]: if not params.get("conditions"): - return [] + return False, None select: typing.Dict[str, str] = { "status": f"{node_man_models.ProcessStatus._meta.db_table}.status", @@ -95,7 +95,7 @@ def handle_plugin_conditions( if condition["key"] in ["source_id", "plugin_name"]: key: str = {"source_id": "source_id", "plugin_name": "name"}[condition["key"]] - sql_params.extend([f'"{cond_val}"' for cond_val in values]) + sql_params.extend(values) wheres.append( f'{node_man_models.ProcessStatus._meta.db_table}.{key} in ({",".join(["%s"] * len(values))})' ) @@ -105,9 +105,9 @@ def handle_plugin_conditions( for cond_val in values: if cond_val == -1: # 无版本插件筛选 - sql_params.append('""') + sql_params.append("") else: - sql_params.append(f'"{cond_val}"') + sql_params.append(cond_val) wheres.append( f'{node_man_models.ProcessStatus._meta.db_table}.version in ({",".join(["%s"] * len(values))})' ) @@ -116,7 +116,7 @@ def handle_plugin_conditions( elif condition["key"] in [f"{plugin}_status" for plugin in plugin_names]: # 插件状态的精确搜索 - sql_params.extend([f'"{cond_val}"' for cond_val in values]) + sql_params.extend(values) wheres.append( f'{node_man_models.ProcessStatus._meta.db_table}.status in ({",".join(["%s"] * len(values))})' ) @@ -126,7 +126,7 @@ def handle_plugin_conditions( if wheres: wheres = init_wheres + wheres - host_queryset: QuerySet = ( + host_id_queryset: QuerySet = ( node_man_models.Host.objects.extra( select=select, tables=[node_man_models.ProcessStatus._meta.db_table], @@ -136,11 +136,8 @@ def handle_plugin_conditions( .order_by() .values_list("bk_host_id", flat=True) ) - if return_sql: - return host_queryset.query - else: - return set(host_queryset) or [-1] - return [] + return True, host_id_queryset + return False, None @staticmethod def fetch_match_node_types(is_proxy: bool, return_all_node_type: bool) -> typing.List[str]: diff --git a/apps/node_man/handlers/plugin.py b/apps/node_man/handlers/plugin.py index 3a11d028b..8079ad3b5 100644 --- a/apps/node_man/handlers/plugin.py +++ b/apps/node_man/handlers/plugin.py @@ -193,19 +193,19 @@ def list(params: Dict[str, Any]): user_biz = plugin_operate_bizs # 查询 - sql: str = HostQuerySqlHelper.handle_plugin_conditions( - params=params, plugin_names=tools.PluginV2Tools.fetch_head_plugins(), return_sql=True + need_query, host_id_queryset = HostQuerySqlHelper.handle_plugin_conditions( + params=params, plugin_names=tools.PluginV2Tools.fetch_head_plugins() ) - extra_wheres = [] - if sql: - extra_wheres = [f"{Host._meta.db_table}.bk_host_id in ({sql})"] hosts_status_sql = ( - HostQuerySqlHelper.multiple_cond_sql( - params=params, biz_scope=user_biz, return_all_node_type=True, extra_wheres=extra_wheres - ).exclude(bk_host_id__in=params.get("exclude_hosts", [])) + HostQuerySqlHelper.multiple_cond_sql(params=params, biz_scope=user_biz, return_all_node_type=True).exclude( + bk_host_id__in=params.get("exclude_hosts", []) + ) ).order_by() + if need_query: + hosts_status_sql = hosts_status_sql.filter(bk_host_id__in=host_id_queryset) + # 计算总数 try: @@ -312,19 +312,16 @@ def operate(params: dict, username: str): if params.get("exclude_hosts") is not None: # 跨页全选 - sql: str = HostQuerySqlHelper.handle_plugin_conditions( - params=params, plugin_names=tools.PluginV2Tools.fetch_head_plugins(), return_sql=True + need_query, host_id_queryset = HostQuerySqlHelper.handle_plugin_conditions( + params=params, plugin_names=tools.PluginV2Tools.fetch_head_plugins() ) - extra_wheres = [] - if sql: - extra_wheres = [f"{Host._meta.db_table}.bk_host_id in ({sql})"] db_host_sql = ( - HostQuerySqlHelper.multiple_cond_sql( - params, user_biz, return_all_node_type=True, extra_wheres=extra_wheres - ) + HostQuerySqlHelper.multiple_cond_sql(params, user_biz, return_all_node_type=True) .exclude(bk_host_id__in=params.get("exclude_hosts", [])) .values("bk_host_id", "bk_biz_id", "bk_cloud_id", "inner_ip", "node_type", "os_type") ) + if need_query: + db_host_sql = db_host_sql.filter(bk_host_id__in=host_id_queryset) else: # 不是跨页全选