stock-scraper / api /reset.py
sbasu2512's picture
Add new metrics to the dashboard
5f57b16
Raw History Blame Contribute Delete
1.56 kB
"""Administrative endpoints for replacing derived prediction data."""
from datetime import datetime
from api.scheduler import IST
from fastapi import APIRouter, Depends, HTTPException
from api.auth import require_pipeline_guid
from app.config import settings
from database.repository import PredictionRepository
from jobs.rebuild_predictions import rebuild_predictions
router = APIRouter(prefix="/reset", tags=["reset-data"])
@router.post("/purge-predictions", dependencies=[Depends(require_pipeline_guid)])
def purge_predictions() -> dict[str, object]:
"""Permanently remove all rows from the predictions table."""
deleted_rows = PredictionRepository(settings.DATABASE_URL).delete_all_predictions()
return {"status": "purged", "deleted_rows": deleted_rows}
@router.post("/rebuild-preds", dependencies=[Depends(require_pipeline_guid)])
def rebuild_preds() -> dict[str, object]:
"""Recreate predictions across every distinct trading date in OHLCV."""
now = datetime.now(IST)
# Do not allow historical prediction rebuilds during the
# daily production pipeline window: 15:00–17:00 IST.
if 15 <= now.hour < 17:
raise HTTPException(
status_code=409,
detail=(
"Manual Prediction rebuild is disabled between "
"3:00 PM and 5:00 PM IST."
),
)
try:
result = rebuild_predictions()
except RuntimeError as exc:
raise HTTPException(status_code=424, detail=str(exc)) from exc
return {"status": "rebuilt", **result}