new changes in the api

This commit is contained in:
2026-07-06 15:15:51 +05:30
parent c742ef0e53
commit 871981035a
43 changed files with 414 additions and 1975 deletions

View File

@@ -1,14 +1,14 @@
"""
Dynamic Configuration - rider-api
Replaces all hardcoded hyperparameters with DB-backed values.
The ML hypertuner writes optimal values here; services read from here.
Replaces all hardcoded hyperparameters with DB-backed values. The autonomous
agents (ETA sync, road-sequencing, rider affinity) and the ml_admin API write
tuned values here; services read from here.
Fallback: If DB is unavailable or no tuned values exist, defaults are used.
This means zero risk - the system works day 1 with no data.
"""
import contextvars
import json
import logging
import os
@@ -21,32 +21,9 @@ logger = logging.getLogger(__name__)
# --- DB Path ------------------------------------------------------------------
_DB_PATH = os.getenv("ML_DB_PATH", "ml_data/ml_store.db")
# ---------------------------------------------------------------------------
# Per-request strategy override (async-safe via contextvars).
# Each FastAPI request/asyncio task gets its own copy — no cross-request leaks.
# Usage:
# set_request_strategy("fuel_saver") → override active for this request
# clear_request_strategy() → restore to DB value
# ---------------------------------------------------------------------------
_strategy_override: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar(
"strategy_override", default=None
)
def set_request_strategy(strategy: Optional[str]) -> None:
"""Override ml_strategy for the current async task only (thread-safe)."""
_strategy_override.set(strategy)
def clear_request_strategy() -> None:
"""Remove the per-request strategy override for the current async task."""
_strategy_override.set(None)
# --- Hard Defaults (What the system used before ML) ---------------------------
DEFAULTS: Dict[str, Any] = {
# System Strategy / Prompt
"ml_strategy": "balanced",
# AssignmentService
"max_pickup_distance_km": 10.0,
"max_kitchen_distance_km": 3.0,
@@ -65,9 +42,6 @@ DEFAULTS: Dict[str, Any] = {
"road_factor": 1.3,
# ClusteringService
"cluster_radius_km": 3.0,
# KalmanFilter
"kalman_process_noise": 1e-4,
"kalman_measurement_noise": 0.01,
# RealisticETACalculator
"eta_pickup_time_min": 3.0,
"eta_delivery_time_min": 4.0,
@@ -101,6 +75,18 @@ DEFAULTS: Dict[str, Any] = {
"rider_affinity_enabled": True, # False -> pure curated config
"rider_affinity_min_deliveries": 10, # learned owner needs >= this many deliveries
"rider_affinity_refresh_hours": 6, # recompute cadence (piggybacks the agent)
# Phase-0 kitchen+zone pattern store (delivery_history_store.py): which rider
# historically owns a kitchen->drop-zone pair, used to pre-assign orders.
# "csv" -> legacy: built from delivery_details.csv, only refreshed when a
# human re-exports it and calls POST /ml/reload-history.
# "db" -> built from the already-synced nearledb mirror (delivery_raw),
# rebuilt automatically every eta_sync_interval_hours — no manual
# step, no extra DB load (reuses rows the ETA sync already pulled).
"pattern_source": "csv",
"pattern_history_days": 30, # retention floor for delivery_raw kept for pattern-
# matching volume; independent of eta_history_days
# so the (already-validated) empirical ETA window
# is untouched.
}
@@ -137,18 +123,8 @@ class DynamicConfig:
# --------------------------------------------------------------------------
def get(self, key: str, default: Any = None) -> Any:
"""Get a config value. Returns ML-tuned value if available, else default.
For 'ml_strategy' specifically, a per-request ContextVar override takes
precedence so that hypertuning_params requests don't mutate the shared
singleton (thread-safe for concurrent FastAPI requests).
"""
"""Get a config value. Returns ML-tuned value if available, else default."""
self._maybe_reload()
# Per-request strategy override (async-safe, no cross-request leaks)
if key == "ml_strategy":
override = _strategy_override.get()
if override is not None:
return override
val = self._cache.get(key)
if val is not None:
return val
@@ -163,7 +139,7 @@ class DynamicConfig:
return result
def set(self, key: str, value: Any, source: str = "manual") -> None:
"""Write a config value to DB (used by hypertuner)."""
"""Write a config value to DB (used by the ml_admin API and agents)."""
try:
os.makedirs(os.path.dirname(_DB_PATH) or ".", exist_ok=True)
conn = sqlite3.connect(_DB_PATH)
@@ -185,8 +161,8 @@ class DynamicConfig:
except Exception as e:
logger.error(f"[DynamicConfig] Failed to set {key}: {e}")
def set_bulk(self, params: Dict[str, Any], source: str = "ml_hypertuner") -> None:
"""Write multiple config values at once (called after each Optuna study)."""
def set_bulk(self, params: Dict[str, Any], source: str = "manual") -> None:
"""Write multiple config values at once."""
for key, value in params.items():
self.set(key, value, source=source)
logger.info(f"[DynamicConfig] Bulk update: {len(params)} params from {source}")
@@ -219,16 +195,6 @@ class DynamicConfig:
updated_at TEXT
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS kitchen_encoding (
kitchen_name TEXT PRIMARY KEY,
label_id INTEGER NOT NULL,
frequency REAL DEFAULT 0.0,
avg_profit REAL DEFAULT 0.0,
order_count INTEGER DEFAULT 0,
updated_at TEXT
)
""")
conn.commit()
conn.close()
except Exception as e:
@@ -278,131 +244,4 @@ def get_config() -> DynamicConfig:
__all__ = [
"DynamicConfig",
"get_config",
"set_request_strategy",
"clear_request_strategy",
"get_kitchen_label_id",
"get_kitchen_frequency",
"update_kitchen_stats",
"get_kitchen_avg_profit_smoothed",
]
# --- Kitchen Encoding Persistence ---------------------------------------------
def get_kitchen_label_id(kitchen_name: str) -> int:
"""Get or create persistent label ID for a kitchen."""
try:
conn = sqlite3.connect(_DB_PATH)
row = conn.execute(
"SELECT label_id FROM kitchen_encoding WHERE kitchen_name = ?",
(kitchen_name,),
).fetchone()
if row:
conn.close()
return row[0]
max_id = conn.execute(
"SELECT COALESCE(MAX(label_id), -1) FROM kitchen_encoding"
).fetchone()[0]
new_id = max_id + 1
conn.close()
return new_id
except Exception as e:
logger.warning(f"[KitchenEncoding] Failed to get label_id: {e}")
return hash(kitchen_name.lower().strip()) % 10000
def get_kitchen_frequency(kitchen_name: str) -> float:
"""Get frequency ratio for a kitchen from DB."""
try:
conn = sqlite3.connect(_DB_PATH)
row = conn.execute(
"SELECT frequency FROM kitchen_encoding WHERE kitchen_name = ?",
(kitchen_name,),
).fetchone()
conn.close()
return row[0] if row else 0.0
except Exception:
return 0.0
def update_kitchen_stats(kitchen_name: str, profit: float):
"""Update kitchen stats after order completion."""
try:
conn = sqlite3.connect(_DB_PATH)
row = conn.execute(
"SELECT order_count, avg_profit FROM kitchen_encoding WHERE kitchen_name = ?",
(kitchen_name,),
).fetchone()
if row:
count, avg = row
new_count = count + 1
new_avg = ((avg * count) + profit) / new_count
new_freq = new_count / (
conn.execute(
"SELECT SUM(order_count) FROM kitchen_encoding"
).fetchone()[0]
or 1
)
conn.execute(
"UPDATE kitchen_encoding SET order_count = ?, avg_profit = ?, frequency = ?, updated_at = ? WHERE kitchen_name = ?",
(
new_count,
new_avg,
new_freq,
datetime.utcnow().isoformat(),
kitchen_name,
),
)
else:
conn.execute(
"INSERT INTO kitchen_encoding (kitchen_name, label_id, frequency, avg_profit, order_count, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
(
kitchen_name,
get_kitchen_label_id(kitchen_name),
1.0,
profit,
1,
datetime.utcnow().isoformat(),
),
)
conn.commit()
conn.close()
except Exception as e:
logger.warning(f"[KitchenEncoding] Failed to update stats: {e}")
def get_kitchen_avg_profit_smoothed(
kitchen_name: str, global_avg: float = 40.0, min_samples: int = 5
) -> float:
"""
Get smoothed average profit for a kitchen using Bayesian smoothing.
Reduces noise for kitchens with few orders.
Formula: smoothed = (kitchen_count * kitchen_avg + min_samples * global_avg) / (kitchen_count + min_samples)
This means:
- Kitchen with many samples -> uses its own avg
- Kitchen with few samples -> pulls toward global avg
"""
try:
conn = sqlite3.connect(_DB_PATH)
row = conn.execute(
"SELECT order_count, avg_profit FROM kitchen_encoding WHERE kitchen_name = ?",
(kitchen_name,),
).fetchone()
conn.close()
if row and row[0] > 0:
count, avg = row
if count >= min_samples:
return avg
# Bayesian smoothing
smoothed = ((count * avg) + (min_samples * global_avg)) / (
count + min_samples
)
return smoothed
return global_avg # Unknown kitchen defaults to global avg
except Exception:
return global_avg