|
12 | 12 | import logging
|
13 | 13 | from typing import Dict, List
|
14 | 14 |
|
| 15 | +from django.db.models import Count |
| 16 | + |
15 | 17 | from backend.components import CCApi
|
16 | 18 | from backend.components.dbconfig.constants import DEPLOY_FILE_NAME, ConfType, LevelName
|
17 |
| -from backend.db_meta.models import AppCache, DBModule |
| 19 | +from backend.db_meta.models import AppCache, Cluster, DBModule, Machine, ProxyInstance, StorageInstance |
| 20 | +from backend.db_services.cmdb.constants import ModuleCountType |
18 | 21 | from backend.db_services.cmdb.exceptions import BkAppAttrAlreadyExistException
|
19 | 22 | from backend.db_services.dbconfig.dataclass import DBBaseConfig, DBConfigLevelData
|
20 | 23 | from backend.db_services.dbconfig.handlers import DBConfigHandler
|
@@ -222,3 +225,85 @@ def get_or_create_set_with_name(bk_biz_id: int, bk_set_name: str) -> int:
|
222 | 225 | raise err
|
223 | 226 | else:
|
224 | 227 | return bk_set_id
|
| 228 | + |
| 229 | + |
| 230 | +def filter_by_biz_name(data: list, biz_name: str) -> list: |
| 231 | + return [biz for biz in data if biz["bk_biz_name"] == biz_name] |
| 232 | + |
| 233 | + |
| 234 | +def filter_by_module_name(data: list, module_name: str) -> list: |
| 235 | + result = [] |
| 236 | + for biz in data: |
| 237 | + filtered_modules = [module for module in biz["modules"] if module["module_name"] == module_name] |
| 238 | + if filtered_modules: |
| 239 | + new_biz = biz.copy() |
| 240 | + new_biz["modules"] = filtered_modules |
| 241 | + result.append(new_biz) |
| 242 | + return result |
| 243 | + |
| 244 | + |
| 245 | +def process_count_obj(queryset, nested_data): |
| 246 | + count_obj = queryset.values("db_module_id", "bk_biz_id").annotate(count=Count("db_module_id")).order_by("-count") |
| 247 | + for obj in count_obj: |
| 248 | + bk_biz_id = obj["bk_biz_id"] |
| 249 | + db_module_id = obj["db_module_id"] |
| 250 | + count = obj["count"] |
| 251 | + nested_data[bk_biz_id]["count"] += count |
| 252 | + nested_data[bk_biz_id]["modules"][db_module_id] = count |
| 253 | + |
| 254 | + |
| 255 | +def list_biz_module_trees( |
| 256 | + cluster_types: str, bk_biz_name: str, module_name: str, count_type: str, instance_inner_role: str |
| 257 | +) -> List[Dict]: |
| 258 | + """ |
| 259 | + 获取业务与模块维度集群数量 |
| 260 | + :param cluster_types: 以逗号分隔的集群类型字符串 |
| 261 | + :param bk_biz_name: 业务名称 |
| 262 | + :param module_name: 模块名称 |
| 263 | + :param count_type: 计数类型(如 "cluster" 或 "instance") |
| 264 | + :param instance_inner_role: 是否过滤为后端主实例 |
| 265 | + :return: 包含业务和模块维度的计数列表 |
| 266 | + """ |
| 267 | + cluster_type_list = cluster_types.split(",") |
| 268 | + |
| 269 | + if count_type == ModuleCountType.CLUSTER.value: |
| 270 | + queryset = Cluster.objects.filter(cluster_type__in=cluster_type_list) |
| 271 | + elif count_type == ModuleCountType.INSTANCE.value: |
| 272 | + filters = {"cluster_type__in": cluster_type_list} |
| 273 | + if instance_inner_role: |
| 274 | + filters["instance_inner_role"] = instance_inner_role |
| 275 | + queryset = StorageInstance.objects.filter(**filters) |
| 276 | + elif count_type == ModuleCountType.MACHINE.value: |
| 277 | + queryset = Machine.objects.filter(cluster_type__in=cluster_type_list) |
| 278 | + else: |
| 279 | + raise ValueError(f"Unsupported count_type: {count_type}") |
| 280 | + |
| 281 | + nested_data = collections.defaultdict(lambda: {"count": 0, "modules": collections.defaultdict(int)}) |
| 282 | + process_count_obj(queryset, nested_data) |
| 283 | + |
| 284 | + if count_type == ModuleCountType.INSTANCE.value: |
| 285 | + proxy_queryset = ProxyInstance.objects.filter(cluster_type__in=cluster_type_list) |
| 286 | + process_count_obj(proxy_queryset, nested_data) |
| 287 | + |
| 288 | + db_module_map = DBModule.db_module_map() |
| 289 | + id_to_name = AppCache.id_to_name() |
| 290 | + |
| 291 | + final_data = [ |
| 292 | + { |
| 293 | + "bk_biz_name": id_to_name.get(bk_biz_id, ""), |
| 294 | + "bk_biz_id": bk_biz_id, |
| 295 | + "count": data["count"], |
| 296 | + "modules": [ |
| 297 | + {"module_name": db_module_map.get(module_id, ""), "module_id": module_id, "count": count} |
| 298 | + for module_id, count in data["modules"].items() |
| 299 | + ], |
| 300 | + } |
| 301 | + for bk_biz_id, data in nested_data.items() |
| 302 | + ] |
| 303 | + |
| 304 | + if bk_biz_name: |
| 305 | + final_data = filter_by_biz_name(final_data, bk_biz_name) |
| 306 | + if module_name: |
| 307 | + final_data = filter_by_module_name(final_data, module_name) |
| 308 | + |
| 309 | + return final_data |
0 commit comments