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

@@ -444,6 +444,37 @@ class DeliveryHistoryService:
cols = ["deliveryid", "userid", "pickupcustomer", "pickuptime", "deliverytime", "dlat", "dlon", "plat", "plon"]
return [dict(zip(cols, r)) for r in rows]
def get_pattern_records(self, days: int) -> List[Dict[str, Any]]:
"""
Shape the local nearledb mirror into the record format the delivery-
history pattern store (Phase-0 kitchen+zone rider lookup) expects:
kitchen/pickuplat/pickuplon/deliverylat/deliverylong/userid/ridername.
`ridername` is always "" — delivery_raw doesn't store it (it's
display-only in the /ml/history debug endpoint; every actual matching
decision is keyed on userid, so this doesn't affect assignment).
"""
records: List[Dict[str, Any]] = []
for r in self._load_raw_rows(days):
try:
plat = float(r.get("plat") or 0)
plon = float(r.get("plon") or 0)
dlat = float(r.get("dlat") or 0)
dlon = float(r.get("dlon") or 0)
uid = int(float(r.get("userid") or 0))
if not plat or not dlat or uid == 0:
continue
records.append({
"kitchen": (r.get("pickupcustomer") or "").strip().lower(),
"pickuplat": plat, "pickuplon": plon,
"deliverylat": dlat, "deliverylong": dlon,
"userid": uid,
"ridername": "",
})
except (TypeError, ValueError):
continue
return records
def sample_batches(self, days: int = 14, min_drops: int = 4, max_drops: int = 15,
limit: int = 10) -> List[Dict[str, Any]]:
"""
@@ -623,12 +654,24 @@ class DeliveryHistoryService:
full: bool = False) -> Dict[str, Any]:
"""
Full pipeline: incremental DB sync -> prune local store -> rebuild
aggregates locally. This is what the scheduler and the admin endpoint
call. The DB is touched only by the sync step (new rows only).
aggregates locally -> (if pattern_source=db) rebuild the Phase-0
pattern store from the same synced rows. This is what the scheduler
and the admin endpoint call. The DB is touched only by the sync step
(new rows only).
"""
from app.config.dynamic_config import get_config
cfg = get_config()
pattern_on = str(cfg.get("pattern_source", "csv")) == "db"
pattern_days = int(cfg.get("pattern_history_days", 30))
# Local retention must cover whichever consumer needs more history
# (the ETA window vs. the pattern-store window) without changing the
# ETA computation's own `days` window below — that backtest result is
# already validated at 14 days and this must not perturb it.
retain_days = max(days, pattern_days) if pattern_on else days
with _WRITE_LOCK:
try:
sync = self.sync_from_db(days=days, tenant_id=tenant_id, full=full)
sync = self.sync_from_db(days=retain_days, tenant_id=tenant_id, full=full)
except Exception as e:
logger.error(f"[DeliveryHistory] sync failed: {e}", exc_info=True)
# Still try to serve whatever is already local.
@@ -636,9 +679,23 @@ class DeliveryHistoryService:
self._last_summary = {"status": "sync_failed", "error": str(e), "rebuild": rebuilt}
return self._last_summary
self._prune_raw(days)
self._prune_raw(retain_days)
rebuilt = self.rebuild_aggregates(days)
self._last_summary = {"status": "ok", "sync": sync, "rebuild": rebuilt}
pattern_rebuild = None
if pattern_on:
try:
from app.services.vector.delivery_history_store import get_delivery_history_store
records = self.get_pattern_records(pattern_days)
n = get_delivery_history_store().rebuild_from_records(records)
pattern_rebuild = {"records": n, "days": pattern_days}
except Exception as e:
logger.warning(f"[DeliveryHistory] pattern-store rebuild skipped: {e}")
self._last_summary = {
"status": "ok", "sync": sync, "rebuild": rebuilt,
"pattern_rebuild": pattern_rebuild,
}
return self._last_summary
# -- cache + lookup -----------------------------------------------------