From 93f9522d5a6cdb5b435710aafbe8420d63548fd9 Mon Sep 17 00:00:00 2001 From: OG T Date: Sun, 12 Apr 2026 16:33:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(heartbeat):=20=E5=B0=8D=E9=BD=8A=E6=95=B4?= =?UTF-8?q?=E9=BB=9E=E7=99=BC=E9=80=81=E9=81=BF=E5=85=8D=E5=A4=9Areplica?= =?UTF-8?q?=E5=90=84=E8=87=AA=E7=99=BC=20+=20KM=E5=90=91=E9=87=8F=E5=8C=96?= =?UTF-8?q?=E6=94=B9=E6=9F=A5embedding=E6=AC=84=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - _heartbeat_loop: 先 sleep 到下一個整點倍數再開始循環 避免 3 個 replica 啟動時間不同導致短時間內收到多條心跳 - heartbeat_report_service: km_vectorized 改查 KnowledgeEntryRecord.embedding IS NOT NULL 原本錯誤查 IncidentRecord.vectorized 導致顯示 0/714 (0%) 2026-04-12 ogt (ADR-073 heartbeat fix) --- apps/api/src/services/heartbeat_report_service.py | 10 ++++------ apps/api/src/services/telegram_gateway.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/apps/api/src/services/heartbeat_report_service.py b/apps/api/src/services/heartbeat_report_service.py index b936525d..52de5210 100644 --- a/apps/api/src/services/heartbeat_report_service.py +++ b/apps/api/src/services/heartbeat_report_service.py @@ -339,15 +339,13 @@ class HeartbeatReportService: km_total = await db.scalar(select(func.count()).select_from(KnowledgeEntryRecord)) stats.km_total = km_total or 0 - # Incident 向量化數 + # KM 向量化數(embedding IS NOT NULL) + # 注意:knowledge_entries 無 vectorized 欄位,用 embedding 判斷 vec_count = await db.scalar( - select(func.count()).select_from(IncidentRecord) - .where(IncidentRecord.vectorized == True) # noqa: E712 + select(func.count()).select_from(KnowledgeEntryRecord) + .where(KnowledgeEntryRecord.embedding.isnot(None)) ) - inc_total = await db.scalar(select(func.count()).select_from(IncidentRecord)) stats.km_vectorized = vec_count or 0 - if not stats.km_total: - stats.km_total = inc_total or 0 # 24h 修復統計 since = datetime.utcnow() - timedelta(hours=24) diff --git a/apps/api/src/services/telegram_gateway.py b/apps/api/src/services/telegram_gateway.py index 1f8d8185..297032ac 100644 --- a/apps/api/src/services/telegram_gateway.py +++ b/apps/api/src/services/telegram_gateway.py @@ -4748,6 +4748,16 @@ class TelegramGateway: """ interval_seconds = interval_minutes * 60 + # 對齊到下一個整點倍數(例如 interval=30 → 對齊到 :00 或 :30) + # 避免多 replica 因啟動時間不同而各自發送 + now_ts = datetime.now(UTC).timestamp() + next_slot = (int(now_ts / interval_seconds) + 1) * interval_seconds + wait_seconds = next_slot - now_ts + try: + await asyncio.sleep(wait_seconds) + except asyncio.CancelledError: + return + while self._heartbeat_active: try: await self.send_heartbeat()