加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
module_relationship_analyzer.py 6.31 KB
一键复制 编辑 原始数据 按行查看 历史
# -*- coding: utf-8 -*-
import requests
import json
import os
import pandas as pd
from concurrent.futures import ThreadPoolExecutor, as_completed
import logging
# 设置日志配置
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 定义API的URL
search_business_url = "http://weops.topsports.lan/api/c/compapi/v2/cc/search_business/"
search_module_url = "http://weops.topsports.lan/api/c/compapi/v2/cc/search_module/"
search_association_url = "http://weops.topsports.lan/api/c/compapi/v2/cc/search_related_inst_asso/"
# 请求体模板
business_payload_template = {
"bk_app_secret": "f1ccda90-0d79-45d4-8aaa-d50b233bf73c",
"bk_app_code": "bk_itsm",
"bk_username": "admin",
"fields": ["bk_biz_name", "bk_biz_id"]
}
module_payload_template = {
"bk_app_secret": "f1ccda90-0d79-45d4-8aaa-d50b233bf73c",
"bk_app_code": "bk_itsm",
"bk_username": "admin",
"bk_biz_id": 0,
"fields": ["bk_module_id", "bk_module_name"]
}
association_payload_template = {
"bk_app_secret": "f1ccda90-0d79-45d4-8aaa-d50b233bf73c",
"bk_app_code": "bk_itsm",
"bk_username": "admin",
"condition": {
"bk_inst_id": 0,
"bk_obj_id": "module"
},
"fields": ["id", "bk_inst_id", "bk_asst_inst_id", "bk_asst_obj_id"]
}
headers = {"Content-Type": "application/json"}
excluded_module_names = ["idle host", "fault host", "recycle host"]
def fetch_all_business_ids():
try:
response = requests.post(search_business_url, headers=headers, data=json.dumps(business_payload_template), timeout=10)
response.raise_for_status() # 检查请求是否成功
return response.json().get("data", {}).get("info", [])
except requests.RequestException as e:
logging.error(f"Failed to fetch business IDs: {e}")
return []
def fetch_modules_by_business_id(business_id):
payload = module_payload_template.copy()
payload["bk_biz_id"] = business_id
try:
response = requests.post(search_module_url, headers=headers, data=json.dumps(payload), timeout=10)
response.raise_for_status()
return [module for module in response.json().get("data", {}).get("info", []) if module['bk_module_name'] not in excluded_module_names]
except requests.RequestException as e:
logging.error(f"Failed to fetch modules for business ID {business_id}: {e}")
return []
def fetch_associations_by_module_id(module_id):
payload = association_payload_template.copy()
payload["condition"]["bk_inst_id"] = module_id
try:
response = requests.post(search_association_url, headers=headers, data=json.dumps(payload), timeout=10)
response.raise_for_status()
return response.json().get("data", [])
except requests.RequestException as e:
logging.error(f"Failed to fetch associations for module ID {module_id}: {e}")
return []
def save_to_json(directory, filename, data):
try:
if not os.path.exists(directory):
os.makedirs(directory)
with open(os.path.join(directory, filename), 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
logging.info(f"Data saved to {filename}")
except Exception as e:
logging.error(f"Failed to save data to JSON: {e}")
def save_to_excel(directory, filename, data):
try:
if not os.path.exists(directory):
os.makedirs(directory)
df = pd.DataFrame(data)
df.to_excel(os.path.join(directory, filename), index=False)
logging.info(f"Data saved to {filename}")
except Exception as e:
logging.error(f"Failed to save data to Excel: {e}")
if __name__ == '__main__':
# 获取所有业务ID
business_ids = fetch_all_business_ids()
# 使用线程池获取每个业务的模块及其关联关系
all_associations = []
with ThreadPoolExecutor(max_workers=16) as business_executor: # 增加线程数提高并发
future_to_biz = {business_executor.submit(fetch_modules_by_business_id, biz['bk_biz_id']): biz for biz in business_ids}
for future in as_completed(future_to_biz):
biz = future_to_biz[future]
try:
modules = future.result()
with ThreadPoolExecutor(max_workers=10) as module_executor: # 模块级别的线程池
future_to_module = {module_executor.submit(fetch_associations_by_module_id, module['bk_module_id']): module for module in modules}
for module_future in as_completed(future_to_module):
module = future_to_module[module_future]
try:
associations = module_future.result()
module_associations = [
assoc for assoc in associations if assoc['bk_asst_obj_id'] == "module" and assoc['bk_inst_id'] != assoc['bk_asst_inst_id']
]
if module_associations:
all_associations.append({
"business_id": biz['bk_biz_id'],
"business_name": biz['bk_biz_name'],
"module_id": module['bk_module_id'],
"module_name": module['bk_module_name'],
"associations": module_associations
})
except Exception as exc:
logging.error(f"Error processing associations for module {module['bk_module_name']}: {exc}")
except Exception as exc:
logging.error(f"Error processing business {biz['bk_biz_name']}: {exc}")
# 保存结果到 JSON 和 Excel
save_to_json('target', 'filtered_associations.json', all_associations)
flattened_data = [
{
"业务ID": assoc["business_id"],
"业务名称": assoc["business_name"],
"模块ID": assoc["module_id"],
"模块名称": assoc["module_name"],
"关联模块ID": rel["bk_asst_inst_id"],
"关联ID": rel["id"]
}
for assoc in all_associations for rel in assoc["associations"]
]
save_to_excel('target', 'filtered_associations.xlsx', flattened_data)
logging.info(f"Processed {len(all_associations)} module associations.")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化