refactor(api): Phase 22 技術債修復 - 業務邏輯分層
Some checks failed
E2E Health Check / e2e-health (push) Has been cancelled

P2.3: LearningService.get_learning_summary() 業務邏輯移至 Service 層
- Repository 只提供原始統計數據
- Service 計算 best_action 和 learning_status

P2.6: Playbook similarity 計算邏輯抽取
- 新增 src/utils/similarity.py
- Repository 從 utils 導入,不再定義演算法

2026-03-31 Claude Code (首席架構師技術債修復)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-03-31 18:55:06 +08:00
parent 83a0845858
commit e1e3bba296
3 changed files with 142 additions and 59 deletions

View File

@@ -655,7 +655,12 @@ class LearningService:
"""
取得學習摘要
2026-03-29 P0 修正: 委託 Repository 實作
Phase 22 P2: 業務邏輯移至 Service 層
2026-03-31 Claude Code (首席架構師技術債修復)
邏輯:
- 從 Repository 取得原始統計數據
- 在 Service 層計算 best_action 和 learning_status
Returns:
{
@@ -667,7 +672,51 @@ class LearningService:
'learning_status': 'sufficient',
}
"""
return await self._repository.get_learning_summary(anomaly_key)
# 從 Repository 取得原始統計
all_stats = await self._repository.get_all_repair_stats(anomaly_key)
if not all_stats:
return {
"anomaly_key": anomaly_key,
"total_repair_attempts": 0,
"overall_success_rate": 0.0,
"actions_tried": [],
"best_action": None,
"learning_status": "insufficient",
}
# === 以下為業務邏輯,應在 Service 層 ===
total_attempts = sum(s["total"] for s in all_stats.values())
total_success = sum(s["success"] for s in all_stats.values())
overall_rate = total_success / total_attempts if total_attempts > 0 else 0.0
# 找出最佳動作 (需要至少 3 次數據)
best_action = None
best_rate = 0.0
for action, stats in all_stats.items():
if stats["total"] >= 3 and stats["success_rate"] > best_rate:
best_rate = stats["success_rate"]
best_action = {"action": action, "success_rate": best_rate}
# 判斷學習狀態
if total_attempts < 3:
learning_status = "insufficient"
elif total_attempts < 10:
learning_status = "learning"
elif overall_rate >= 0.8:
learning_status = "excellent"
else:
learning_status = "sufficient"
return {
"anomaly_key": anomaly_key,
"total_repair_attempts": total_attempts,
"overall_success_rate": overall_rate,
"actions_tried": list(all_stats.keys()),
"best_action": best_action,
"learning_status": learning_status,
}
def _get_action_tier(self, action: str) -> int:
"""取得動作的 Tier"""