|
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 import env
|
16 | 18 | from backend.components import CCApi
|
17 | 19 | from backend.components.dbconfig.constants import DEPLOY_FILE_NAME, ConfType, LevelName
|
18 | 20 | from backend.configuration.constants import SystemSettingsEnum
|
19 | 21 | from backend.configuration.models import SystemSettings
|
20 |
| -from backend.db_meta.models import AppCache, DBModule |
| 22 | +from backend.db_meta.models import AppCache, Cluster, DBModule, Machine, ProxyInstance, StorageInstance |
| 23 | +from backend.db_services.cmdb.constants import ModuleCountType |
21 | 24 | from backend.db_services.cmdb.exceptions import BkAppAttrAlreadyExistException
|
22 | 25 | from backend.db_services.dbconfig.dataclass import DBBaseConfig, DBConfigLevelData
|
23 | 26 | from backend.db_services.dbconfig.handlers import DBConfigHandler
|
@@ -241,3 +244,88 @@ def get_or_create_resource_module():
|
241 | 244 | topo = {"set_id": manage_set_id, "resource_module_id": module_id}
|
242 | 245 | SystemSettings.insert_setting_value(key=SystemSettingsEnum.MANAGE_TOPO.value, value=topo, value_type="dict")
|
243 | 246 | return module_id
|
| 247 | + |
| 248 | + |
| 249 | +def filter_by_biz_name(data: list, biz_name: str) -> list: |
| 250 | + return [biz for biz in data if biz["bk_biz_name"] == biz_name] |
| 251 | + |
| 252 | + |
| 253 | +def filter_by_module_name(data: list, module_name: str) -> list: |
| 254 | + result = [] |
| 255 | + for biz in data: |
| 256 | + filtered_modules = [module for module in biz["modules"] if module["module_name"] == module_name] |
| 257 | + if filtered_modules: |
| 258 | + new_biz = biz.copy() |
| 259 | + new_biz["modules"] = filtered_modules |
| 260 | + result.append(new_biz) |
| 261 | + return result |
| 262 | + |
| 263 | + |
| 264 | +def process_count_obj(queryset, nested_data): |
| 265 | + count_obj = queryset.values("db_module_id", "bk_biz_id").annotate(count=Count("db_module_id")).order_by("-count") |
| 266 | + for obj in count_obj: |
| 267 | + bk_biz_id = obj["bk_biz_id"] |
| 268 | + db_module_id = obj["db_module_id"] |
| 269 | + count = obj["count"] |
| 270 | + nested_data[bk_biz_id]["count"] += count |
| 271 | + nested_data[bk_biz_id]["modules"][db_module_id] = count |
| 272 | + |
| 273 | + |
| 274 | +def list_biz_module_trees( |
| 275 | + cluster_types: str, bk_biz_name: str, module_name: str, count_type: str, role: str |
| 276 | +) -> List[Dict]: |
| 277 | + """ |
| 278 | + 获取业务与模块维度集群数量 |
| 279 | + :param cluster_types: 以逗号分隔的集群类型字符串 |
| 280 | + :param bk_biz_name: 业务名称 |
| 281 | + :param module_name: 模块名称 |
| 282 | + :param count_type: 计数类型(如 "cluster" 或 "instance") |
| 283 | + :param role: 是否过滤为实例角色 |
| 284 | + :return: 包含业务和模块维度的计数列表 |
| 285 | + """ |
| 286 | + cluster_type_list = cluster_types.split(",") |
| 287 | + |
| 288 | + if count_type == ModuleCountType.CLUSTER.value: |
| 289 | + queryset = Cluster.objects.filter(cluster_type__in=cluster_type_list) |
| 290 | + elif count_type == ModuleCountType.INSTANCE.value: |
| 291 | + filters = {"cluster_type__in": cluster_type_list} |
| 292 | + if role: |
| 293 | + filters["instance_role"] = role |
| 294 | + queryset = StorageInstance.objects.filter(**filters) |
| 295 | + elif count_type == ModuleCountType.MACHINE.value: |
| 296 | + queryset = Machine.objects.filter(cluster_type__in=cluster_type_list) |
| 297 | + else: |
| 298 | + raise ValueError(f"Unsupported count_type: {count_type}") |
| 299 | + |
| 300 | + nested_data = collections.defaultdict(lambda: {"count": 0, "modules": collections.defaultdict(int)}) |
| 301 | + process_count_obj(queryset, nested_data) |
| 302 | + |
| 303 | + if count_type == ModuleCountType.INSTANCE.value: |
| 304 | + filters = {"cluster_type__in": cluster_type_list} |
| 305 | + if role: |
| 306 | + filters["access_layer"] = role |
| 307 | + proxy_queryset = ProxyInstance.objects.filter(**filters) |
| 308 | + process_count_obj(proxy_queryset, nested_data) |
| 309 | + |
| 310 | + db_module_map = DBModule.db_module_map() |
| 311 | + id_to_name = AppCache.id_to_name() |
| 312 | + |
| 313 | + final_data = [ |
| 314 | + { |
| 315 | + "bk_biz_name": id_to_name.get(bk_biz_id, ""), |
| 316 | + "bk_biz_id": bk_biz_id, |
| 317 | + "count": data["count"], |
| 318 | + "modules": [ |
| 319 | + {"module_name": db_module_map.get(module_id, ""), "module_id": module_id, "count": count} |
| 320 | + for module_id, count in data["modules"].items() |
| 321 | + ], |
| 322 | + } |
| 323 | + for bk_biz_id, data in nested_data.items() |
| 324 | + ] |
| 325 | + |
| 326 | + if bk_biz_name: |
| 327 | + final_data = filter_by_biz_name(final_data, bk_biz_name) |
| 328 | + if module_name: |
| 329 | + final_data = filter_by_module_name(final_data, module_name) |
| 330 | + |
| 331 | + return final_data |
0 commit comments