diff --git a/dbm-ui/backend/db_meta/models/app.py b/dbm-ui/backend/db_meta/models/app.py index 1b3d7542ca..80e2a50567 100644 --- a/dbm-ui/backend/db_meta/models/app.py +++ b/dbm-ui/backend/db_meta/models/app.py @@ -92,10 +92,20 @@ def batch_get_app_attr(cls, bk_biz_ids, attr_name="db_app_abbr"): app_infos = {info["bk_biz_id"]: info[attr_name] for info in infos} return app_infos + @classmethod + def get_appcache(cls, key): + if key not in ["appcache_list", "appcache_dict"]: + raise ValueError(_("缓存key不存在,请检查key是否为appcache_dict/appcache_list")) + if not cache.get(key): + from backend.db_meta.utils import cache_appcache_data + + cache_appcache_data(cls) + return cache.get(key) + @classmethod def get_choices(cls): try: - appcache_data = cache.get("appcache_list") + appcache_data = cls.get_appcache("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 # 忽略出现的异常,此时可能因为表未初始化 diff --git a/dbm-ui/backend/db_meta/models/db_module.py b/dbm-ui/backend/db_meta/models/db_module.py index 18fe08c776..f614abb320 100644 --- a/dbm-ui/backend/db_meta/models/db_module.py +++ b/dbm-ui/backend/db_meta/models/db_module.py @@ -10,7 +10,6 @@ """ 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 _ @@ -71,7 +70,7 @@ def get_choices_with_filter(cls, cluster_type=None): for dm in cls.objects.filter(q).all(): try: - appcache_dict = cache.get("appcache_dict") + appcache_dict = AppCache.get_appcache(key="appcache_dict") appcache = appcache_dict.get(str(dm.bk_biz_id)) db_module_choices.append( ( diff --git a/dbm-ui/backend/db_meta/utils.py b/dbm-ui/backend/db_meta/utils.py index 48df07da2f..e5823e77b6 100644 --- a/dbm-ui/backend/db_meta/utils.py +++ b/dbm-ui/backend/db_meta/utils.py @@ -209,5 +209,7 @@ 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) + # 默认30min过期,稍微晚于周期同步cc拓扑的定时任务(20min) + timeout = 60 * 30 + cache.set("appcache_list", appcache_list, timeout=timeout) + cache.set("appcache_dict", appcache_dict, timeout=timeout) diff --git a/dbm-ui/backend/db_services/dbresource/views/resource.py b/dbm-ui/backend/db_services/dbresource/views/resource.py index 795adb135b..d1ea8e5a37 100644 --- a/dbm-ui/backend/db_services/dbresource/views/resource.py +++ b/dbm-ui/backend/db_services/dbresource/views/resource.py @@ -318,10 +318,15 @@ def get_subzones(self, request): def get_device_class(self, request): data = self.params_validate(self.get_serializer_class()) page = {"start": data["offset"], "limit": data["limit"]} - # 默认过滤条件是支持申请的机型 - dvc_filter = {"condition": "AND", "rules": [{"field": "enable_apply", "operator": "equal", "value": True}]} + # 默认过滤条件是支持申请的机型,且是常规项目 + rules = [ + {"field": "enable_apply", "operator": "equal", "value": True}, + {"field": "require_type", "operator": "equal", "value": 1}, + ] + # 如果有名称过滤,则加上 if data.get("name"): - dvc_filter["rules"].append({"field": "device_type", "operator": "contains", "value": data["name"]}) + rules.append({"field": "device_type", "operator": "contains", "value": data["name"]}) + dvc_filter = {"condition": "AND", "rules": rules} # 请求hcm平台获取机型列表,若报错则忽略 try: device_list = HCMApi.list_cvm_device(params={"filter": dvc_filter, "page": page})