From dd6def83bafeb721683b0076d23212f0ad3d6710 Mon Sep 17 00:00:00 2001 From: WytheLi Date: Fri, 1 Nov 2024 15:10:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20sql=E6=9F=A5=E8=AF=A2=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E4=BC=98=E5=8C=96=20#7666=20#=20Reviewed,=20transacti?= =?UTF-8?q?on=20id:=2022552?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/db_meta/apps.py | 5 ++++- dbm-ui/backend/db_meta/models/app.py | 4 +++- dbm-ui/backend/db_meta/models/db_module.py | 8 ++++++-- dbm-ui/backend/db_meta/utils.py | 10 ++++++++++ dbm-ui/backend/db_monitor/views/notice_group.py | 7 +++++-- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/dbm-ui/backend/db_meta/apps.py b/dbm-ui/backend/db_meta/apps.py index f1c00df0d6..e66fde878e 100644 --- a/dbm-ui/backend/db_meta/apps.py +++ b/dbm-ui/backend/db_meta/apps.py @@ -43,8 +43,11 @@ class DBMeta(AppConfig): name = "backend.db_meta" def ready(self): - from backend.db_meta.models import ProxyInstance, StorageInstance + from backend.db_meta.models import AppCache, ProxyInstance, StorageInstance from backend.db_meta.signals import update_cluster_status + from backend.db_meta.utils import cache_appcache_data + + post_save.connect(cache_appcache_data, sender=AppCache) post_migrate.connect(init_db_meta, sender=self) # 当实例进行修改或者删除时,更新集群状态 diff --git a/dbm-ui/backend/db_meta/models/app.py b/dbm-ui/backend/db_meta/models/app.py index 86896815f7..1b3d7542ca 100644 --- a/dbm-ui/backend/db_meta/models/app.py +++ b/dbm-ui/backend/db_meta/models/app.py @@ -10,6 +10,7 @@ """ import logging +from django.core.cache import cache from django.db import models from django.utils.translation import ugettext_lazy as _ @@ -94,7 +95,8 @@ def batch_get_app_attr(cls, bk_biz_ids, attr_name="db_app_abbr"): @classmethod def get_choices(cls): try: - biz_choices = [(biz.bk_biz_id, f"[{biz.bk_biz_id}]{biz.bk_biz_name}") for biz in cls.objects.all()] + appcache_data = cache.get("appcache_list") + biz_choices = [(app["bk_biz_id"], f"[{app['bk_biz_id']}]{app['bk_biz_name']}") for app in appcache_data] except Exception: # pylint: disable=broad-except # 忽略出现的异常,此时可能因为表未初始化 biz_choices = [] diff --git a/dbm-ui/backend/db_meta/models/db_module.py b/dbm-ui/backend/db_meta/models/db_module.py index 1904bd138d..18fe08c776 100644 --- a/dbm-ui/backend/db_meta/models/db_module.py +++ b/dbm-ui/backend/db_meta/models/db_module.py @@ -10,6 +10,7 @@ """ import logging +from django.core.cache import cache from django.db import models from django.db.models import Q from django.utils.translation import ugettext_lazy as _ @@ -70,11 +71,14 @@ def get_choices_with_filter(cls, cluster_type=None): for dm in cls.objects.filter(q).all(): try: - app = AppCache.objects.get(bk_biz_id=dm.bk_biz_id) + appcache_dict = cache.get("appcache_dict") + appcache = appcache_dict.get(str(dm.bk_biz_id)) db_module_choices.append( ( dm.db_module_id, - f"[{dm.db_module_id}]-[{dm.cluster_type}]-[app:{app.db_app_abbr}]-{dm.db_module_name}", + f"[{dm.db_module_id}]-" + f"[{dm.cluster_type}]-[app:{appcache['db_app_abbr']}]-" + f"{dm.db_module_name}", ) ) except AppCache.DoesNotExist: diff --git a/dbm-ui/backend/db_meta/utils.py b/dbm-ui/backend/db_meta/utils.py index 77623f35d9..48df07da2f 100644 --- a/dbm-ui/backend/db_meta/utils.py +++ b/dbm-ui/backend/db_meta/utils.py @@ -13,6 +13,7 @@ from collections import defaultdict from django.conf import settings +from django.core.cache import cache from django.utils.translation import ugettext as _ from backend import env @@ -20,6 +21,7 @@ from backend.configuration.constants import DBType from backend.db_meta.enums import ClusterPhase, ClusterType from backend.db_meta.models import ( + AppCache, Cluster, ClusterEntry, Machine, @@ -201,3 +203,11 @@ def clean_cc_topo(bk_biz_id=env.DBA_APP_BK_BIZ_ID): "bk_module_id": bk_module["bk_module_id"], } ) + + +def cache_appcache_data(sender, **kwargs): + data = AppCache.objects.all().values() + appcache_list = list(data) if data else [] + appcache_dict = {app["bk_biz_id"]: app for app in data} + cache.set("appcache_list", appcache_list) + cache.set("appcache_dict", appcache_dict) diff --git a/dbm-ui/backend/db_monitor/views/notice_group.py b/dbm-ui/backend/db_monitor/views/notice_group.py index baef385c90..512f91db24 100644 --- a/dbm-ui/backend/db_monitor/views/notice_group.py +++ b/dbm-ui/backend/db_monitor/views/notice_group.py @@ -83,8 +83,11 @@ def get_action_permission_map(self): def get_serializer_context(self): context = super().get_serializer_context() - notify_groups = MonitorPolicy.objects.exclude(notify_groups=[]).values_list("notify_groups", flat=True) - context["group_used"] = dict(Counter([item for group in notify_groups for item in group])) + context["group_used"] = {} + if "X-Requested-With" in self.request.META: + # 仅在实际API调用时执行查库逻辑。/swagger/文档API忽略查库 + notify_groups = MonitorPolicy.objects.exclude(notify_groups=[]).values_list("notify_groups", flat=True) + context["group_used"] = dict(Counter([item for group in notify_groups for item in group])) return context def get_queryset(self):