فهرست منبع

fix: clean garbled titles and locate problem turbines/components

zhouyang.xie 1 هفته پیش
والد
کامیت
7985ff1c00
2فایلهای تغییر یافته به همراه65 افزوده شده و 1 حذف شده
  1. 11 1
      app/report/generator.py
  2. 54 0
      app/report/template_builder.py

+ 11 - 1
app/report/generator.py

@@ -28,6 +28,15 @@ def _safe_name(value: str) -> str:
     return value[:80]
 
 
+def _clean_title(value: str, fallback: str) -> str:
+    """清洗报告标题:乱码(连续问号)或空白时回退为默认标题。"""
+    value = (value or "").strip()
+    value = re.sub(r"[\u0000-\u001f\u007f]", "", value)
+    if not value or value.count("?") >= 3 or "??" in value:
+        return fallback
+    return value
+
+
 def _read_manifest() -> List[Dict[str, Any]]:
     ensure_dirs()
     if not os.path.exists(MANIFEST):
@@ -120,7 +129,8 @@ def run_job(job_id: str, request: Dict[str, Any], store: Dict[str, Dict[str, Any
         update(65, "生成图表与 Word 文档…")
 
         ts = datetime.now().strftime("%Y%m%d_%H%M%S")
-        title = request.get("title") or f"{data['wind']['name']}风电机组可靠性和能效双提升数据分析报告"
+        default_title = f"{data['wind']['name']}风电机组可靠性和能效双提升数据分析报告"
+        title = _clean_title(request.get("title"), default_title)
         filename = f"{_safe_name(data['wind']['name'])}_{start_month}_{end_month}_可靠性和能效双提升数据分析报告_{ts}.docx"
         output_path = os.path.join(REPORT_DIR, filename)
         tmp_dir = os.path.join(TMP_ROOT, job_id)

+ 54 - 0
app/report/template_builder.py

@@ -395,6 +395,28 @@ def _section_power_curve(doc, data: Dict[str, Any]):
         )
 
 
+def _worst_subsystem(t: Dict[str, Any]) -> str:
+    labels = {
+        "generator": "发电机系统",
+        "nacelle": "机舱系统",
+        "grid": "电网环境",
+        "drive_train": "传动系统",
+    }
+    vals = []
+    for k, name in labels.items():
+        v = t.get(k)
+        try:
+            v = float(v)
+        except (TypeError, ValueError):
+            v = None
+        if v is not None and v >= 0:
+            vals.append((v, name))
+    if not vals:
+        return "未评估"
+    vals.sort(key=lambda x: x[0])
+    return vals[0][1]
+
+
 def _section_conclusion(doc, data: Dict[str, Any]):
     from app.report import criteria
 
@@ -410,6 +432,38 @@ def _section_conclusion(doc, data: Dict[str, Any]):
     L._heading(doc, "8.2 结论", 2)
     for i, c in enumerate(cr["conclusions"], 1):
         L._para(doc, f"{i}. {c}", indent=False)
+
+    # 重点机组与部件定位
+    L._para(doc, "重点机组定位:", indent=False, bold=True)
+    ov = data.get("overview") or {}
+    h = data.get("health") or {}
+    f = data.get("fault") or {}
+    ah = data.get("algorithm_health") or {}
+
+    if ov.get("available") and ov.get("turbines"):
+        low_eff = sorted(ov["turbines"], key=lambda x: x.get("gen_mwh") or 0)[:5]
+        names = "、".join(
+            f"{t.get('name') or t.get('code')}({_num(t.get('gen_mwh'), 2, ' MWh')})" for t in low_eff
+        )
+        L._para(doc, f"· 能效低机组(发电量排名末 5):{names}。", indent=False, size=10)
+
+    if h.get("available") and h.get("low_turbines"):
+        items = []
+        for t in h["low_turbines"][:8]:
+            items.append(f"{t.get('name') or t.get('code')}({_num(t.get('score'), 2, ' 分')},{_worst_subsystem(t)})")
+        L._para(doc, f"· 健康低分机组(<70 分):{';'.join(items)}。", indent=False, size=10)
+
+    if f.get("available") and f.get("turbines"):
+        ft = sorted(f["turbines"], key=lambda x: x.get("count") or 0, reverse=True)[:8]
+        names = "、".join(f"{t.get('name') or t.get('code')}({t.get('count', 0)} 次)" for t in ft)
+        L._para(doc, f"· 故障集中机组:{names}。", indent=False, size=10)
+
+    if ah.get("available"):
+        unscored = [t for t in (ah.get("turbines") or []) if (t.get("overall") or {}).get("status") != "scored"]
+        if unscored:
+            names = "、".join(t.get("turbine_name") or t.get("turbine") or "" for t in unscored[:10])
+            L._para(doc, f"· 算法健康评估未评总分机组:{names}。", indent=False, size=10)
+
     L._heading(doc, "8.3 建议", 2)
     for i, s in enumerate(cr["suggestions"], 1):
         L._para(doc, f"{i}. {s}", indent=False, size=10)