fix(adr075): drift 通知改用 send_drift_card,補齊所有呼叫點
All checks were successful
CD Pipeline / build-and-deploy (push) Successful in 14m13s

- drift.py: 移除死碼 send_text(),改由 narrate_and_notify() 統一發卡片
- drift_narrator_service: _send_telegram() 改呼 send_drift_card() 帶四顆按鈕
- webhooks.py /alerts 路徑: 補傳 alert_category 啟用動態按鈕

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-04-12 20:20:41 +08:00
parent f4675872f9
commit eda0cfd034
3 changed files with 30 additions and 43 deletions

View File

@@ -158,40 +158,13 @@ async def _analyze_and_notify(report: DriftReport) -> None:
_logger = _structlog.get_logger(__name__)
try:
interpreter = get_drift_interpreter()
analyzer = get_drift_analyzer()
interpretation = await interpreter.analyze(report)
repo = get_drift_repository()
await repo.update_interpretation(report.report_id, interpretation)
diff_summary = analyzer.format_diff_summary(report)
intent_label = {
"emergency_hotfix": "🚨 緊急 Hotfix",
"human_error": "⚠️ 人為誤操作",
"automated_change": "🤖 系統自動變更",
"unknown": "❓ 意圖不明",
}.get(interpretation.intent.value, "❓ 意圖不明")
try:
from src.services.telegram_gateway import get_telegram_gateway
tg = get_telegram_gateway()
await tg.send_text(
f"🔍 <b>Config Drift 偵測</b>\n"
f"Namespace: {report.namespace}\n"
f"嚴重度: HIGH×{report.high_count} MEDIUM×{report.medium_count}\n\n"
f"<b>意圖分析</b>: {intent_label}\n"
f"{interpretation.explanation}\n"
f"信心: {interpretation.confidence:.0%}\n\n"
f"<b>漂移詳情</b>:\n{diff_summary}\n\n"
f"Report ID: <code>{report.report_id}</code>\n"
f"POST /api/v1/drift/reports/{report.report_id}/rollback — 覆蓋回 Git\n"
f"adopt 端點暫停開放,待 ADR-057 實作後啟用)"
)
except Exception as e:
_logger.warning("drift_telegram_failed", error=str(e))
# Phase 30 (2026-04-10 Claude Code ADR-067): AI 人話摘要推送
# 在技術格式訊息之後,額外推送 qwen2.5:7b-instruct 生成的繁中摘要
# ADR-075: drift_narrator_service 負責發送 TYPE-4D 卡片(含按鈕)
# 舊的 send_text() 已移除,改由 narrate_and_notify() 統一處理
try:
from src.services.drift_narrator_service import get_drift_narrator_service
narrator = get_drift_narrator_service()

View File

@@ -976,6 +976,16 @@ async def receive_alert(
# 2026-04-08 ogt: 補傳 incident_id 以啟用詳情/重診/歷史按鈕
incident_id="", # /alerts 路徑尚無 incidentdetail/reanalyze/history 按鈕不顯示
# /alerts 路徑沒有 notification_type非 Alertmanager 路徑),不需 TYPE-4D routing
# ADR-075: alert_type → category 對應,啟用動態按鈕
alert_category={
"k8s_node_failure": "kubernetes",
"k8s_pod_crash": "kubernetes",
"db_connection_timeout": "database",
"high_cpu": "host_resource",
"high_memory": "host_resource",
"disk_full": "storage",
"ssl_expiry": "ssl_cert",
}.get(alert.alert_type, "general"),
)
return AlertResponse(

View File

@@ -17,7 +17,6 @@ Drift Narrator Service - Phase 30
from __future__ import annotations
import html
from typing import TYPE_CHECKING
import httpx
@@ -224,25 +223,30 @@ class DriftNarratorService:
)
async def _send_telegram(self, report: "DriftReport", narrative: str) -> None:
"""推送 Telegram 人話摘要卡"""
"""
推送 TYPE-4D Config Drift 卡片ADR-075
使用 send_drift_card() 取代舊 send_notification(),呈現結構化格式與操作按鈕。
diff_summary = AI 研判narrative + 漂移詳情(前 8 筆)
approval_id / incident_id 均使用 report_id無需建立 ApprovalRequest
"""
from src.services.telegram_gateway import get_telegram_gateway
severity_icon = "🔴" if report.high_count > 0 else "🟡"
msg = (
f"{severity_icon} <b>K8s 配置漂移</b>\n"
f"Namespace: <code>{html.escape(report.namespace)}</code>\n"
f"HIGH: {report.high_count} | MEDIUM: {report.medium_count}\n"
f"\n"
f"🤖 <b>AI 研判</b>\n"
f"{html.escape(narrative)}\n"
f"\n"
f"📋 Report: <code>{html.escape(report.report_id)}</code>\n"
f"<i>qwen2.5:7b-instruct | 免費本地推理</i>"
diff_summary = (
f"🤖 AI 研判\n{narrative}\n\n"
f"漂移明細HIGH: {report.high_count} | MEDIUM: {report.medium_count}\n"
f"{self._format_drift_summary(report)}"
)
try:
tg = get_telegram_gateway()
await tg.send_notification(msg[:4096])
await tg.send_drift_card(
incident_id=report.report_id,
approval_id=report.report_id,
resource_name=report.namespace,
diff_summary=diff_summary[:500],
detected_at="",
)
except Exception as e:
logger.warning("drift_narrator_telegram_error", error=str(e))