From 4c0f15d7b3db98295222c56ba59c1717d38513a2 Mon Sep 17 00:00:00 2001 From: OG T Date: Tue, 31 Mar 2026 11:27:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=BE=A9=203=20=E5=80=8B=20P0?= =?UTF-8?q?=20Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. E2E Health: Docker 容器無法訪問內網 IP,改用公網域名 2. metrics_repository: asyncpg 需要 datetime 物件,不能用字串 3. metrics_repository: PostgreSQL 用 date_trunc 而非 strftime 2026-03-31 ogt: 首席架構師審查發現並修復 Co-Authored-By: Claude Opus 4.5 --- .gitea/workflows/e2e-health.yaml | 4 ++-- apps/api/src/repositories/metrics_repository.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.gitea/workflows/e2e-health.yaml b/.gitea/workflows/e2e-health.yaml index e739cccb3..ffad4731c 100644 --- a/.gitea/workflows/e2e-health.yaml +++ b/.gitea/workflows/e2e-health.yaml @@ -24,8 +24,8 @@ jobs: id: health_check run: | sudo apt-get update && sudo apt-get install -y curl || (apt-get update && apt-get install -y curl) - # 使用 host.docker.internal 或直接 IP - API_URL="http://192.168.0.121:32334" + # 2026-03-31 ogt: Docker 容器無法訪問內網 IP,改用公網域名 + API_URL="https://awoooi.wooo.work" echo "🔗 檢查 API 健康狀態..." for i in 1 2 3; do diff --git a/apps/api/src/repositories/metrics_repository.py b/apps/api/src/repositories/metrics_repository.py index b50207714..03099329d 100644 --- a/apps/api/src/repositories/metrics_repository.py +++ b/apps/api/src/repositories/metrics_repository.py @@ -56,8 +56,8 @@ class MetricsDBRepository(IMetricsRepository): """ try: async with get_db_context() as session: + # 2026-03-31 ogt: asyncpg 需要 datetime 物件,不能用字串 cutoff = datetime.now(UTC) - timedelta(hours=hours) - cutoff_str = cutoff.isoformat() # Query: 統計 executed vs total (approved + executed + execution_failed) query = text(""" @@ -69,7 +69,7 @@ class MetricsDBRepository(IMetricsRepository): AND status IN ('approved', 'executed', 'execution_failed') """) - result = await session.execute(query, {"cutoff": cutoff_str}) + result = await session.execute(query, {"cutoff": cutoff}) row = result.fetchone() if row and row.total_count > 0: @@ -119,13 +119,14 @@ class MetricsDBRepository(IMetricsRepository): """ try: async with get_db_context() as session: + # 2026-03-31 ogt: asyncpg 需要 datetime 物件,不能用字串 cutoff = datetime.now(UTC) - timedelta(hours=hours) - cutoff_str = cutoff.isoformat() # Trend: 過去 N 個時間點的成功率 (每小時一點) + # 2026-03-31 ogt: PostgreSQL 用 date_trunc 而非 strftime trend_query = text(""" SELECT - strftime('%Y-%m-%d %H:00:00', created_at) as hour_bucket, + date_trunc('hour', created_at) as hour_bucket, COUNT(CASE WHEN status = 'executed' THEN 1 END) * 100.0 / NULLIF(COUNT(*), 0) as hourly_rate FROM approval_records @@ -138,7 +139,7 @@ class MetricsDBRepository(IMetricsRepository): result = await session.execute( trend_query, - {"cutoff": cutoff_str, "limit": points}, + {"cutoff": cutoff, "limit": points}, ) rows = result.fetchall()