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
+15 -2
View File
@@ -10,7 +10,7 @@ from uuid import uuid4
from fastapi import HTTPException
from sqlalchemy import delete, func, select, update
from .alphas import list_statement, sorted_statement, summary
from .alphas import list_statement, sorted_statement, submission_condition, summary
from .jobs import ACTIVE
from .models import Account, Alpha, Job, JobItem, Pnl, Research, ResearchTag, SelfCorrelation, now
from .research.provenance import alpha_sources, source_kinds
@@ -203,9 +203,22 @@ class Business:
for job in (
await self.db.scalars(select(Job).where(Job.kind == body.kind, Job.status.in_(ACTIVE)))
).all():
if job.payload == payload:
if body.kind == "pnl_backfill" or job.payload == payload:
return JobOutput.model_validate(job).model_dump(mode="json")
job = Job(id=str(uuid4()), kind=body.kind, payload=payload)
if body.kind == "pnl_backfill":
# Fix the full missing set on the server, independently of UI paging.
# The account lock above also serializes duplicate button clicks.
ids = list((await self.db.scalars(
select(Alpha.id)
.outerjoin(Pnl, Pnl.alpha_id == Alpha.id)
.where(submission_condition("SUBMITTED"), Pnl.alpha_id.is_(None))
.order_by(Alpha.id)
)).all())
job.payload = {"alpha_ids": ids, "submission": "SUBMITTED"}
job.total = len(ids)
if not ids:
job.status = "completed"
self.db.add(job)
await self.db.flush()
return JobOutput.model_validate(job).model_dump(mode="json")