Skip to content

Commit

Permalink
fix(backend): 实例搜索 close #2204
Browse files Browse the repository at this point in the history
  • Loading branch information
gaohongsong committed Nov 29, 2023
1 parent 6ebe553 commit 048d845
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions dbm-ui/backend/db_services/quick_search/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""过滤任务"""
Expand Down

0 comments on commit 048d845

Please sign in to comment.