feat: add fallback to most recent prices when requested date has no data

- When no prices exist for the requested date, query for the most
  recent available date and return those prices instead
- Log informational message when falling back to alternate date
This commit is contained in:
2026-02-27 18:41:12 -05:00
parent af9c2f99e7
commit fad7c1dc28

View File

@@ -144,6 +144,17 @@ async def get_stored_prices(
CompanyPrice.scrape_date == date CompanyPrice.scrape_date == date
).all() ).all()
# Fallback: if no prices for requested date, get most recent ones
if not prices:
latest_date_result = db.query(CompanyPrice.scrape_date).order_by(
CompanyPrice.scrape_date.desc()
).first()
if latest_date_result:
prices = db.query(CompanyPrice).filter(
CompanyPrice.scrape_date == latest_date_result[0]
).all()
logger.info(f"No prices for {date}, returning {len(prices)} prices from {latest_date_result[0]}")
return [ return [
PriceRecord( PriceRecord(
company_name=p.company_name, company_name=p.company_name,