Explorar el Código

fix: record fault diagnosis time and filter report by record time

zhouyang.xie hace 1 semana
padre
commit
ddd87c979e
Se han modificado 2 ficheros con 29 adiciones y 7 borrados
  1. 7 4
      app/report/page_source.py
  2. 22 3
      app/routers/autodiag.py

+ 7 - 4
app/report/page_source.py

@@ -202,10 +202,13 @@ def collect_fault_from_page(wind: Dict[str, Any], months: List[str]) -> Dict[str
             cur.execute(
                 """
                 SELECT wind_code, engine_code, point_name, diagnosis_type, max_status,
-                       component_type, create_time
+                       component_type, create_time,
+                       COALESCE(record_time, create_time) AS rec_time
                 FROM vibration_diagnosis_result
-                WHERE wind_code=%s AND create_time >= %s AND create_time < %s
-                ORDER BY create_time
+                WHERE wind_code=%s
+                  AND COALESCE(record_time, create_time) >= %s
+                  AND COALESCE(record_time, create_time) < %s
+                ORDER BY COALESCE(record_time, create_time)
                 """,
                 (code, start, end),
             )
@@ -218,7 +221,7 @@ def collect_fault_from_page(wind: Dict[str, Any], months: List[str]) -> Dict[str
     turbine_map: Dict[str, int] = {}
     type_map: Dict[str, int] = {}
     for row in rows:
-        month = row["create_time"].strftime("%Y-%m")
+        month = row["rec_time"].strftime("%Y-%m")
         m = monthly_map.setdefault(month, {"month": month, "count": 0})
         m["count"] += 1
         eng = row["engine_code"] or row["point_name"]

+ 22 - 3
app/routers/autodiag.py

@@ -12,7 +12,7 @@ from fastapi.responses import JSONResponse
 
 from app.logger import logger
 from app.models.AutoDiagModel import AutoDiagInput
-from app.report.collector import platform_conn
+from app.report.collector import platform_conn, business_conn, safe_code
 from app.services.Auto_diag import Auto_diag
 
 router = APIRouter(prefix="/autodiag", tags=["autodiag"])
@@ -50,6 +50,24 @@ def _save_result(analysis_type: str, inp: AutoDiagInput, result: Dict[str, Any])
     meta = result.get("meta") or {}
     frequencies = meta.get("frequencies") or {}
     unsupported = result.get("unsupported") or {}
+    record_time = None
+    try:
+        ids = list(inp.ids or [])
+        if ids:
+            bconn = business_conn()
+            try:
+                table = f"`{safe_code(inp.windCode)}_wave`"
+                with bconn.cursor() as cur:
+                    cur.execute(
+                        f"SELECT MIN(time_stamp) AS t FROM {table} WHERE id IN ({','.join(['%s'] * len(ids))})",
+                        ids,
+                    )
+                    row = cur.fetchone()
+                    record_time = row["t"] if row else None
+            finally:
+                bconn.close()
+    except Exception as exc:
+        logger.warning("诊断记录时间查询失败: %s", exc)
     try:
         conn = platform_conn()
         try:
@@ -59,8 +77,8 @@ def _save_result(analysis_type: str, inp: AutoDiagInput, result: Dict[str, Any])
                     INSERT INTO vibration_diagnosis_result
                     (engine_code, wind_code, point_name, diagnosis_type, max_status,
                      component_type, brand, model, frequencies_json,
-                     unsupported_json, meta_json)
-                    VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
+                     unsupported_json, meta_json, record_time)
+                    VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
                     """,
                     (
                         inp.engine_code,
@@ -74,6 +92,7 @@ def _save_result(analysis_type: str, inp: AutoDiagInput, result: Dict[str, Any])
                         json.dumps(frequencies, ensure_ascii=False),
                         json.dumps(unsupported, ensure_ascii=False),
                         json.dumps(meta, ensure_ascii=False),
+                        record_time,
                     ),
                 )
                 return int(cur.lastrowid)