From 90e8366a8d8fa0a6996deb77f5dcbf3fb5c20d1e Mon Sep 17 00:00:00 2001
From: OoO
Date: Tue, 5 May 2026 01:13:31 +0800
Subject: [PATCH] =?UTF-8?q?feat(p54):=20chart.js=20=E8=A6=96=E8=A6=BA?=
=?UTF-8?q?=E5=BE=AE=E8=AA=BF=20=E2=80=94=20KPI=20sparkline=20+=20verdict?=
=?UTF-8?q?=20=E5=9C=93=E9=A4=85=20+=20heal=20=E8=B6=A8=E5=8B=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
R-1: ai_calls KPI 卡片加 24h sparkline
- 呼叫次數卡片下加 24px 高 mini line chart(藍)
- 成本卡片下加 sparkline(黃)
- 錯誤次數卡片下加 sparkline(紅)
- Token / 平均耗時 / RAG 命中卡片改顯示「平均 tk/call」「cache 命中數」「RAG 命中率%」
- 整排 KPI 從乾巴巴數字 → 含 24h 趨勢視覺
- 共用 chart.js dataset,無新 query
R-2: business_intel verdict 改 doughnut + 表格雙視角
- 取代原 col-md-3 卡片網格
- 左圓餅:effective(綠)/backfired(紅)/neutral(灰) 視覺比例
- 右表格:4 欄(verdict/筆數/佔比/平均 Δ)含正負色
- 與 quality_trend RAG pie chart 視覺風格統一
R-3: host_health AIOps card 加 7d 自癒成功率 sparkline
- routes/admin_observability_routes.py 新加 heal_daily query
date_trunc('day') GROUP BY 7 天每日 success rate
- AIOps 7d card 底部加 80px 高 line chart
- Y 軸 0-100% / X 軸 7 天日期
- tooltip 顯示「ok/total 成功 (rate%)」
chart.js 視覺化從 4 個 → 7 個:
hourly trend / 30d stacked / 三主機 sparkline / RAG doughnut /
KPI sparkline × 3 / verdict doughnut / heal trend
Phase 38→54 累計 19 commits / 10 觀測頁 + topbar indicator / 7 chart.js。
Co-Authored-By: Claude Opus 4.7 (1M context)
---
routes/admin_observability_routes.py | 21 ++++++
templates/admin/ai_calls_dashboard.html | 77 +++++++++++++++++++---
templates/admin/business_intel.html | 88 +++++++++++++++++++------
templates/admin/host_health.html | 42 ++++++++++++
4 files changed, 200 insertions(+), 28 deletions(-)
diff --git a/routes/admin_observability_routes.py b/routes/admin_observability_routes.py
index 0be0aa2..4fa609f 100644
--- a/routes/admin_observability_routes.py
+++ b/routes/admin_observability_routes.py
@@ -2347,6 +2347,27 @@ def host_health_dashboard():
float(heal_rows[1] or 0) / float(heal_rows[0]) * 100
) if heal_rows[0] else 0,
}
+
+ # Phase 54 R-3: heal 7d daily success rate sparkline
+ heal_daily = _session2.execute(
+ sa_text("""
+ SELECT date_trunc('day', created_at)::date AS d,
+ COUNT(*) AS total,
+ COUNT(*) FILTER (WHERE result = 'success') AS ok
+ FROM heal_logs
+ WHERE created_at >= NOW() - INTERVAL '7 days'
+ GROUP BY d ORDER BY d ASC
+ """),
+ ).fetchall()
+ aiops_summary['heal_sparkline'] = [
+ {
+ 'date': r[0].strftime('%m-%d') if r[0] else '',
+ 'total': int(r[1] or 0),
+ 'ok': int(r[2] or 0),
+ 'rate': (float(r[2] or 0) / float(r[1]) * 100) if r[1] else 0,
+ }
+ for r in heal_daily
+ ]
except Exception:
aiops_summary = {}
diff --git a/templates/admin/ai_calls_dashboard.html b/templates/admin/ai_calls_dashboard.html
index c2f2136..9fed29c 100644
--- a/templates/admin/ai_calls_dashboard.html
+++ b/templates/admin/ai_calls_dashboard.html
@@ -55,14 +55,54 @@
-
+
-
呼叫次數
{{ "{:,}".format(summary.total_calls or 0) }}
-
Token 用量
{{ "{:,}".format(summary.total_tokens or 0) }}
-
成本 (USD)
${{ "%.2f"|format(summary.total_cost or 0) }}
-
平均耗時
{{ summary.avg_duration or 0 }} ms
-
RAG 命中
{{ summary.rag_hits or 0 }}
-
錯誤次數
{{ summary.error_calls or 0 }}
+
+
+ 呼叫次數
+
{{ "{:,}".format(summary.total_calls or 0) }}
+ {% if hourly_trend %}{% endif %}
+
+
+
+
+ Token 用量
+
{{ "{:,}".format(summary.total_tokens or 0) }}
+ {{ (summary.total_tokens or 0) // (summary.total_calls or 1) }} tk/call 平均
+
+
+
+
+ 成本 (USD)
+
${{ "%.2f"|format(summary.total_cost or 0) }}
+ {% if hourly_trend %}{% endif %}
+
+
+
+
+ 平均耗時
+
{{ summary.avg_duration or 0 }} ms
+ {{ summary.cache_hits or 0 }} 次 cache 命中
+
+
+
+
+ RAG 命中
+
{{ summary.rag_hits or 0 }}
+
+ {% if (summary.total_calls or 0) > 0 %}
+ {{ "%.1f"|format((summary.rag_hits or 0) / summary.total_calls * 100) }}%
+ {% else %}—{% endif %}
+
+
+
+
+
+ 錯誤次數
+
{{ summary.error_calls or 0 }}
+ {% if hourly_trend %}{% endif %}
+
+
@@ -268,12 +308,33 @@
+
{% endif %}
diff --git a/templates/admin/host_health.html b/templates/admin/host_health.html
index e5ee49e..f941fe9 100644
--- a/templates/admin/host_health.html
+++ b/templates/admin/host_health.html
@@ -179,6 +179,14 @@
7d 共 {{ aiops_summary.heals_total }} 次自癒嘗試
(成功 {{ aiops_summary.heals_success }} · 失敗 {{ aiops_summary.heals_failed }})
+ {% if aiops_summary.heal_sparkline %}
+
+
+
+
+ 7 日每日自癒成功率趨勢
+
+ {% endif %}
{% endif %}
@@ -474,7 +482,41 @@
+