Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, Header, HTTPException | |
| router = APIRouter() | |
| # Mock in-memory analytics (replace with Supabase later) | |
| ANALYTICS_DATA = { | |
| "tool_usage": { | |
| "rag": 12, | |
| "web": 8, | |
| "admin": 3 | |
| }, | |
| "redflags": [ | |
| { | |
| "tenant": "tenant123", | |
| "match": "salary", | |
| "message": "get salary data now", | |
| "timestamp": "2025-01-14T10:22:00Z" | |
| } | |
| ], | |
| "activity": { | |
| "total_queries": 23, | |
| "active_users": 4, | |
| "last_query": "2025-01-14T10:24:31Z" | |
| } | |
| } | |
| async def analytics_overview( | |
| x_tenant_id: str = Header(None) | |
| ): | |
| """ | |
| Returns an overview of analytics for the dashboard. | |
| """ | |
| if not x_tenant_id: | |
| raise HTTPException(status_code=400, detail="Missing tenant ID") | |
| return { | |
| "tenant_id": x_tenant_id, | |
| "overview": { | |
| "total_queries": ANALYTICS_DATA["activity"]["total_queries"], | |
| "tool_usage": ANALYTICS_DATA["tool_usage"], | |
| "redflag_count": len(ANALYTICS_DATA["redflags"]), | |
| "active_users": ANALYTICS_DATA["activity"]["active_users"] | |
| } | |
| } | |
| async def analytics_tool_usage( | |
| x_tenant_id: str = Header(None) | |
| ): | |
| """ | |
| Returns how often each tool (RAG, Web, Admin) was used. | |
| """ | |
| if not x_tenant_id: | |
| raise HTTPException(status_code=400, detail="Missing tenant ID") | |
| return { | |
| "tenant_id": x_tenant_id, | |
| "tool_usage": ANALYTICS_DATA["tool_usage"] | |
| } | |
| async def analytics_redflags( | |
| x_tenant_id: str = Header(None) | |
| ): | |
| """ | |
| Returns red-flag violations for this tenant. | |
| """ | |
| if not x_tenant_id: | |
| raise HTTPException(status_code=400, detail="Missing tenant ID") | |
| redflags = [ | |
| r for r in ANALYTICS_DATA["redflags"] | |
| if r["tenant"] == x_tenant_id | |
| ] | |
| return { | |
| "tenant_id": x_tenant_id, | |
| "redflags": redflags | |
| } | |
| async def analytics_activity( | |
| x_tenant_id: str = Header(None) | |
| ): | |
| """ | |
| Returns general tenant activity statistics. | |
| """ | |
| if not x_tenant_id: | |
| raise HTTPException(status_code=400, detail="Missing tenant ID") | |
| return { | |
| "tenant_id": x_tenant_id, | |
| "activity": ANALYTICS_DATA["activity"] | |
| } | |