This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
"""Backfill production correlation from saved IS metrics without platform calls."""
|
||||
|
||||
import math
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "0013"
|
||||
down_revision = "0012"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
table = sa.table("alphas", sa.column("id", sa.String()),
|
||||
sa.column("is_metrics", sa.JSON()), sa.column("prod_correlation", sa.Float()))
|
||||
connection = op.get_bind()
|
||||
last_id = None
|
||||
while True:
|
||||
query = sa.select(table.c.id, table.c.is_metrics).where(table.c.prod_correlation.is_(None)).order_by(table.c.id).limit(500)
|
||||
if last_id is not None:
|
||||
query = query.where(table.c.id > last_id)
|
||||
rows = connection.execute(query).mappings().all()
|
||||
if not rows:
|
||||
break
|
||||
for row in rows:
|
||||
metrics = row["is_metrics"]
|
||||
value = metrics.get("prodCorrelation") if isinstance(metrics, dict) else None
|
||||
if value is None or isinstance(value, bool):
|
||||
continue
|
||||
try:
|
||||
value = float(value)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if math.isfinite(value):
|
||||
connection.execute(table.update().where(table.c.id == row["id"]).values(prod_correlation=value))
|
||||
last_id = rows[-1]["id"]
|
||||
|
||||
|
||||
def downgrade():
|
||||
# A data correction has no schema to undo; preserve recovered observations.
|
||||
pass
|
||||
Reference in New Issue
Block a user