Files
awoooi/apps/api/migrations/phase27_incident_frequency_snapshot.sql
OG T 88ac1c7f50
Some checks failed
CD Pipeline / build-and-deploy (push) Failing after 1m44s
feat(phase27): 歷史按鈕雙層頻率統計 + DB frequency_snapshot 持久化
- telegram_gateway: _send_incident_history 改為 Phase 27 雙層策略
  Layer 1: DB frequency_snapshot (建立時刻永久快照)
  Layer 2: Redis AnomalyCounter disposition 累積統計 (35d TTL)
  修復舊版呼叫 record_anomaly() 導致誤計數的 bug
- 新增 migration: phase27_incident_frequency_snapshot.sql (已在 prod 執行)
- CLAUDE.md: 精簡至 123 行,減少 Token 消耗

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-10 01:06:51 +08:00

25 lines
1.2 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Phase 27: Incident Frequency Snapshot 持久化
-- 2026-04-10 ogt: frequency_stats 只存記憶體/Redis(35天TTL),重啟或超期即失
-- 解決方案:在 incidents 表加 frequency_snapshot JSONB建立 incident 時寫入快照
-- 歷史按鈕優先讀 DB 快照Redis AnomalyCounter 補充長期累積統計
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'incidents' AND column_name = 'frequency_snapshot'
) THEN
ALTER TABLE incidents ADD COLUMN frequency_snapshot JSONB DEFAULT NULL;
COMMENT ON COLUMN incidents.frequency_snapshot IS
'Snapshot of AnomalyFrequency at incident creation time. '
'Fields: anomaly_key, count_1h, count_24h, count_7d, count_30d, '
'escalation_level, auto_repair_count, last_repair_action, '
'human_approved_count, manual_resolved_count, cold_start_trust_count, total_resolution_count. '
'Added 2026-04-10 (Phase 27).';
END IF;
END $$;
CREATE INDEX IF NOT EXISTS ix_incidents_frequency_snapshot_key
ON incidents ((frequency_snapshot->>'anomaly_key'))
WHERE frequency_snapshot IS NOT NULL;