feat: backfill missing PnL for submitted alphas

This commit is contained in:
yuxuanhui
2026-09-09 20:10:20 +08:00
parent 3d85b073a4
commit 6b4990f100
9 changed files with 179 additions and 12 deletions
+14 -7
View File
@@ -412,21 +412,28 @@ class Runner:
await db.commit()
async def sync_ids(self, job_id, kind, alpha_ids):
"""Process fixed IDs, preserving existing PnL during a missing-only backfill.
Successful items commit individually so cancellation and retries keep
completed work. Cache presence is rechecked when a queued item runs.
"""
pnl_job = kind in ("pnl_refresh", "pnl_backfill")
await self.checkpoint(job_id, {"total": len(alpha_ids)})
for alpha_id in alpha_ids:
async with self.sessions() as db:
previous = await db.get(JobItem, (job_id, alpha_id))
if previous and not previous.error:
continue
await self.checkpoint(job_id, {"next_retry_at": None})
cached = await db.get(Pnl, alpha_id) if kind == "pnl_backfill" else None
await self.checkpoint(job_id, {"next_retry_at": None, "checkpoint": {"alpha_id": alpha_id}})
error = None
try:
raw = await (
self.client.pnl(alpha_id) if kind == "pnl_refresh" else self.client.alpha(alpha_id)
raw = cached.raw if cached is not None else await (
self.client.pnl(alpha_id) if pnl_job else self.client.alpha(alpha_id)
)
if kind != "pnl_refresh" and raw.get("id") != alpha_id:
if not pnl_job and raw.get("id") != alpha_id:
raise ValueError("平台返回的 Alpha ID 与请求不一致")
points = pnl_points(raw) if kind == "pnl_refresh" else None
points = cached.points if cached is not None else pnl_points(raw) if pnl_job else None
except VerificationRequired:
raise
except WqError as exc:
@@ -446,10 +453,10 @@ class Runner:
previous = JobItem(job_id=job_id, alpha_id=alpha_id)
db.add(previous)
if not error:
if kind == "pnl_refresh":
if pnl_job:
if not await db.get(Alpha, alpha_id):
error = "请先导入此 Alpha"
else:
elif kind != "pnl_backfill" or await db.get(Pnl, alpha_id) is None:
await self.save_pnl(db, alpha_id, raw, points)
else:
await db.scalar(select(Account).where(Account.id == 1).with_for_update())