nothingworry commited on
Commit
59f118a
·
1 Parent(s): e87ab5b

fix(analytics): make Supabase optional and update websockets for MCP server

Browse files
backend/api/routes/analytics.py CHANGED
@@ -1,14 +1,35 @@
1
  from fastapi import APIRouter, Header, HTTPException, Query
2
  from typing import Optional
3
  from datetime import datetime, timedelta
 
 
4
 
5
  from ..storage.analytics_store import AnalyticsStore
6
  from ..utils.access_control import require_api_permission
7
 
8
  router = APIRouter()
9
-
10
- # Initialize analytics store
11
- analytics_store = AnalyticsStore()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
 
14
  @router.get("/overview")
@@ -22,6 +43,12 @@ async def analytics_overview(
22
  Includes total queries, tool usage, red-flag count, and active users.
23
  """
24
 
 
 
 
 
 
 
25
  if not x_tenant_id:
26
  raise HTTPException(status_code=400, detail="Missing tenant ID")
27
  require_api_permission(x_user_role, "view_analytics")
@@ -56,6 +83,12 @@ async def analytics_tool_usage(
56
  Includes counts, latency, tokens, and success/error rates.
57
  """
58
 
 
 
 
 
 
 
59
  if not x_tenant_id:
60
  raise HTTPException(status_code=400, detail="Missing tenant ID")
61
  require_api_permission(x_user_role, "view_analytics")
@@ -82,6 +115,12 @@ async def analytics_redflags(
82
  Includes rule details, severity, confidence, and timestamps.
83
  """
84
 
 
 
 
 
 
 
85
  if not x_tenant_id:
86
  raise HTTPException(status_code=400, detail="Missing tenant ID")
87
  require_api_permission(x_user_role, "view_analytics")
@@ -112,6 +151,12 @@ async def analytics_activity(
112
  Includes total queries, active users, last query timestamp, and individual activity records for heatmap visualization.
113
  """
114
 
 
 
 
 
 
 
115
  if not x_tenant_id:
116
  raise HTTPException(status_code=400, detail="Missing tenant ID")
117
  require_api_permission(x_user_role, "view_analytics")
@@ -141,6 +186,12 @@ async def analytics_rag_quality(
141
  Includes average hits, scores, and latency.
142
  """
143
 
 
 
 
 
 
 
144
  if not x_tenant_id:
145
  raise HTTPException(status_code=400, detail="Missing tenant ID")
146
  require_api_permission(x_user_role, "view_analytics")
 
1
  from fastapi import APIRouter, Header, HTTPException, Query
2
  from typing import Optional
3
  from datetime import datetime, timedelta
4
+ import logging
5
+ import os
6
 
7
  from ..storage.analytics_store import AnalyticsStore
8
  from ..utils.access_control import require_api_permission
9
 
10
  router = APIRouter()
11
+ logger = logging.getLogger(__name__)
12
+
13
+ # Initialize analytics store, but don't crash the app if Supabase is not available.
14
+ # In environments like Hugging Face Spaces where Supabase isn't configured,
15
+ # we disable analytics gracefully instead of raising at import time.
16
+ try:
17
+ # Only attempt to initialize Supabase analytics when credentials are present
18
+ if os.getenv("SUPABASE_URL") and os.getenv("SUPABASE_SERVICE_KEY"):
19
+ analytics_store: Optional[AnalyticsStore] = AnalyticsStore()
20
+ else:
21
+ analytics_store = None
22
+ logger.warning(
23
+ "AnalyticsStore: Supabase credentials not configured. "
24
+ "Analytics endpoints will return 503."
25
+ )
26
+ except RuntimeError as exc:
27
+ analytics_store = None
28
+ logger.warning(
29
+ "AnalyticsStore initialization failed (%s). "
30
+ "Analytics endpoints will return 503.",
31
+ exc,
32
+ )
33
 
34
 
35
  @router.get("/overview")
 
43
  Includes total queries, tool usage, red-flag count, and active users.
44
  """
45
 
46
+ if analytics_store is None:
47
+ raise HTTPException(
48
+ status_code=503,
49
+ detail="Analytics is disabled because Supabase is not configured on this deployment.",
50
+ )
51
+
52
  if not x_tenant_id:
53
  raise HTTPException(status_code=400, detail="Missing tenant ID")
54
  require_api_permission(x_user_role, "view_analytics")
 
83
  Includes counts, latency, tokens, and success/error rates.
84
  """
85
 
86
+ if analytics_store is None:
87
+ raise HTTPException(
88
+ status_code=503,
89
+ detail="Analytics is disabled because Supabase is not configured on this deployment.",
90
+ )
91
+
92
  if not x_tenant_id:
93
  raise HTTPException(status_code=400, detail="Missing tenant ID")
94
  require_api_permission(x_user_role, "view_analytics")
 
115
  Includes rule details, severity, confidence, and timestamps.
116
  """
117
 
118
+ if analytics_store is None:
119
+ raise HTTPException(
120
+ status_code=503,
121
+ detail="Analytics is disabled because Supabase is not configured on this deployment.",
122
+ )
123
+
124
  if not x_tenant_id:
125
  raise HTTPException(status_code=400, detail="Missing tenant ID")
126
  require_api_permission(x_user_role, "view_analytics")
 
151
  Includes total queries, active users, last query timestamp, and individual activity records for heatmap visualization.
152
  """
153
 
154
+ if analytics_store is None:
155
+ raise HTTPException(
156
+ status_code=503,
157
+ detail="Analytics is disabled because Supabase is not configured on this deployment.",
158
+ )
159
+
160
  if not x_tenant_id:
161
  raise HTTPException(status_code=400, detail="Missing tenant ID")
162
  require_api_permission(x_user_role, "view_analytics")
 
186
  Includes average hits, scores, and latency.
187
  """
188
 
189
+ if analytics_store is None:
190
+ raise HTTPException(
191
+ status_code=503,
192
+ detail="Analytics is disabled because Supabase is not configured on this deployment.",
193
+ )
194
+
195
  if not x_tenant_id:
196
  raise HTTPException(status_code=400, detail="Missing tenant ID")
197
  require_api_permission(x_user_role, "view_analytics")
requirements.txt CHANGED
@@ -12,7 +12,7 @@ duckduckgo-search
12
  PyPDF2
13
  python-docx
14
  python-multipart
15
- websockets>=11.0
16
  gradio==4.20.0
17
  requests>=2.31.0
18
  plotly>=5.0.0
 
12
  PyPDF2
13
  python-docx
14
  python-multipart
15
+ websockets>=13.0
16
  gradio==4.20.0
17
  requests>=2.31.0
18
  plotly>=5.0.0