Skip to content

Commit

Permalink
feature: 策略列表拉取慢 (closed TencentBlueKing#1792)
Browse files Browse the repository at this point in the history
  • Loading branch information
Huayeaaa committed Feb 5, 2024
1 parent 2bea8e0 commit 095d89d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
21 changes: 13 additions & 8 deletions apps/node_man/handlers/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import Any, Dict, List, Optional, Set, Union

from django.conf import settings
from django.db.models import Max, Q
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from packaging import version
Expand Down Expand Up @@ -136,13 +136,18 @@ def search_deploy_policy(query_params: Dict[str, Any]) -> Dict[str, Any]:
all_policy_ids = [policy["id"] for policy in all_policies]

# 查询每个策略下最新的任务
latest_task_ids_in_same_policy = (
models.SubscriptionTask.objects.filter(subscription_id__in=all_policy_ids)
.values("subscription_id")
.annotate(id=Max("id"))
.values_list("id", flat=True)
)
sub_tasks = models.SubscriptionTask.objects.filter(id__in=latest_task_ids_in_same_policy).values(
latest_task_ids_in_same_policy = models.SubscriptionTask.objects.filter(
subscription_id__in=all_policy_ids
).values("id", "subscription_id")

max_sub_task_id_list, task_ids_gby_sub_id = [], defaultdict(list)
for sub_task_tuple in latest_task_ids_in_same_policy:
task_ids_gby_sub_id[sub_task_tuple["subscription_id"]].append(sub_task_tuple["id"])

for sub_task_id_list in task_ids_gby_sub_id.values():
max_sub_task_id_list.append(max(sub_task_id_list))

sub_tasks = models.SubscriptionTask.objects.filter(id__in=max_sub_task_id_list).values(
"id", "subscription_id", "is_ready", "err_msg", "is_auto_trigger"
)
job_objs = tools.JobTools.list_sub_task_related_jobs(
Expand Down
19 changes: 19 additions & 0 deletions apps/node_man/tests/test_handlers/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ProcessStatus,
Subscription,
SubscriptionStep,
SubscriptionTask,
)
from apps.node_man.tests.utils import (
SEARCH_BUSINESS,
Expand Down Expand Up @@ -192,6 +193,24 @@ def test_search_policy(self):
)
self.assertEqual(len(result["list"]), 1)

@patch("apps.node_man.handlers.cmdb.CmdbHandler.cmdb_or_cache_biz", cmdb_or_cache_biz)
@patch("common.api.NodeApi.subscription_search_policy", NodeApi.subscription_search_policy)
def test_search_policy_modify(self):
for i in range(1, 11):
SubscriptionTask.objects.create(
subscription_id=1, scope={}, actions={"host|instance|host|id": {"bkmonitorbeat": "MAIN_INSTALL_PLUGIN"}}
)
result = PolicyHandler.search_deploy_policy(
query_params={
"bk_biz_ids": self.subscription_obj.bk_biz_scope,
"conditions": [],
"only_root": True,
"page": 1,
"pagesize": 20,
}
)
self.assertEqual(len(result["list"]), 1)

@patch("apps.node_man.handlers.cmdb.CmdbHandler.cmdb_or_cache_biz", cmdb_or_cache_biz)
def test_fetch_policy_topo(self):
policy_topo = PolicyHandler.fetch_policy_topo(bk_biz_ids=[SEARCH_BUSINESS[0]["bk_biz_id"]])
Expand Down

0 comments on commit 095d89d

Please sign in to comment.