feat: add scoped alpha sync and local self-correlation
This commit is contained in:
+28
-2
@@ -4,9 +4,24 @@ import math
|
||||
import re
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy import or_, select, update
|
||||
|
||||
from .models import Alpha, Research, ResearchTag, SelfCorrelation, now
|
||||
|
||||
|
||||
def submission_condition(submission):
|
||||
"""Match the platform list contract; a missing status is never assumed submitted."""
|
||||
return Alpha.status == "UNSUBMITTED" if submission == "UNSUBMITTED" else Alpha.status != "UNSUBMITTED"
|
||||
|
||||
|
||||
async def invalidate_correlations(db, alpha_id, regions=()):
|
||||
"""A changed baseline or PnL invalidates local conclusions without touching platform checks."""
|
||||
await db.execute(
|
||||
update(SelfCorrelation)
|
||||
.where(or_(SelfCorrelation.alpha_id == alpha_id, SelfCorrelation.region.in_(regions)))
|
||||
.values(stale=True)
|
||||
)
|
||||
|
||||
from .models import Alpha, Research, ResearchTag, now
|
||||
|
||||
SENSITIVE_KEYS = {
|
||||
"password",
|
||||
@@ -67,6 +82,8 @@ async def upsert_alpha(db, raw: dict):
|
||||
if not isinstance(alpha_id, str) or not alpha_id:
|
||||
raise ValueError("Alpha 数据缺少 ID")
|
||||
item = await db.get(Alpha, alpha_id)
|
||||
previous_region = item.region if item else None
|
||||
previous_status = item.status if item else None
|
||||
if item is None:
|
||||
item = Alpha(id=alpha_id)
|
||||
db.add(item)
|
||||
@@ -78,6 +95,13 @@ async def upsert_alpha(db, raw: dict):
|
||||
item.alpha_type, item.language = raw.get("type"), settings.get("language")
|
||||
item.stage, item.status, item.hidden = raw.get("stage"), raw.get("status"), raw.get("hidden") is True
|
||||
item.region, item.universe = settings.get("region"), settings.get("universe")
|
||||
if previous_region != item.region or previous_status != item.status:
|
||||
regions = {
|
||||
region
|
||||
for region, status in ((previous_region, previous_status), (item.region, item.status))
|
||||
if region and status and status != "UNSUBMITTED"
|
||||
}
|
||||
await invalidate_correlations(db, alpha_id, regions)
|
||||
item.settings, item.is_metrics = sanitize(settings), sanitize(metrics)
|
||||
item.os_metrics = sanitize(raw.get("os")) if isinstance(raw.get("os"), dict) else {}
|
||||
item.checks = sanitize(metrics.get("checks") or raw.get("checks") or [])
|
||||
@@ -93,6 +117,8 @@ async def upsert_alpha(db, raw: dict):
|
||||
|
||||
def list_statement(filters):
|
||||
query = select(Alpha, Research).join(Research, Research.alpha_id == Alpha.id)
|
||||
if filters.submission:
|
||||
query = query.where(submission_condition(filters.submission))
|
||||
q = filters.q
|
||||
if q:
|
||||
pattern = "%" + q.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%"
|
||||
|
||||
Reference in New Issue
Block a user