From 7eba15db91d5f62515ea1124f5f5ba3b425c9fbe Mon Sep 17 00:00:00 2001 From: wangxu <1318272526@qq.com> Date: Sat, 13 Dec 2025 14:44:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9pb=E5=AF=B9=E8=B1=A1=E4=B8=AD?= =?UTF-8?q?=E5=81=B6=E5=B0=94=E5=87=BA=E7=8E=B0=E7=9A=84=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=80=BC=E4=B8=BANan=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common_worker.py | 21 ++++++++++++++++++++- app/cross_compare_worker.py | 4 ++-- app/cross_evaluate_worker.py | 8 ++++---- app/cross_monitor_worker.py | 8 ++++---- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/common_worker.py b/app/common_worker.py index a6cc134..3b85023 100644 --- a/app/common_worker.py +++ b/app/common_worker.py @@ -465,4 +465,23 @@ def gen_ten_weeks_ago_data_list_with_weekdays(date_type='week'): # 大 list:从远到近(最早一周在最前面) result = [week_buckets[k] for k in sorted(week_buckets, reverse=False)] - return result \ No newline at end of file + return result + + +def clean_dict_nan(obj, default_value=None): + """ + 递归清理 dict/list 中的 NaN/Infinity + Args: + obj: 任意 Python 对象 + default_value: NaN 的替换值(None 会转为 JSON null) + """ + if isinstance(obj, dict): + return {k: clean_dict_nan(v, default_value) for k, v in obj.items()} + + if isinstance(obj, list): + return [clean_dict_nan(item, default_value) for item in obj] + + if isinstance(obj, float) and (math.isnan(obj) or math.isinf(obj)): + return default_value + + return obj \ No newline at end of file diff --git a/app/cross_compare_worker.py b/app/cross_compare_worker.py index 294119e..bf6dc65 100644 --- a/app/cross_compare_worker.py +++ b/app/cross_compare_worker.py @@ -94,7 +94,7 @@ def query_compare_data(params): if export == 1: return query_compare_data_export_excel(compared_inroad_delay_infos, cross_static_info['name'],compare_date_list,date_list,time_range) - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) def do_add_cross_survey_job(params): @@ -452,7 +452,7 @@ def query_cross_survey_result(params): 'ledger_info': cross_ledger_info, 'can_rerun_dir': dir_info } - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) def rerun_cross_survey_dir(params): diff --git a/app/cross_evaluate_worker.py b/app/cross_evaluate_worker.py index ef519b2..0b406bf 100644 --- a/app/cross_evaluate_worker.py +++ b/app/cross_evaluate_worker.py @@ -164,7 +164,7 @@ def query_cross_delay_info_controller(params): if excel == 1: return query_cross_delay_info_controller_export_excel(road_flow_delay_infos, road_flow_turn_rate) - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) # 问题诊断接口 @@ -224,7 +224,7 @@ def query_cross_problems(params): problems = gen_cross_problems(crossid, nodeid, area_id, time_range, str(tp_start).replace('h', '').replace('t', ''), date_list, avg_cross_delay_info, roads_dir_dict, inroad_static_info_dict, cross_phase, is_peak, cross_ledger_info, weekdays) res = make_common_res(0, 'ok') res['data'] = problems - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) # 指标变化趋势接口 @@ -318,7 +318,7 @@ def query_cross_index_trend_controller(params): 'days_data': days_data_with_change_rate, 'weeks_data': weeks_data_with_change_rate } - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) def query_cross_examine_records_detail(params): @@ -410,7 +410,7 @@ def query_cross_examine_records_detail(params): 'first_date': first_date, 'time_range': tp_start + '-' + tp_end if tp_start != '' else '', } - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) def update_cross_examine_record_state(params): diff --git a/app/cross_monitor_worker.py b/app/cross_monitor_worker.py index 903cee6..84d4a86 100644 --- a/app/cross_monitor_worker.py +++ b/app/cross_monitor_worker.py @@ -106,7 +106,7 @@ def query_monitor_data(params): 'monitor_crosses_ledger_info': monitor_crosses_ledger_info, 'cross_delay_info_list': cross_delay_info_list } - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) # 查询巡检数据变化趋势 @@ -142,7 +142,7 @@ def query_monitor_data_trend(params): data = parse_monitor_trend_data(monitor_datas, date_type, special_time_range) res = make_common_res(0, 'ok') res['data'] = data - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) # 查询路口时段数据变化趋势 @@ -222,7 +222,7 @@ def query_cross_tp_data_trend(params): 'time_range': time_range, 'delay_infos': res_data } - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) def query_monitor_problems(params): @@ -326,7 +326,7 @@ def query_monitor_problems(params): monitor_problems = gen_monitor_problem_info(cross_report_pb, routing_crosses_dict, special_time_range, records, date_type, shield_info, query_date, nodeid, area_id, filter_shield) res = make_common_res(0, 'ok') res['data'] = monitor_problems - return json.dumps(res, ensure_ascii=False) + return json.dumps(clean_dict_nan(res, '-'), ensure_ascii=False) def update_cross_problem_shield_state(params):