Explorar o código

更新数据库日志配置,关闭SQLAlchemy详细日志;修改Cd通量计算服务,固定地区参数为"韶关",并优化错误信息提示。

drggboy hai 6 días
pai
achega
6568b8d296
Modificáronse 2 ficheiros con 19 adicións e 30 borrados
  1. 3 5
      app/database.py
  2. 16 25
      app/services/cd_flux_removal_service.py

+ 3 - 5
app/database.py

@@ -5,12 +5,10 @@ import os
 from dotenv import load_dotenv # type: ignore
 import logging
 from sqlalchemy.exc import SQLAlchemyError
-import logging
-# 开启SQLAlchemy的SQL执行日志(会打印所有执行的SQL语句和错误)
-logging.basicConfig()
-logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
-# 配置日志
+# 配置日志系统
 logging.basicConfig(level=logging.INFO)
+# 关闭SQLAlchemy的详细SQL执行日志,只保留错误日志
+logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING)
 logger = logging.getLogger(__name__)
 
 # 创建Base类

+ 16 - 25
app/services/cd_flux_removal_service.py

@@ -43,20 +43,20 @@ class CdFluxRemovalService:
         """
         根据地区计算籽粒移除Cd通量
         
-        @param area: 地区名称
+        @param area: 地区名称(仅用于地图边界,参数固定使用"韶关")
         @returns: 计算结果字典
         
         计算公式:籽粒移除(g/ha/a) = EXP(LnCropCd) * F11 * 0.5 * 15 / 1000
         """
         try:
             with SessionLocal() as db:
-                # 查询指定地区的参数(严格等号匹配,不做任何映射)
-                parameter = db.query(Parameters).filter(Parameters.area == area).first()
+                # 参数固定使用"韶关",area参数仅用于地图边界
+                parameter = db.query(Parameters).filter(Parameters.area == "韶关").first()
                 
                 if not parameter:
                     return {
                         "success": False,
-                        "message": f"未找到地区 '{area}' 的参数数据",
+                        "message": f"未找到韶关地区的参数数据",
                         "data": None
                     }
                 
@@ -119,20 +119,20 @@ class CdFluxRemovalService:
         """
         根据地区计算秸秆移除Cd通量
         
-        @param area: 地区名称
+        @param area: 地区名称(仅用于地图边界,参数固定使用"韶关")
         @returns: 计算结果字典
         
         计算公式:秸秆移除(g/ha/a) = [EXP(LnCropCd)/(EXP(LnCropCd)*0.76-0.0034)] * F11 * 0.5 * 15 / 1000
         """
         try:
             with SessionLocal() as db:
-                # 查询指定地区的参数(严格等号匹配,不做任何映射)
-                parameter = db.query(Parameters).filter(Parameters.area == area).first()
+                # 参数固定使用"韶关",area参数仅用于地图边界
+                parameter = db.query(Parameters).filter(Parameters.area == "韶关").first()
                 
                 if not parameter:
                     return {
                         "success": False,
-                        "message": f"未找到地区 '{area}' 的参数数据",
+                        "message": f"未找到韶关地区的参数数据",
                         "data": None
                     }
                 
@@ -408,24 +408,15 @@ class CdFluxRemovalService:
                             self.logger.warning(
                                 f"存在 {outside_count} 个样点位于边界之外,绘图时将被掩膜隐藏。示例(最多10条): {sample_preview}")
 
-                        report = {
-                            "area": area,
-                            "level": level,
-                            "calculation_type": calculation_type,
-                            "total_points": total_points,
-                            "inside_points": inside_count,
-                            "outside_points": outside_count,
-                            "inside_percentage": round(inside_pct, 2),
-                            "outside_samples": outside_points
-                        }
-                        os.makedirs(output_dir, exist_ok=True)
-                        report_path = os.path.join(
-                            output_dir,
-                            f"{calculation_type}_{area}_points_boundary_check_{timestamp}.json"
+                        # 在日志中打印边界检查统计结果
+                        self.logger.info(
+                            f"边界检查统计 - 地区: {area}, 层级: {level}, 计算类型: {calculation_type}, "
+                            f"总样点: {total_points}, 边界内: {inside_count} ({inside_pct:.2f}%), "
+                            f"边界外: {outside_count}"
                         )
-                        with open(report_path, "w", encoding="utf-8") as f:
-                            json.dump(report, f, ensure_ascii=False, indent=2)
-                        generated_files["point_boundary_report"] = report_path
+                        if outside_count > 0 and len(outside_points) > 0:
+                            sample_preview = outside_points[:5]  # 只显示前5个样本
+                            self.logger.info(f"边界外样点示例(前5个): {sample_preview}")
                     else:
                         generated_files = {}
                 except Exception as check_err: