"""C-04 演示:扫描已配置阈值的客户,命中则 Redis pub + 控制台输出。 用法:python scripts/demo/push_threshold_alerts.py 需 MySQL 演示库 + Redis 6380。 """ from __future__ import annotations import json import os import sys ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, ROOT) from app.repository.core_ro import CoreReadOnlyRepository from app.repository.threshold_repository import ThresholdRepository from app.service.threshold_service import THRESHOLD_PUSH_CHANNEL, run_threshold_check def main() -> None: core = CoreReadOnlyRepository() repo = ThresholdRepository() customer_ids = repo.list_customer_ids_with_threshold() if not customer_ids: print("No customers with threshold config.") return hits = 0 for cid in customer_ids: rows = core.list_holdings(cid, limit=500) out = run_threshold_check(cid, rows, trace_id="demo-push", push=True) if out.get("alert"): hits += 1 print(json.dumps({"customer_id": cid, **out}, ensure_ascii=False)) print(f"channel={THRESHOLD_PUSH_CHANNEL} alerts={hits}/{len(customer_ids)}") if __name__ == "__main__": sys.stdout.reconfigure(encoding="utf-8", errors="replace") main()