瀏覽代碼

fix: capture chart elements only, remove full-page screenshots

zhouyang.xie 1 周之前
父節點
當前提交
993bdac789
共有 1 個文件被更改,包括 40 次插入 和 24 次删除
  1. 40 24
      app/report/page_chart.py

+ 40 - 24
app/report/page_chart.py

@@ -85,37 +85,43 @@ def capture_overview(page, out_dir: str, wind_code: str, start: str, end: str) -
     return paths
 
 
+def _shot_canvas(page, out_dir: str, name: str) -> str:
+    """抓取页面中第一个可见的 ECharts canvas。"""
+    try:
+        for i in range(page.locator("canvas").count()):
+            el = page.locator("canvas").nth(i)
+            box = el.bounding_box()
+            if box and box["width"] > 200 and box["height"] > 100:
+                el.screenshot(path=os.path.join(out_dir, f"{name}.png"))
+                return os.path.join(out_dir, f"{name}.png")
+    except Exception:
+        pass
+    return ""
+
+
 def capture_health(page, out_dir: str) -> List[str]:
-    _goto(page, "/home/health/index")
-    paths = []
-    p = _save_shot(page, out_dir, "health_page")
-    if p:
-        paths.append(p)
-    return paths
+    # 健康评估页面展示的是评分表格,无 ECharts/Plotly 图,不抓整页截图。
+    return []
 
 
 def capture_algorithm_health(page, out_dir: str) -> List[str]:
-    _goto(page, "/home/health/index")
-    paths = []
-    try:
-        page.locator("button", has_text="算法健康评估").first.click()
-        time.sleep(2)
-        p = _save_shot(page, out_dir, "algorithm_health_dialog", selector=".el-dialog")
-        if p:
-            paths.append(p)
-    except Exception:
-        pass
-    if not paths:
-        p = _save_shot(page, out_dir, "algorithm_health_page")
-        if p:
-            paths.append(p)
-    return paths
+    # 算法健康评估页面展示的是评分表格,无图表,不抓整页截图。
+    return []
 
 
 def capture_fault(page, out_dir: str) -> List[str]:
-    _goto(page, "/home/health/malfunction")
+    page.goto(
+        f"{BASE}/home/health/malfunction?windCode=WOF046400029&engineCode=WOG01315&month=2024-11",
+        wait_until="domcontentloaded", timeout=60000,
+    )
+    time.sleep(7)
+    try:
+        page.locator(".search-btn", has_text="查询").first.click()
+        time.sleep(9)
+    except Exception:
+        pass
     paths = []
-    p = _save_shot(page, out_dir, "fault_page")
+    p = _shot_canvas(page, out_dir, "fault_chart")
     if p:
         paths.append(p)
     return paths
@@ -123,8 +129,18 @@ def capture_fault(page, out_dir: str) -> List[str]:
 
 def capture_vibration(page, out_dir: str) -> List[str]:
     _goto(page, "/home/health/vibration")
+    try:
+        page.locator(".searchbox .el-button", has_text="查询").first.click()
+        time.sleep(7)
+    except Exception:
+        pass
+    try:
+        page.locator(".picture", has_text="频谱图").first.click()
+        time.sleep(8)
+    except Exception:
+        pass
     paths = []
-    p = _save_shot(page, out_dir, "vibration_page")
+    p = _shot_canvas(page, out_dir, "vibration_chart")
     if p:
         paths.append(p)
     return paths