rmi-backend/app/_archive/legacy_2026_07/provider_health.py
cryptorugmunch 628c1d2a10
Some checks failed
CI / build (push) Failing after 3s
refactor(rmi-backend,audit): mount Wave 3 + archive 136 dead-code files (P2.3)
PHASE 2.3 (AUDIT-2026-Q3.md):

Task 1 — Wire-in Wave 3 (1 router mounted, 2 deferred):
  - app.routers.unified_scanner_router mounted at /api/v2/scanner/* (2 routes:
    POST /api/v2/scanner/token/scan, POST /api/v2/scanner/wallet/scan).
    Refactored prefix from /api/v2 -> /api/v2/scanner to avoid future conflicts
    with the v1 /api/v1/scanner/ stub.
  - app.routers.unified_wallet_scanner DEFERRED (no router APIRouter attribute;
    library module consumed by unified_scanner_router via get_wallet_scanner()).
  - app.routers.admin_extensions DEFERRED (DORMANT per audit; 25 routes at
    /api/v1/admin/* would shadow /api/v1/admin/alerts_webhook).

Task 2 — Archive 136 dead-code files to app/_archive/legacy_2026_07/:
  - 73 routers in app/routers/ (reach graph showed zero reach into mount.py).
  - 63 flat app/*.py (domain modules never imported by live code).
  - 1 file RESTORED post-archive: app/routers/x402_bridge_health.py (caught by
    tests/unit/test_bridge_health.py which directly imports it; reach graph
    considered tests/ only as transitive reach — to be patched in next cycle).

Forced-LIVE (NOT archived per user directive):
  - app/ai_pipeline_v3.py  (3 importers in audit window, importers themselves DEAD)
  - app/splade_bm25.py       (LIVE via app.rag_service)
  - app/wallet_manager_v2.py (LIVE via x402_enforcement, x402_tools, sweep_all, sweep_now)
  - app/crypto_embeddings.py (NOT in audit ARCHIVE list; heavy import graph)

Verification (forward-import closure from mount.py + main.py + factory.py + lifespan.py):
  - imports = 348 app.* modules
  - reached = 194 files reachable from roots
  - archive set = audit_dead (186) - reached - forced_live (4) - test_live (1) = 136
  - Net delta: 136 files moved, 44,932 LOC reduction, 293->295 active routes (+2 from Wave 3)

pyproject.toml updates:
  - setuptools.packages.find: added exclude for app._archive*
  - ruff.extend-exclude: added "app/_archive/"
  - mypy.exclude: added "app/_archive/"

Smoke test: pytest tests/ — 817 passed, 3 pre-existing failures unchanged
(0 new failures; 0 routes lost; all 4 forced-LIVE files still importable).

Restoration: git mv app/_archive/legacy_2026_07/<name>.py <original-path>
and add the import to app/mount.py ROUTER_MODULES.

Refs: AUDIT-2026-Q3.md /home/dev/pry/rmi-final-deadcode-2026-07-06.md
2026-07-06 20:52:31 +02:00

125 lines
4 KiB
Python

"""
Provider Health Dashboard + Credit Burn Tracking
=================================================
Live status of all 10 embedding providers + enterprise credit burn rate.
"""
import logging
import time
from app.rate_limiter import get_dispatcher
logger = logging.getLogger("health.dashboard")
async def provider_health() -> dict:
"""Live health status of all embedding providers."""
d = await get_dispatcher()
rl = await d.tracker.stats()
providers = []
for p in rl["providers"]:
# Determine health status
day_pct = p["day_pct"]
if not p["available"]:
status = "exhausted" if day_pct >= 100 else "rate_limited"
elif day_pct > 80:
status = "warning"
elif day_pct > 50:
status = "active_warm"
else:
status = "healthy"
providers.append(
{
"id": p["id"],
"name": p["name"],
"status": status,
"day_used": p["day_used"],
"day_limit": p["day_limit"],
"day_pct": p["day_pct"],
"minute_used": p["minute_used"],
"minute_limit": p["minute_limit"],
"total_calls": p["total"],
"free": p["free"],
}
)
healthy = sum(1 for p in providers if p["status"] == "healthy")
warning = sum(1 for p in providers if p["status"] == "warning")
degraded = sum(1 for p in providers if p["status"] in ("active_warm", "rate_limited"))
exhausted = sum(1 for p in providers if p["status"] == "exhausted")
return {
"summary": {
"total": len(providers),
"healthy": healthy,
"warning": warning,
"degraded": degraded,
"exhausted": exhausted,
},
"providers": providers,
"cache": d.cache.stats(),
"timestamp": time.time(),
}
async def credit_burn() -> dict:
"""Enterprise credit burn tracking - daily rate, projected exhaustion."""
d = await get_dispatcher()
rl = await d.tracker.stats()
# Calculate cost estimates
# Vertex AI: $0.000025 per 1K chars (~$0.000025 per embedding call)
# Gemini AI Studio: free up to 1,500/day per key
# OpenRouter: free with credits
# Mistral: free (1B tokens/month)
total_calls_today = 0
total_free_limit = 0
provider_usage = []
for p in rl["providers"]:
total_calls_today += p["day_used"]
total_free_limit += p["day_limit"] if p["free"] else 0
# Estimate cost (only Vertex AI burns credits from enterprise trial)
cost_est = 0
if p["id"] == "vertex_ai":
# $0.000025 per embedding call (approximate)
cost_est = round(p["day_used"] * 0.000025, 6)
if p["day_used"] > 0:
provider_usage.append(
{
"name": p["name"],
"calls_today": p["day_used"],
"estimated_cost_usd": cost_est,
"free": p["free"],
}
)
# $300 trial, assume 90 days started ~June 1, 2026
trial_start = time.mktime(time.strptime("2026-06-01", "%Y-%m-%d"))
trial_end = trial_start + 90 * 86400
days_remaining = max(0, (trial_end - time.time()) / 86400)
# Daily burn rate estimate
daily_cost = sum(p["estimated_cost_usd"] for p in provider_usage)
return {
"trial": {
"credits": 300,
"days_remaining": round(days_remaining, 0),
"exhaustion_date": "2026-08-30" if days_remaining > 0 else "now",
},
"today": {
"total_calls": total_calls_today,
"free_limit": total_free_limit,
"utilization_pct": round(total_calls_today / max(total_free_limit, 1) * 100, 2),
"estimated_cost_usd": daily_cost,
"daily_burn_rate": f"${daily_cost:.6f}",
},
"provider_usage": provider_usage,
"note": "Gemini, Mistral, NVIDIA, and OpenRouter are FREE. Only Vertex AI burns enterprise credits.",
}