|
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.enums import ClusterType |
| 20 | +from backend.db_meta.models import AppCache, Cluster, DBModule |
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 |
@@ -221,3 +224,70 @@ def get_or_create_set_with_name(bk_biz_id: int, bk_set_name: str) -> int: |
221 | 224 | raise err |
222 | 225 | else: |
223 | 226 | return bk_set_id |
| 227 | + |
| 228 | + |
| 229 | +def filter_by_biz_name(data: list, biz_name: str) -> list: |
| 230 | + return [biz for biz in data if biz["bk_biz_name"] == biz_name] |
| 231 | + |
| 232 | + |
| 233 | +def filter_by_module_name(data: list, module_name: str) -> list: |
| 234 | + result = [] |
| 235 | + for biz in data: |
| 236 | + filtered_modules = [module for module in biz["modules"] if module["module_name"] == module_name] |
| 237 | + if filtered_modules: |
| 238 | + new_biz = biz.copy() |
| 239 | + new_biz["modules"] = filtered_modules |
| 240 | + result.append(new_biz) |
| 241 | + return result |
| 242 | + |
| 243 | + |
| 244 | +def list_biz_module_trees(cluster_type: str, bk_biz_name: str, module_name: str) -> List[Dict]: |
| 245 | + """ |
| 246 | + 获取业务与模块维度集群数量 |
| 247 | + """ |
| 248 | + if cluster_type in [ClusterType.TenDBSingle, ClusterType.TenDBHA]: |
| 249 | + clusters = ( |
| 250 | + Cluster.objects.filter(cluster_type__in=[ClusterType.TenDBSingle, ClusterType.TenDBHA]) |
| 251 | + .values("db_module_id", "bk_biz_id") |
| 252 | + .annotate(count=Count("db_module_id")) |
| 253 | + .order_by("-count") |
| 254 | + ) |
| 255 | + else: |
| 256 | + clusters = ( |
| 257 | + Cluster.objects.filter(cluster_type=cluster_type) |
| 258 | + .values("db_module_id", "bk_biz_id") |
| 259 | + .annotate(count=Count("db_module_id")) |
| 260 | + .order_by("-count") |
| 261 | + ) |
| 262 | + |
| 263 | + db_module_map = DBModule.db_module_map() |
| 264 | + id_to_name = AppCache.id_to_name() |
| 265 | + |
| 266 | + nested_data = collections.defaultdict(lambda: {"count": 0, "modules": collections.defaultdict(int)}) |
| 267 | + for cluster in clusters: |
| 268 | + bk_biz_id = cluster["bk_biz_id"] |
| 269 | + db_module_id = cluster["db_module_id"] |
| 270 | + count = cluster["count"] |
| 271 | + nested_data[bk_biz_id]["count"] += count |
| 272 | + nested_data[bk_biz_id]["modules"][db_module_id] = count |
| 273 | + |
| 274 | + final_data = [] |
| 275 | + for bk_biz_id, data in nested_data.items(): |
| 276 | + modules = [ |
| 277 | + {"module_name": db_module_map.get(module_id), "module_id": module_id, "count": count} |
| 278 | + for module_id, count in data["modules"].items() |
| 279 | + ] |
| 280 | + final_data.append( |
| 281 | + { |
| 282 | + "bk_biz_name": id_to_name.get(bk_biz_id), |
| 283 | + "bk_biz_id": bk_biz_id, |
| 284 | + "count": data["count"], |
| 285 | + "modules": modules, |
| 286 | + } |
| 287 | + ) |
| 288 | + |
| 289 | + if bk_biz_name: |
| 290 | + final_data = filter_by_biz_name(final_data, bk_biz_name) |
| 291 | + if module_name: |
| 292 | + final_data = filter_by_module_name(final_data, module_name) |
| 293 | + return final_data |
0 commit comments