From 048d8457e78237d645977c2e732940a0fc947127 Mon Sep 17 00:00:00 2001 From: gaohongsong Date: Wed, 29 Nov 2023 16:49:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20=E5=AE=9E=E4=BE=8B=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=20close=20#2204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db_services/quick_search/handlers.py | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/dbm-ui/backend/db_services/quick_search/handlers.py b/dbm-ui/backend/db_services/quick_search/handlers.py index a886710806..60e12dd649 100644 --- a/dbm-ui/backend/db_services/quick_search/handlers.py +++ b/dbm-ui/backend/db_services/quick_search/handlers.py @@ -8,10 +8,10 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ -from django.db.models import Q +from django.db.models import F, Q from backend.db_meta.enums import ClusterType -from backend.db_meta.models import Cluster, Machine, StorageInstance +from backend.db_meta.models import Cluster, Machine, ProxyInstance, StorageInstance from backend.db_services.quick_search.constants import FilterType, ResourceType from backend.flow.models import FlowTree @@ -40,12 +40,18 @@ def search(self, keyword): return result - def common_filter(self, objs): + def common_filter(self, objs, return_type="list"): + """ + return_type: list | objects + """ if self.bk_biz_ids: objs = objs.filter(bk_biz_id__in=self.bk_biz_ids) if self.db_types: objs = objs.filter(cluster_type__in=self.cluster_types) + if return_type == "objects": + return objs + return list(objs[: self.limit].values()) def filter_cluster_name(self, keyword): @@ -75,8 +81,36 @@ def filter_instance(self, keyword): else: qs = Q(machine__ip__contains=keyword) | Q(name__contains=keyword) - objs = StorageInstance.objects.filter(qs) - return self.common_filter(objs) + if self.bk_biz_ids: + qs = Q(bk_biz_id__in=self.bk_biz_ids) & qs + + if self.db_types: + qs = Q(cluster_type__in=self.cluster_types) & qs + + objs = ( + StorageInstance.objects.filter(qs) + .annotate(role=F("instance_role"), cluster_id=F("cluster__id"), ip=F("machine__ip")) + .union( + ProxyInstance.objects.filter(qs).annotate( + role=F("access_layer"), + cluster_id=F("cluster__id"), + ip=F("machine__ip"), + ) + ) + ) + + return objs.values( + "id", + "name", + "bk_biz_id", + "cluster_id", + "role", + "ip", + "port", + "machine_type", + "status", + "phase", + ) def filter_task(self, keyword): """过滤任务"""