from flask import Blueprint, json, jsonify, current_app, request from app.blueprints.incident_gen5.service import ( aggregate_and_sort_incidents, aggregate_line_list, get_logging_list, process_incident_data, ) from app.services.remote_service import DatabricksQuery from app.link_wedata_utils import query_tencent_cloud_data from app.utils import decrypt_code,mask_vin incident_gen5_bp = Blueprint("incident", __name__) @incident_gen5_bp.route("/query", methods=["GET"]) def query(): try: try: query_client = DatabricksQuery() results = query_client.query_table( table_name=current_app.config["GEN5_TABLE_NAME"], # 替换为实际表名 taf_level_two_plus.accident_report_data vin="LE4LG4GB6RLMLFJEH", # LE4LG4GB6RLMLFJEH limit=100, ) # print(999, results) # for row in results: # print(111111, row) # print(type(results)) finally: query_client.stop() # for i in query_client: # print(i['incident_time']) return jsonify({"status": "success", "data": results}), 200 except Exception as e: return jsonify({"status": "error", "message": str(e)}), 500 @incident_gen5_bp.route("/list", methods=["GET","POST"]) def get_gen5_list(): # 验证请求头的信息,通过才继续 encrypted = request.headers.get('X-Encrypted-Timestamp') if not encrypted: return jsonify({"msg": "Parameters are Incorrect"}), 400 encrypt_str=decrypt_code(encrypted) if not encrypt_str: return jsonify({"msg": "Invalid request"}), 400 data = request.get_json() if not data: return jsonify({"code": 400, "data": None, "msg": "请填写有效参数"}), 400 required_fields = ["incident_time", "oneid"] for field in required_fields: if field not in data: return ( jsonify({"code": 400, "data": None, "msg": f"缺少必需字段: {field}"}), 400, ) incident_time = data["incident_time"] # 检查是否为非空列表 if not isinstance(incident_time, list) or len(incident_time) != 2: return ( jsonify( { "code": 400, "data": [], "msg": "incident_time必须是包含两个时间的数组", } ), 400, ) oneid = data["oneid"] if not isinstance(oneid, str) or not oneid.strip(): return jsonify({"code": 400, "data": None, "msg": "oneid必须是非空字符串"}), 400 mask_oneid=mask_vin(oneid) query_client = DatabricksQuery() results = query_client.query_table( table_name=current_app.config["GEN5_TABLE_NAME"], vin=mask_oneid, start_time=incident_time[0], end_time=incident_time[1], # limit=10000, ) # print(11,results) if len(results) == 0: return jsonify({"code": 200, "data": [], "msg": "查询结果为空"}), 200 try: events_list = process_incident_data(results) map_list = aggregate_and_sort_incidents(results) line_list = aggregate_line_list(map_list) logging_list = get_logging_list(map_list) response = { "code": 200, "data": { "events_list": events_list, "map_list":map_list, "line_list": line_list, "logging_list": logging_list, }, "msg": "success", } return jsonify(response) except Exception as e: # 打印错误日志,方便后端排查 print(f"数据处理异常:{str(e)}") # 返回友好的错误信息,前端正常解析 return jsonify({ "code": 500, "data": [], "msg": f"数据处理失败:{str(e)}" }), 200