fix(lotto): Phase 3 리뷰 반영 (run-forward 백그라운드·review 404·track_record distinct·테스트 보강)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-31 17:30:10 +09:00
parent 74f385c7bd
commit add433233a
5 changed files with 73 additions and 7 deletions

View File

@@ -207,6 +207,7 @@ def track_record() -> Dict[str, Any]:
db = _db()
rows = db.get_backtest_runs()
agg: Dict[str, Dict[str, int]] = {}
draw_sets: Dict[str, set] = {}
for r in rows:
a = agg.setdefault(r["strategy"], {
"n_tickets": 0, "1st": 0, "2nd": 0, "3rd": 0, "4th": 0, "5th": 0, "draws": 0})
@@ -214,7 +215,9 @@ def track_record() -> Dict[str, Any]:
a["n_tickets"] += r["n_tickets"]
for tier in ("1st", "2nd", "3rd", "4th", "5th"):
a[tier] += p[tier]
a["draws"] += 1
draw_sets.setdefault(r["strategy"], set()).add(r["draw_no"])
for strat, s in draw_sets.items():
agg[strat]["draws"] = len(s)
return {"by_strategy": agg}

View File

@@ -49,6 +49,7 @@ from .routers import briefing as briefing_router
from .routers import review as review_router
from .routers import backtest as backtest_router
from .jobs.grade_weekly_review import run_for_latest as grade_run_for_latest
from . import backtest
app = FastAPI()
install_access_log(app)
@@ -86,9 +87,8 @@ def on_startup():
_refresh_perf_cache() # 새 채점 결과 반영 → 즉시 갱신
# 자가학습 백테스트 — 새 회차 forward 구매 + 당첨조합 캘리브레이션
try:
from . import backtest as _backtest
_backtest.run_forward_purchase(draw_no=res["drawNo"])
_backtest.calibrate_winner(res["drawNo"])
backtest.run_forward_purchase(draw_no=res["drawNo"])
backtest.calibrate_winner(res["drawNo"])
except Exception as e:
logger.warning(f"backtest 갱신 실패: {e}")

View File

@@ -16,12 +16,21 @@ def calibration(weeks: int = Query(52, ge=1, le=520)):
@router.get("/review/{draw_no}")
def review(draw_no: int):
if db.get_draw(draw_no) is None:
from fastapi import HTTPException
raise HTTPException(404, f"no draw {draw_no}")
return backtest.build_review_payload(draw_no)
@router.post("/run-forward")
def run_forward(draw_no: int = Query(...), k: int = 5000, pool_n: int = 20000):
return backtest.run_forward_purchase(draw_no=draw_no, k=k, pool_n=pool_n)
def run_forward(
background_tasks: BackgroundTasks,
draw_no: int = Query(...),
k: int = Query(5000, ge=1, le=5000),
pool_n: int = Query(20000, ge=1000, le=20000),
):
background_tasks.add_task(backtest.run_forward_purchase, draw_no, k, pool_n)
return {"ok": True, "queued": True, "draw_no": draw_no}
@router.post("/backfill")