Expand AI insight: fix broken competitor query, add continuity, 30-day horizon, holidays

- Fix gather_competitor_data() tier filter: it queried tier IN ('primary','secondary'),
  values that never exist (real values are 'own'/'competitor'/'market'), so the
  cheapest-competitor comparison has always silently returned nothing.
- Feed the previous insight back into the prompt so the model can note what's
  changed/resolved instead of repeating itself.
- Extend forecast horizon from 14 to 30 days; add a per-day revenue table
  alongside the existing occupancy table.
- Annotate the occupancy table with UK (England) bank holidays.
- Add a same-channel market-movement section (B.com vs B.com, rack vs rack)
  diffing rates against the last insight's snapshot, threshold £3.
- Add a parsed headline field + insight history list on the Dashboard,
  collapsed to headline/age and expandable to full content.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-24 18:03:31 +00:00
parent 2f5349bd1c
commit 2a7ee1d6b8
9 changed files with 381 additions and 83 deletions

View file

@ -4,6 +4,8 @@ Auth is handled by the central HNF stack cookie (hnf_session).
"""
import logging
import sys
import os
import time
from contextlib import asynccontextmanager
logging.basicConfig(
@ -105,6 +107,9 @@ async def lifespan(app: FastAPI):
CREATE INDEX IF NOT EXISTS idx_ai_insights_generated
ON ai_insights(generated_at DESC)
"""))
db.execute(text("""
ALTER TABLE ai_insights ADD COLUMN IF NOT EXISTS headline TEXT
"""))
db.commit()
finally:
db.close()
@ -122,6 +127,7 @@ app = FastAPI(
version="2.0.0",
lifespan=lifespan,
)
STARTED_AT = str(int(time.time() * 1000))
app.add_middleware(
CORSMiddleware,
@ -155,4 +161,4 @@ app.include_router(ai_insights.router, prefix="/ai-insights", tags=[
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": "forecasting-api"}
return {"status": "healthy", "service": "forecasting-api", "version": os.environ.get("BUILD_VERSION", STARTED_AT)}