MCPcopy Create free account
hub / github.com/CommonstackAI/UncommonRoute / handle_stats

Function handle_stats

uncommon_route/proxy.py:3389–3470  ·  view source on GitHub ↗

GET /v1/stats — route analytics. POST /v1/stats — reset.

(request: Request)

Source from the content-addressed store, hash-verified

3387 return JSONResponse({"error": "Invalid action"}, status_code=400)
3388
3389 async def handle_stats(request: Request) -> JSONResponse:
3390 """GET /v1/stats — route analytics. POST /v1/stats — reset."""
3391 if request.method == "POST":
3392 body = await request.json()
3393 if body.get("action") == "reset":
3394 traces_cleared = _traces.count
3395 _stats.reset()
3396 _traces.reset()
3397 _route_confidence.reset()
3398 cleared_feedback = _feedback.clear_pending()
3399 return JSONResponse({
3400 "ok": True,
3401 "reset": True,
3402 "traces_cleared": traces_cleared,
3403 "feedback_cleared": cleared_feedback,
3404 "route_confidence_calibration_reset": True,
3405 })
3406 return JSONResponse({"error": "Invalid action"}, status_code=400)
3407 s = _stats.summary()
3408 return JSONResponse({
3409 "total_requests": s.total_requests,
3410 "time_range_s": round(s.time_range_s, 1),
3411 "avg_confidence": round(s.avg_confidence, 3),
3412 "avg_savings": round(s.avg_savings, 3),
3413 "avg_latency_ms": round(s.avg_latency_us / 1000.0, 3),
3414 "avg_input_reduction_ratio": round(s.avg_input_reduction_ratio, 3),
3415 "avg_cache_hit_ratio": round(s.avg_cache_hit_ratio, 3),
3416 "total_estimated_cost": round(s.total_estimated_cost, 6),
3417 "total_baseline_cost": round(s.total_baseline_cost, 6),
3418 "total_actual_cost": round(s.total_actual_cost, 6),
3419 "total_savings_absolute": round(s.total_savings_absolute, 6),
3420 "total_savings_ratio": round(s.total_savings_ratio, 6),
3421 "total_cache_savings": round(s.total_cache_savings, 6),
3422 "total_compaction_savings": round(s.total_compaction_savings, 6),
3423 "total_usage_input_tokens": s.total_usage_input_tokens,
3424 "total_usage_output_tokens": s.total_usage_output_tokens,
3425 "total_cache_read_input_tokens": s.total_cache_read_input_tokens,
3426 "total_cache_write_input_tokens": s.total_cache_write_input_tokens,
3427 "total_cache_breakpoints": s.total_cache_breakpoints,
3428 "total_input_tokens_before": s.total_input_tokens_before,
3429 "total_input_tokens_after": s.total_input_tokens_after,
3430 "total_artifacts_created": s.total_artifacts_created,
3431 "total_compacted_messages": s.total_compacted_messages,
3432 "total_semantic_summaries": s.total_semantic_summaries,
3433 "total_semantic_calls": s.total_semantic_calls,
3434 "total_semantic_failures": s.total_semantic_failures,
3435 "total_semantic_quality_fallbacks": s.total_semantic_quality_fallbacks,
3436 "total_checkpoints_created": s.total_checkpoints_created,
3437 "total_rehydrated_artifacts": s.total_rehydrated_artifacts,
3438 "route_confidence_calibration": _route_confidence.status(),
3439 "by_mode": s.by_mode,
3440 "by_decision_tier": s.by_decision_tier,
3441 "by_served_quality": s.by_served_quality,
3442 "by_capability_lane": s.by_capability_lane,
3443 "by_tier": {
3444 tier: {
3445 "count": ts.count,
3446 "avg_confidence": round(ts.avg_confidence, 3),

Callers

nothing calls this directly

Calls 6

_selector_stateFunction · 0.85
clear_pendingMethod · 0.80
getMethod · 0.45
resetMethod · 0.45
summaryMethod · 0.45
statusMethod · 0.45

Tested by

no test coverage detected