Optimize signal read paths and enforce score invariants
This commit is contained in:
@@ -16,6 +16,7 @@ from datetime import datetime, timezone
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database import insert_for_session
|
||||
from app.exceptions import NotFoundError, ValidationError
|
||||
from app.models.score import CompositeScore, DimensionScore
|
||||
from app.models.ticker import Ticker
|
||||
@@ -661,14 +662,23 @@ async def compute_dimension_score(
|
||||
# Can't compute — mark stale
|
||||
existing.is_stale = True
|
||||
elif score_val is not None:
|
||||
dim = DimensionScore(
|
||||
stmt = insert_for_session(db, DimensionScore).values(
|
||||
ticker_id=ticker.id,
|
||||
dimension=dimension,
|
||||
score=score_val,
|
||||
is_stale=False,
|
||||
computed_at=now,
|
||||
)
|
||||
db.add(dim)
|
||||
await db.execute(
|
||||
stmt.on_conflict_do_update(
|
||||
index_elements=["ticker_id", "dimension"],
|
||||
set_={
|
||||
"score": stmt.excluded.score,
|
||||
"is_stale": False,
|
||||
"computed_at": stmt.excluded.computed_at,
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
return score_val
|
||||
|
||||
@@ -749,14 +759,24 @@ async def compute_composite_score(
|
||||
existing.weights_json = json.dumps(weights)
|
||||
existing.computed_at = now
|
||||
else:
|
||||
comp = CompositeScore(
|
||||
stmt = insert_for_session(db, CompositeScore).values(
|
||||
ticker_id=ticker.id,
|
||||
score=composite,
|
||||
is_stale=False,
|
||||
weights_json=json.dumps(weights),
|
||||
computed_at=now,
|
||||
)
|
||||
db.add(comp)
|
||||
await db.execute(
|
||||
stmt.on_conflict_do_update(
|
||||
index_elements=["ticker_id"],
|
||||
set_={
|
||||
"score": stmt.excluded.score,
|
||||
"is_stale": False,
|
||||
"weights_json": stmt.excluded.weights_json,
|
||||
"computed_at": stmt.excluded.computed_at,
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
return composite, missing
|
||||
|
||||
|
||||
Reference in New Issue
Block a user