feat(lotto-agent): run_daily_digest task_id wrap
daily_digest에 create_task/update_task_status/add_log task_id wrap 적용. test_run_daily_digest_creates_task 추가 (75 passed). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -110,18 +110,19 @@ class LottoAgent(BaseAgent):
|
|||||||
return {"ok": False, "message": f"{type(e).__name__}: {e}"}
|
return {"ok": False, "message": f"{type(e).__name__}: {e}"}
|
||||||
|
|
||||||
async def run_daily_digest(self) -> dict:
|
async def run_daily_digest(self) -> dict:
|
||||||
"""일일 요약 — 지난 24h normal/urgent 발화를 묶어 텔레그램 1통."""
|
"""일일 요약 — 지난 24h normal/urgent 발화 텔레그램 1통. task_id wrap."""
|
||||||
from ..db import (
|
from ..db import (
|
||||||
get_recent_lotto_signals, get_signals_history, add_log,
|
create_task, update_task_status, add_log,
|
||||||
get_baseline,
|
get_recent_lotto_signals, get_signals_history, get_baseline,
|
||||||
)
|
)
|
||||||
from ..notifiers.telegram_lotto import send_signal_summary
|
from ..notifiers.telegram_lotto import send_signal_summary
|
||||||
|
|
||||||
|
task_id = create_task("lotto", "daily_digest", {})
|
||||||
|
try:
|
||||||
sigs = get_recent_lotto_signals(hours=24, min_fire="normal")
|
sigs = get_recent_lotto_signals(hours=24, min_fire="normal")
|
||||||
total_24h = get_signals_history(days=1)
|
total_24h = get_signals_history(days=1)
|
||||||
evaluated = len(total_24h)
|
evaluated = len(total_24h)
|
||||||
|
|
||||||
# weights_trend: drift_weights_cache의 prev/curr 차이
|
|
||||||
trend = {}
|
trend = {}
|
||||||
try:
|
try:
|
||||||
cache = get_baseline("drift_weights_cache")
|
cache = get_baseline("drift_weights_cache")
|
||||||
@@ -133,7 +134,7 @@ class LottoAgent(BaseAgent):
|
|||||||
for k in (set(prev_w) | set(curr_w))
|
for k in (set(prev_w) | set(curr_w))
|
||||||
}
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
add_log(self.agent_id, f"weights_trend 계산 실패: {e}", level="warning")
|
add_log("lotto", f"weights_trend 계산 실패: {e}", level="warning", task_id=task_id)
|
||||||
|
|
||||||
digest = {
|
digest = {
|
||||||
"evaluated": evaluated,
|
"evaluated": evaluated,
|
||||||
@@ -142,8 +143,17 @@ class LottoAgent(BaseAgent):
|
|||||||
"weights_trend": trend,
|
"weights_trend": trend,
|
||||||
}
|
}
|
||||||
await send_signal_summary(digest)
|
await send_signal_summary(digest)
|
||||||
add_log(self.agent_id, f"daily_digest 발송: 평가 {evaluated} / 발화 {len(sigs)}")
|
update_task_status(task_id, "succeeded", result_data={
|
||||||
|
"evaluated": evaluated,
|
||||||
|
"fired": len(sigs),
|
||||||
|
"signals_count": len(sigs),
|
||||||
|
})
|
||||||
|
add_log("lotto", f"daily_digest 발송: 평가 {evaluated} / 발화 {len(sigs)}", task_id=task_id)
|
||||||
return {"ok": True, **digest}
|
return {"ok": True, **digest}
|
||||||
|
except Exception as e:
|
||||||
|
update_task_status(task_id, "failed", result_data={"error": str(e)})
|
||||||
|
add_log("lotto", f"daily_digest 예외: {e}", level="error", task_id=task_id)
|
||||||
|
return {"ok": False, "message": f"{type(e).__name__}: {e}"}
|
||||||
|
|
||||||
async def run_weekly_evolution_report(self) -> dict:
|
async def run_weekly_evolution_report(self) -> dict:
|
||||||
"""토 22:15 — lotto-lab evaluate-now 트리거 후 텔레그램 리포트."""
|
"""토 22:15 — lotto-lab evaluate-now 트리거 후 텔레그램 리포트."""
|
||||||
|
|||||||
@@ -92,3 +92,23 @@ async def test_run_signal_check_failure_marks_task_failed(monkeypatch):
|
|||||||
assert len(tasks) == 1
|
assert len(tasks) == 1
|
||||||
assert tasks[0]["status"] == "failed"
|
assert tasks[0]["status"] == "failed"
|
||||||
assert "boom" in tasks[0]["result_data"]["error"]
|
assert "boom" in tasks[0]["result_data"]["error"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_run_daily_digest_creates_task(monkeypatch):
|
||||||
|
"""run_daily_digest이 agent_tasks에 task 생성 + result_data 저장."""
|
||||||
|
from app.agents.lotto import LottoAgent
|
||||||
|
from app.notifiers import telegram_lotto
|
||||||
|
|
||||||
|
async def fake_send(_d): pass
|
||||||
|
monkeypatch.setattr(telegram_lotto, "send_signal_summary", fake_send)
|
||||||
|
|
||||||
|
agent = LottoAgent()
|
||||||
|
result = await agent.run_daily_digest()
|
||||||
|
assert result["ok"] is True
|
||||||
|
|
||||||
|
tasks = db.get_agent_tasks("lotto", task_type="daily_digest", days=1)
|
||||||
|
assert len(tasks) == 1
|
||||||
|
assert tasks[0]["status"] == "succeeded"
|
||||||
|
assert "fired" in tasks[0]["result_data"]
|
||||||
|
assert "evaluated" in tasks[0]["result_data"]
|
||||||
|
|||||||
Reference in New Issue
Block a user