2024-11-23 17:50:32 +08:00
|
|
|
|
# flask_app/routes/get_deviation.py
|
2025-01-14 17:10:38 +08:00
|
|
|
|
import json
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
2025-01-09 15:57:35 +08:00
|
|
|
|
from flask import Blueprint, Response, g
|
2024-11-23 17:50:32 +08:00
|
|
|
|
import os
|
2024-12-05 15:01:37 +08:00
|
|
|
|
from flask_app.general.format_change import download_file
|
2024-12-16 13:56:28 +08:00
|
|
|
|
from flask_app.routes.偏离表main import get_tech_and_business_deviation
|
2025-01-14 17:10:38 +08:00
|
|
|
|
from flask_app.routes.utils import generate_deviation_response, validate_and_setup_logger, create_response, sse_format, \
|
|
|
|
|
log_error_unique_id
|
2024-11-25 16:04:53 +08:00
|
|
|
|
from flask_app.ConnectionLimiter import require_connection_limit
|
2024-11-23 17:50:32 +08:00
|
|
|
|
get_deviation_bp = Blueprint('get_deviation', __name__)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
@get_deviation_bp.route('/get_deviation', methods=['POST'])
|
2024-11-26 11:32:24 +08:00
|
|
|
|
@validate_and_setup_logger
|
2025-02-11 12:16:53 +08:00
|
|
|
|
@require_connection_limit(timeout=1800)
|
2024-11-23 17:50:32 +08:00
|
|
|
|
def get_deviation():
|
|
|
|
|
logger = g.logger
|
|
|
|
|
unique_id = g.unique_id
|
2024-12-02 11:10:49 +08:00
|
|
|
|
file_url = g.file_url
|
|
|
|
|
zb_type = g.zb_type
|
2025-01-09 14:59:28 +08:00
|
|
|
|
output_folder = g.output_folder
|
|
|
|
|
def generate():
|
|
|
|
|
try:
|
|
|
|
|
logger.info("call /get_deviation: 开始解析 URL: " + file_url)
|
2025-01-14 17:10:38 +08:00
|
|
|
|
if zb_type not in [1, 2, 3]:
|
2025-01-09 14:59:28 +08:00
|
|
|
|
logger.error(f"无效的 zb_type: {zb_type}. 期望 zb_type: 1 或 2")
|
2025-01-14 17:10:38 +08:00
|
|
|
|
log_error_unique_id(unique_id, 3)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
response = create_response(
|
|
|
|
|
message='此端点仅支持 zb_type 1 或 2',
|
|
|
|
|
status='error',
|
|
|
|
|
data=''
|
|
|
|
|
)
|
2025-01-09 15:32:04 +08:00
|
|
|
|
yield sse_format(response)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
return # 终止生成器
|
2025-01-14 17:10:38 +08:00
|
|
|
|
# 映射 zb_type,如果是 3 则映射为 2
|
|
|
|
|
mapped_zb_type = 2 if zb_type == 3 else zb_type
|
2024-12-27 16:14:42 +08:00
|
|
|
|
# 直接下载并处理文件
|
|
|
|
|
filename = "ztbfile"
|
|
|
|
|
downloaded_filename = os.path.join(output_folder, filename)
|
|
|
|
|
# 下载文件
|
|
|
|
|
downloaded_filepath, file_type = download_file(file_url, downloaded_filename)
|
|
|
|
|
if downloaded_filepath is None or file_type == 4:
|
2025-01-09 14:59:28 +08:00
|
|
|
|
logger.error("下载地址不存在或不支持的文件类型!")
|
2025-01-14 17:10:38 +08:00
|
|
|
|
log_error_unique_id(unique_id, 3)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
response = create_response(
|
|
|
|
|
message='下载地址不存在或不支持的文件类型!',
|
|
|
|
|
status='error',
|
|
|
|
|
data=''
|
|
|
|
|
)
|
2025-01-09 15:32:04 +08:00
|
|
|
|
yield sse_format(response)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
return # 终止生成器
|
2024-12-27 16:14:42 +08:00
|
|
|
|
|
|
|
|
|
logger.info("Local file path: " + downloaded_filepath)
|
|
|
|
|
|
|
|
|
|
# 处理文件
|
2025-01-09 14:59:28 +08:00
|
|
|
|
deviations = get_tech_and_business_deviation(
|
2025-01-14 17:10:38 +08:00
|
|
|
|
downloaded_filepath, file_type, unique_id, output_folder, mapped_zb_type
|
2025-01-09 14:59:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if deviations is None:
|
2025-01-14 17:10:38 +08:00
|
|
|
|
logger.info(f"服务器内部出错!")
|
|
|
|
|
log_error_unique_id(unique_id, 3)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
response = create_response(
|
2025-01-14 17:10:38 +08:00
|
|
|
|
message='服务器内部出错!',
|
2025-01-09 14:59:28 +08:00
|
|
|
|
status='error',
|
|
|
|
|
data=''
|
|
|
|
|
)
|
2025-01-09 15:32:04 +08:00
|
|
|
|
yield sse_format(response)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
return # 终止生成器
|
|
|
|
|
|
|
|
|
|
# 解包返回值
|
|
|
|
|
(tech_deviation, tech_star_deviation, business_deviation,
|
2025-01-14 17:10:38 +08:00
|
|
|
|
business_star_deviation, zigefuhe_deviation, proof_materials,technical_standards) = deviations
|
2024-12-27 16:14:42 +08:00
|
|
|
|
|
|
|
|
|
# 生成偏差响应
|
2025-01-09 14:59:28 +08:00
|
|
|
|
tech_deviation_response, tech_deviation_star_response, zigefuhe_deviation_response, \
|
|
|
|
|
shangwu_deviation_response, shangwu_star_deviation_response, proof_materials_response = generate_deviation_response(
|
|
|
|
|
tech_deviation, tech_star_deviation, business_deviation,
|
|
|
|
|
business_star_deviation, zigefuhe_deviation, proof_materials, logger
|
|
|
|
|
)
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
final_response = {
|
|
|
|
|
'message': 'processed successfully',
|
2025-01-09 14:59:28 +08:00
|
|
|
|
'status': 'success',
|
2024-11-23 17:50:32 +08:00
|
|
|
|
'data': 'END'
|
|
|
|
|
}
|
2025-01-14 17:10:38 +08:00
|
|
|
|
# logger.info(f"技术评分: {json.dumps(technical_standards, ensure_ascii=False, indent=4)}")
|
2024-12-27 16:14:42 +08:00
|
|
|
|
|
2025-01-14 17:10:38 +08:00
|
|
|
|
technical_standards_response = {
|
|
|
|
|
'message': 'tech_evaluation',
|
|
|
|
|
'status': 'success',
|
|
|
|
|
'data': json.dumps(technical_standards, ensure_ascii=False)
|
|
|
|
|
}
|
2024-12-27 16:14:42 +08:00
|
|
|
|
# 流式返回数据
|
2025-01-09 15:32:04 +08:00
|
|
|
|
yield sse_format(tech_deviation_response)
|
|
|
|
|
yield sse_format(tech_deviation_star_response)
|
|
|
|
|
yield sse_format(zigefuhe_deviation_response)
|
|
|
|
|
yield sse_format(shangwu_deviation_response)
|
|
|
|
|
yield sse_format(shangwu_star_deviation_response)
|
|
|
|
|
yield sse_format(proof_materials_response)
|
2025-01-14 17:10:38 +08:00
|
|
|
|
# yield sse_format(technical_standards_response)
|
2025-01-09 15:32:04 +08:00
|
|
|
|
yield sse_format(final_response)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-01-14 17:10:38 +08:00
|
|
|
|
logger.error('服务器内部发生异常: ' + str(e))
|
|
|
|
|
log_error_unique_id(unique_id, 3)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
response = create_response(
|
|
|
|
|
message=str(e),
|
|
|
|
|
status='error',
|
|
|
|
|
data=''
|
|
|
|
|
)
|
2025-01-09 15:32:04 +08:00
|
|
|
|
yield sse_format(response)
|
2024-12-27 16:14:42 +08:00
|
|
|
|
|
2025-01-09 14:59:28 +08:00
|
|
|
|
return Response(generate(), mimetype='text/event-stream')
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
|