Deploy AIOS web (React glide grid + FastAPI slice)
Browse files- api/routes_customers.py +19 -1
- api/routes_keychain.py +95 -6
api/routes_customers.py
CHANGED
|
@@ -66,11 +66,29 @@ def _pool_rows(session: Session):
|
|
| 66 |
def _pool_for(rt, team_id, agent):
|
| 67 |
"""The cached pool for an explicit scope β session-free so the prewarm thread and the
|
| 68 |
stale-refresh path can call it. STALE-WHILE-REFRESH (scope_cache): once a copy exists no
|
| 69 |
-
request blocks on the 10β30s Odoo rebuild again; only a scope's FIRST-ever build does.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
import modules.customer_data as cl
|
|
|
|
| 71 |
|
| 72 |
key = ("pool", team_id, agent)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def _build():
|
| 75 |
# β THE SCOPE GOES INTO THE BUILDER. `pool(agent_name, team_id)` is the same reconciled
|
| 76 |
# builder the Streamlit page uses β passing the scope here is what makes the isolation a
|
|
|
|
| 66 |
def _pool_for(rt, team_id, agent):
|
| 67 |
"""The cached pool for an explicit scope β session-free so the prewarm thread and the
|
| 68 |
stale-refresh path can call it. STALE-WHILE-REFRESH (scope_cache): once a copy exists no
|
| 69 |
+
request blocks on the 10β30s Odoo rebuild again; only a scope's FIRST-ever build does.
|
| 70 |
+
|
| 71 |
+
DEBT-2 (2026-08-04): while the tenant's RESOLVED Odoo source is PAUSED this path never
|
| 72 |
+
goes live β it serves the in-process copy at any age, else the persisted pause-time
|
| 73 |
+
snapshot (restart-safe), else answers 503. Never a WIDER scope's snapshot: handing a
|
| 74 |
+
scoped user the consolidated rows would widen their book, which is worse than an error."""
|
| 75 |
import modules.customer_data as cl
|
| 76 |
+
import routes_keychain
|
| 77 |
|
| 78 |
key = ("pool", team_id, agent)
|
| 79 |
|
| 80 |
+
if routes_keychain.odoo_paused(rt):
|
| 81 |
+
hit = rt.pool_cache.get(key)
|
| 82 |
+
if hit:
|
| 83 |
+
return hit[1]
|
| 84 |
+
snap = routes_keychain.load_pool_snapshot(rt, team_id, agent)
|
| 85 |
+
if snap is not None:
|
| 86 |
+
rt.pool_cache[key] = snap # seed, so the memo stamps stay coherent
|
| 87 |
+
return snap[1]
|
| 88 |
+
raise err(503, "connector_paused",
|
| 89 |
+
"this data source is paused and no snapshot exists for your scope β "
|
| 90 |
+
"an admin can resume it under Settings β Connectors")
|
| 91 |
+
|
| 92 |
def _build():
|
| 93 |
# β THE SCOPE GOES INTO THE BUILDER. `pool(agent_name, team_id)` is the same reconciled
|
| 94 |
# builder the Streamlit page uses β passing the scope here is what makes the isolation a
|
api/routes_keychain.py
CHANGED
|
@@ -20,6 +20,9 @@ from routes_admin import admin_gate
|
|
| 20 |
router = APIRouter(prefix="/api/v1")
|
| 21 |
|
| 22 |
_CONNECTOR_FLAGS_KEY = "connectors"
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def _kc():
|
|
@@ -27,6 +30,82 @@ def _kc():
|
|
| 27 |
return keychain
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
@router.get("/admin/keychain")
|
| 31 |
def list_keychain(session: Session = Depends(admin_gate)):
|
| 32 |
kc = _kc()
|
|
@@ -126,12 +205,14 @@ def connectors(session: Session = Depends(admin_gate)):
|
|
| 126 |
"active": (e["type"] == "odoo" and resolved == f"keychain:{e['id']}"),
|
| 127 |
"paused": bool((flags.get(e["id"]) or {}).get("paused"))})
|
| 128 |
out = {"connectors": rows, "locked": not kc.unlocked(), "resolved": resolved,
|
| 129 |
-
#
|
| 130 |
-
#
|
| 131 |
-
#
|
|
|
|
| 132 |
"pausedNote": ("Pausing a connector never deletes data β notes, custom fields and "
|
| 133 |
-
"views stay.
|
| 134 |
-
"
|
|
|
|
| 135 |
if session.tenant == "royal-imports":
|
| 136 |
out["unsynced"] = _unsynced_customer_records(session)
|
| 137 |
return out
|
|
@@ -144,6 +225,14 @@ def pause_connector(key: str, body: dict = Body(default=None),
|
|
| 144 |
if not session.runtime.available():
|
| 145 |
raise err(503, "store_unavailable", "the tenant store is unavailable")
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
def _up(cur):
|
| 148 |
cur[str(key)] = {"paused": paused}
|
| 149 |
return cur
|
|
@@ -152,4 +241,4 @@ def pause_connector(key: str, body: dict = Body(default=None),
|
|
| 152 |
session.runtime.update(_CONNECTOR_FLAGS_KEY, _up)
|
| 153 |
except Exception:
|
| 154 |
raise err(503, "store_unavailable", "the change was not saved β try again")
|
| 155 |
-
return {"key": key, "paused": paused}
|
|
|
|
| 20 |
router = APIRouter(prefix="/api/v1")
|
| 21 |
|
| 22 |
_CONNECTOR_FLAGS_KEY = "connectors"
|
| 23 |
+
#: DEBT-2 (2026-08-04): the last-successful-sync snapshot bucket. Written when the RESOLVED
|
| 24 |
+
#: odoo connector is paused; read by the pool path while paused; survives a Space restart.
|
| 25 |
+
SNAPSHOT_KEY = "connector_snapshots"
|
| 26 |
|
| 27 |
|
| 28 |
def _kc():
|
|
|
|
| 30 |
return keychain
|
| 31 |
|
| 32 |
|
| 33 |
+
def _resolved_odoo_key(rt):
|
| 34 |
+
"""Which source would serve this tenant's Odoo queries β the ROUTE-LEVEL mirror of
|
| 35 |
+
TenantRuntime.odoo_source(): first unlocked keychain odoo entry, else env for tenant #0
|
| 36 |
+
only, else None. Takes the runtime (not the session) so the pool path can ask too."""
|
| 37 |
+
kc = _kc()
|
| 38 |
+
try:
|
| 39 |
+
entries = kc.list_entries(rt)
|
| 40 |
+
except Exception:
|
| 41 |
+
entries = []
|
| 42 |
+
first_odoo = next((e["id"] for e in entries if e["type"] == "odoo"), None)
|
| 43 |
+
if first_odoo and kc.unlocked():
|
| 44 |
+
return f"keychain:{first_odoo}", first_odoo
|
| 45 |
+
if rt.key == "royal-imports" and os.environ.get("ODOO_URL"):
|
| 46 |
+
return "env", "odoo-env"
|
| 47 |
+
return None, None
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def odoo_paused(rt):
|
| 51 |
+
"""True when the tenant's RESOLVED Odoo source carries the pause flag. Pausing an entry
|
| 52 |
+
that is not the resolved source freezes nothing β it serves nothing."""
|
| 53 |
+
try:
|
| 54 |
+
_, flag_key = _resolved_odoo_key(rt)
|
| 55 |
+
if not flag_key:
|
| 56 |
+
return False
|
| 57 |
+
flags = rt.get(_CONNECTOR_FLAGS_KEY) or {}
|
| 58 |
+
return bool((flags.get(flag_key) or {}).get("paused"))
|
| 59 |
+
except Exception:
|
| 60 |
+
return False # an unreachable flags bucket must never freeze a live surface
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def _snap_scope_key(team_id, agent):
|
| 64 |
+
return f"t={team_id}|a={agent}"
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def load_pool_snapshot(rt, team_id, agent):
|
| 68 |
+
"""(ts, rows) from the persisted snapshot for this exact scope, or None. NEVER a wider
|
| 69 |
+
scope's rows β serving the consolidated snapshot to a scoped user would widen their book."""
|
| 70 |
+
try:
|
| 71 |
+
snap = (rt.get(SNAPSHOT_KEY) or {}).get("odoo_pool") or {}
|
| 72 |
+
e = snap.get(_snap_scope_key(team_id, agent))
|
| 73 |
+
if isinstance(e, dict) and isinstance(e.get("rows"), list):
|
| 74 |
+
return float(e.get("ts") or 0), e["rows"]
|
| 75 |
+
except Exception:
|
| 76 |
+
pass
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def save_pool_snapshots(rt, taken_by=""):
|
| 81 |
+
"""Persist every currently-cached pool scope as the pause-time snapshot ('the last
|
| 82 |
+
successful sync', made concrete). Ensures the consolidated default scope exists first so
|
| 83 |
+
a pause on a cold process still captures something to serve."""
|
| 84 |
+
import time as _time
|
| 85 |
+
import routes_customers as _rc
|
| 86 |
+
try:
|
| 87 |
+
_rc._pool_for(rt, None, None) # the scope every admin/all-BU account lands on
|
| 88 |
+
except Exception:
|
| 89 |
+
pass # cold + Odoo down: persist whatever IS cached
|
| 90 |
+
pools = {}
|
| 91 |
+
for key, entry in list(rt.pool_cache.items()):
|
| 92 |
+
if (isinstance(key, tuple) and len(key) == 3 and key[0] == "pool"
|
| 93 |
+
and isinstance(entry, tuple) and len(entry) == 2
|
| 94 |
+
and isinstance(entry[1], list)):
|
| 95 |
+
pools[_snap_scope_key(key[1], key[2])] = {"ts": entry[0], "rows": entry[1]}
|
| 96 |
+
if not pools:
|
| 97 |
+
return 0
|
| 98 |
+
|
| 99 |
+
def _up(cur):
|
| 100 |
+
cur["odoo_pool"] = pools
|
| 101 |
+
cur["taken"] = _time.strftime("%Y-%m-%dT%H:%M:%S")
|
| 102 |
+
cur["takenBy"] = str(taken_by or "")
|
| 103 |
+
return cur
|
| 104 |
+
|
| 105 |
+
rt.update(SNAPSHOT_KEY, _up, flush="sync")
|
| 106 |
+
return len(pools)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
@router.get("/admin/keychain")
|
| 110 |
def list_keychain(session: Session = Depends(admin_gate)):
|
| 111 |
kc = _kc()
|
|
|
|
| 205 |
"active": (e["type"] == "odoo" and resolved == f"keychain:{e['id']}"),
|
| 206 |
"paused": bool((flags.get(e["id"]) or {}).get("paused"))})
|
| 207 |
out = {"connectors": rows, "locked": not kc.unlocked(), "resolved": resolved,
|
| 208 |
+
# DEBT-2 (2026-08-04): pause now MEANS something β the customer pool freezes on the
|
| 209 |
+
# pause-time snapshot (persisted, restart-safe) and Unsynced records counts against
|
| 210 |
+
# that snapshot. Stated honestly: measure/page computations still reach the source
|
| 211 |
+
# for questions not already memoized; their full freeze rides the module cutovers.
|
| 212 |
"pausedNote": ("Pausing a connector never deletes data β notes, custom fields and "
|
| 213 |
+
"views stay. While paused, the customer workspace serves the "
|
| 214 |
+
"pause-time snapshot instead of reading live; measures not already "
|
| 215 |
+
"computed may still reach the source until their cutover.")}
|
| 216 |
if session.tenant == "royal-imports":
|
| 217 |
out["unsynced"] = _unsynced_customer_records(session)
|
| 218 |
return out
|
|
|
|
| 225 |
if not session.runtime.available():
|
| 226 |
raise err(503, "store_unavailable", "the tenant store is unavailable")
|
| 227 |
|
| 228 |
+
# DEBT-2: pausing the RESOLVED Odoo source captures the snapshot FIRST, so there is a
|
| 229 |
+
# "last successful sync" to serve before the freeze takes effect. Capturing before the
|
| 230 |
+
# flag flips means a failed capture leaves the connector live (never paused-with-nothing).
|
| 231 |
+
snapshots = 0
|
| 232 |
+
_, flag_key = _resolved_odoo_key(session.runtime)
|
| 233 |
+
if paused and flag_key and str(key) == flag_key:
|
| 234 |
+
snapshots = save_pool_snapshots(session.runtime, taken_by=session.uname)
|
| 235 |
+
|
| 236 |
def _up(cur):
|
| 237 |
cur[str(key)] = {"paused": paused}
|
| 238 |
return cur
|
|
|
|
| 241 |
session.runtime.update(_CONNECTOR_FLAGS_KEY, _up)
|
| 242 |
except Exception:
|
| 243 |
raise err(503, "store_unavailable", "the change was not saved β try again")
|
| 244 |
+
return {"key": key, "paused": paused, "snapshots": snapshots}
|