Gaurav711's picture
deploy: revert to 733e96b
d491dc1
Raw
History Blame Contribute Delete
8.99 kB
import random
import datetime
import pandas as pd
import numpy as np
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from backend.db.session import get_db
from backend.db.models import Inventory, SalesEvent
from backend.core.logger import get_logger
from backend.core.state import demand_forecaster, profitability_scorer, safeguards, GLOBAL_STATS
# For retrain endpoint
import backend.core.state as state
logger = get_logger(__name__)
router = APIRouter()
@router.get("/forecast/{store_id}/{sku_id}")
async def get_forecast(store_id: str, sku_id: str, db: Session = Depends(get_db)):
inv_item = db.query(Inventory).filter(Inventory.store_id == store_id, Inventory.sku_id == sku_id).first()
recent_sale = db.query(SalesEvent).filter(SalesEvent.sku_id == sku_id).order_by(SalesEvent.created_at.desc()).first()
temp = recent_sale.weather_temp if recent_sale and recent_sale.weather_temp else float(random.uniform(20.0, 35.0))
rain = recent_sale.weather_rain if recent_sale and recent_sale.weather_rain else float(random.exponential(1.5))
elapsed_time = recent_sale.time_elapsed_sec if recent_sale and recent_sale.time_elapsed_sec else float(random.normalvariate(900.0, 200.0))
test_df = pd.DataFrame([{"weather_temp": temp, "weather_rain": rain, "time_elapsed_sec": elapsed_time}])
clipped_df, clip_alerts = safeguards.validate_and_clip(test_df)
unit_alerts = safeguards.check_unit_consistency(clipped_df)
X_pred = clipped_df.values
point, lower, upper = demand_forecaster.predict_with_intervals(X_pred)
current_stock = inv_item.qty_available if inv_item else 25
sku_name = inv_item.sku_name if inv_item else f"SKU {sku_id}"
return {
"store_id": store_id,
"sku_id": sku_id,
"sku_name": sku_name,
"current_stock": current_stock,
"features": {
"temp": round(temp, 2),
"rain": round(rain, 2),
"elapsed_time_sec": round(elapsed_time, 1)
},
"forecast": {
"point_forecast": round(float(point[0]), 2),
"ci_lower": round(float(lower[0]), 2),
"ci_upper": round(float(upper[0]), 2),
"safety_stock_units": round(float(upper[0] * 1.15), 1),
"model_version": "Tobit-LGBM-v2.0"
},
"safeguard_events": {
"clipped": len(clip_alerts) > 0,
"unit_anomaly": len(unit_alerts) > 0,
"alerts": clip_alerts + unit_alerts
}
}
@router.get("/forecast/{store_id}/restock-alerts")
async def get_restock_alerts(store_id: str, db: Session = Depends(get_db)):
alerts = []
inv_rows = db.query(Inventory).filter(Inventory.store_id == store_id).all()
for item in inv_rows:
if item.qty_available <= 5: # Critical threshold
alerts.append({
"sku_id": item.sku_id,
"sku_name": item.sku_name,
"stock": item.qty_available,
"safety_stock": 50,
"suggested_restock": 50 - item.qty_available
})
return alerts
@router.get("/metrics/availability/{store_id}")
async def get_availability_metrics(store_id: str, db: Session = Depends(get_db)):
metrics = GLOBAL_STATS["availability_metrics"].copy()
metrics["store_id"] = store_id
total_items = db.query(Inventory).filter(Inventory.store_id == store_id).count()
if total_items > 0:
in_stock_items = db.query(Inventory).filter(Inventory.store_id == store_id, Inventory.qty_available > 0).count()
metrics["availability_rate"] = round(in_stock_items / max(1, total_items), 3)
metrics["total_skus_tracked"] = total_items
metrics["in_stock_skus"] = in_stock_items
return metrics
@router.get("/metrics/bump-rate")
async def get_bump_rate():
# Return simulated Display ETA Jitter metrics
raw = GLOBAL_STATS["raw_mimo_bumps"]
gated = GLOBAL_STATS["gated_smoother_bumps"]
pct = round(((raw - gated) / max(1, raw) * 100), 1)
return {
"raw_mimo_bumps": raw,
"gated_smoother_bumps": gated,
"jitter_suppression_pct": pct,
"zone_status": "MONSOON_STORM_SURGE_GATED"
}
@router.get("/profitability/{store_id}")
async def get_store_profitability(store_id: str):
# Exposes Dark Store Profitability predictions (Cox survival curve analysis)
# Feature matrix: pop_density, comp_density, dist_to_profitable, skus, aov, non_grocery
mock_profiles = {
"store_01": [8.5, 3, 1.4, 4.2, 5.8, 0.28], # High density, Whitefield
"store_02": [6.2, 1, 2.8, 3.0, 4.5, 0.15], # Koramangala
"store_03": [7.8, 4, 3.5, 3.5, 5.0, 0.20] # Indiranagar
}
profile = mock_profiles.get(store_id, [5.0, 2, 4.0, 2.5, 4.0, 0.10])
# Calculate Cox Proportional Hazard results
X_arr = np.array([profile])
survival_curve = profitability_scorer.predict_survival_curve(X_arr)
expected_months = profitability_scorer.predict_time_to_profit(X_arr)
# Base recommendations
recommendation = "HOLD EXPANSION: High competitive saturation in radius."
if expected_months <= 8.0:
recommendation = "HIGH ALLOCATION: Strong organic density with solid non-grocery share."
elif expected_months <= 12.0:
recommendation = "MEDIUM ALLOCATION: Optimize local SKU mix to focus on pharmacy/electronics."
return {
"store_id": store_id,
"metrics": {
"population_density": profile[0],
"competitors_2km": int(profile[1]),
"distance_profitable_km": profile[2],
"initial_skus_k": profile[3],
"average_aov_inr": int(profile[4] * 100),
"non_grocery_share": profile[5]
},
"profitability_projection": {
"months_to_profit_median": expected_months,
"survival_curve": survival_curve,
"allocation_recommendation": recommendation
}
}
@router.get("/metrics/robustness")
async def get_ml_robustness():
# Instantly returns cached drift metrics without blocking uvicorn event loop
return state.CACHED_ROBUSTNESS_METRICS
@router.post("/ml/retrain")
async def trigger_ml_retrain(db: Session = Depends(get_db)):
logger.info("[MLOPS PIPELINE] Manual retraining triggered via dashboard API gateway.")
try:
sales_events = db.query(SalesEvent).filter(SalesEvent.weather_temp.isnot(None)).order_by(SalesEvent.created_at.desc()).limit(200).all()
if len(sales_events) < 30:
state.CACHED_ROBUSTNESS_METRICS = {
"status": "insufficient_data",
"message": f"Real SalesEvent pipeline requires at least 30 DB records. Currently found {len(sales_events)} records in PostgreSQL.",
"last_audit_timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"features_drift": {
"weather_temp": {"psi": 0.0, "status": "insufficient_data", "message": "Need >= 30 real DB events"},
"weather_rain": {"psi": 0.0, "status": "insufficient_data", "message": "Need >= 30 real DB events"},
"time_elapsed_sec": {"psi": 0.0, "status": "insufficient_data", "message": "Need >= 30 real DB events"}
}
}
return {
"status": "insufficient_data",
"message": f"Found {len(sales_events)}/30 real sales events in Postgres. Real data policy active."
}
prod_df = pd.DataFrame([{
'weather_temp': e.weather_temp,
'weather_rain': e.weather_rain,
'time_elapsed_sec': e.time_elapsed_sec
} for e in sales_events])
drift_metrics = safeguards.calculate_drift_metrics(prod_df)
state.CACHED_ROBUSTNESS_METRICS = {
"status": "nominal",
"last_audit_timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"features_drift": drift_metrics,
"clipping_guard": {
"total_clipped_observations_today": 0,
"active_ranges": {
"temp": f"{safeguards.feature_stats['weather_temp']['p1']:.1f}°C to {safeguards.feature_stats['weather_temp']['p99']:.1f}°C",
"rain": f"{safeguards.feature_stats['weather_rain']['p1']:.1f}mm to {safeguards.feature_stats['weather_rain']['p99']:.1f}mm",
"time_sec": f"{safeguards.feature_stats['time_elapsed_sec']['p1']:.1f}s to {safeguards.feature_stats['time_elapsed_sec']['p99']:.1f}s"
}
},
"unit_warnings": ["REAL_DATA_PIPELINE: Evaluated real PostgreSQL SalesEvent records."]
}
except Exception as e:
logger.error(f"Manual retrain failed: {e}")
return {"status": "error", "message": str(e)}
return {"status": "success", "message": f"Model retraining executed on {len(sales_events)} real sales events."}