加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
delete_module_associations.py 5.14 KB
一键复制 编辑 原始数据 按行查看 历史
Jay Kim 提交于 2024-11-14 16:47 . 优化性能,加上锁机制
import requests
import json
import logging
import os
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# 配置蓝鲸应用的通用参数
APP_SECRET = "f1ccda90-0d79-45d4-8aaa-d50b233bf73c"
APP_CODE = "bk_itsm"
BK_USERNAME = "admin"
DOMAIN = "http://weops.topsports.lan" # 蓝鲸平台域名
DELETE_ASSOCIATION_URL = f"{DOMAIN}/api/c/compapi/v2/cc/delete_instance_association/"
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 检查并创建目标目录
target_dir = "target"
if not os.path.exists(target_dir):
os.makedirs(target_dir)
# 创建Session和连接池
session = requests.Session()
adapter = HTTPAdapter(max_retries=Retry(total=3, backoff_factor=1, status_forcelist=[500, 502, 503, 504]))
session.mount("http://", adapter)
session.mount("https://", adapter)
# 初始化锁
lock = threading.Lock()
# 删除关联关系函数
def delete_association(caller_id, callee_id, assoc_id, bk_obj_id="module"):
"""
删除指定模块之间的关联关系。
:param caller_id: 调用模块的 ID
:param callee_id: 被调用模块的 ID
:param assoc_id: 关联关系的唯一 ID
:param bk_obj_id: 对象类型 ID(通常为 "module")
:return: 返回 (caller_id, callee_id, assoc_id, 删除成功) 的元组
"""
payload = {
"bk_app_code": APP_CODE,
"bk_app_secret": APP_SECRET,
"bk_username": BK_USERNAME,
"bk_obj_id": bk_obj_id,
"bk_inst_id": caller_id,
"bk_asst_inst_id": callee_id,
"id": assoc_id
}
try:
response = session.post(DELETE_ASSOCIATION_URL, headers={"Content-Type": "application/json"}, json=payload)
response.raise_for_status()
result = response.json()
if isinstance(result, dict) and result.get("result", False):
logging.info(f"成功删除关联关系: {assoc_id} (调用模块ID: {caller_id}, 被调用模块ID: {callee_id})")
return caller_id, callee_id, assoc_id, True
else:
logging.error(f"删除关联关系失败: {result.get('message', '未知错误')}")
return caller_id, callee_id, assoc_id, False
except requests.RequestException as e:
logging.error(f"请求失败: {e}")
return caller_id, callee_id, assoc_id, False
# 从文件中读取关联关系
def load_associations(file_path):
"""
从 JSON 文件中读取关联关系数据。
:param file_path: JSON 文件的路径
:return: 关联关系列表
"""
if not os.path.exists(file_path):
logging.error(f"文件不存在: {file_path}")
return []
try:
with open(file_path, 'r', encoding="utf-8") as file:
data = json.load(file)
logging.info("成功加载关联关系数据")
return data
except json.JSONDecodeError as e:
logging.error(f"文件解析错误: {e}")
return []
# 执行删除操作并转换格式
def delete_associations_and_convert_format(input_file, output_file):
"""
从文件读取关联关系,执行删除操作,并将结果转换成目标格式写入另一个文件。
:param input_file: 输入 JSON 文件路径
:param output_file: 输出 JSON 文件路径
"""
associations = load_associations(input_file)
results = []
# 使用多线程执行删除操作
with ThreadPoolExecutor(max_workers=10) as executor:
futures = []
for assoc in associations:
caller_id = assoc.get("caller_module_id")
callee_id = assoc.get("callee_module_id")
assoc_id = assoc.get("common_id")
if caller_id and callee_id and assoc_id:
futures.append(executor.submit(delete_association, caller_id, callee_id, assoc_id))
else:
logging.warning(f"跳过无效数据: {assoc}")
results.append({
"caller_obj_id": caller_id,
"callee_obj_id": callee_id,
"id": assoc_id,
"deleted": False
})
for future in as_completed(futures):
caller_id, callee_id, assoc_id, success = future.result()
# 加锁确保多线程下的写入安全
with lock:
results.append({
"caller_obj_id": caller_id,
"callee_obj_id": callee_id,
"id": assoc_id,
"deleted": success
})
# 将转换后的结果写入输出文件
try:
with open(output_file, 'w', encoding="utf-8") as file:
json.dump(results, file, ensure_ascii=False, indent=4)
logging.info(f"删除结果已保存到 {output_file}")
except IOError as e:
logging.error(f"无法写入文件 {output_file}: {e}")
# 示例用法
if __name__ == "__main__":
input_file_path = "src/module_associations.json" # 输入文件路径
output_file_path = os.path.join(target_dir, "delete_results.json") # 输出文件路径
delete_associations_and_convert_format(input_file_path, output_file_path)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化