fix(tradein): sber series fallback continues on error + benchmark gated to ЕКБ region (#794 review)

This commit is contained in:
bot-backend 2026-05-31 21:37:41 +03:00
parent 3485b977f1
commit 468bdb052b
2 changed files with 30 additions and 27 deletions

View file

@ -965,8 +965,8 @@ def _load_sber_index_series(db: Session, *, region: str) -> dict[date, float]:
.all() .all()
) )
except Exception as exc: except Exception as exc:
logger.warning("sber_price_index lookup failed (graceful, no-op): %s", exc) logger.warning("sber_price_index lookup failed for %s (graceful): %s", dash, exc)
return {} continue
if rows: if rows:
return {r["period_month"]: float(r["index_value_rub_m2"]) for r in rows} return {r["period_month"]: float(r["index_value_rub_m2"]) for r in rows}
return {} return {}

View file

@ -380,32 +380,35 @@ async def pull_sber_indices(
latest_month.isoformat(), latest_month.isoformat(),
latest_val, latest_val,
) )
# Task B: best-effort reconciliation to our scrape-median asking ppm² # our listings median is ЕКБ-only →
try: # compare only against Свердловская обл. (REF_AREA 66)
our_median = db.execute( if ref_area == "66":
text(""" # Task B: best-effort reconciliation to our scrape-median asking ppm²
SELECT percentile_cont(0.5) try:
WITHIN GROUP (ORDER BY price_per_m2) AS m our_median = db.execute(
FROM listings text("""
WHERE is_active = true SELECT percentile_cont(0.5)
AND price_per_m2 BETWEEN 50000 AND 800000 WITHIN GROUP (ORDER BY price_per_m2) AS m
""") FROM listings
).scalar() WHERE is_active = true
if our_median is not None and latest_val: AND price_per_m2 BETWEEN 50000 AND 800000
sber_latest = latest_val """)
divergence_pct = (our_median - sber_latest) / sber_latest * 100 ).scalar()
logger.info( if our_median is not None and latest_val:
"sber_index benchmark [asking vs our median]: " sber_latest = latest_val
"sber=%.0f our_median=%.0f divergence=%+.1f%% " divergence_pct = (our_median - sber_latest) / sber_latest * 100
"(data-quality signal)", logger.info(
sber_latest, "sber_index benchmark [asking vs our median]: "
our_median, "sber=%.0f our_median=%.0f divergence=%+.1f%% "
divergence_pct, "(data-quality signal)",
sber_latest,
our_median,
divergence_pct,
)
except Exception as exc:
logger.debug(
"sber benchmark reconciliation skipped (graceful): %s", exc
) )
except Exception as exc:
logger.debug(
"sber benchmark reconciliation skipped (graceful): %s", exc
)
except httpx.HTTPStatusError as exc: except httpx.HTTPStatusError as exc:
logger.error( logger.error(