Spaces:
Paused

dushyantsingh0 commited on
Commit
7009ad4
·
1 Parent(s): 1d07780

fix: enforce fault-tolerant caching and resolve role-based 500 errors

Browse files
Files changed (2) hide show
  1. api/v1/reports.py +14 -6
  2. api/v1/workspace.py +1 -1
api/v1/reports.py CHANGED
@@ -218,17 +218,25 @@ async def get_ai_service_insights(
218
  redis = get_redis_client()
219
  cache_key = f"insights:narrative:{target_ws}"
220
 
221
- cached_val = await redis.get(cache_key)
222
- if cached_val:
223
- logger.info(f"⚡ Research Cache HIT [Narrative Insights]: {target_ws}")
224
- return json.loads(cached_val)
 
 
 
 
225
 
226
  # 3. Generate Narrative
227
  insight_text = await generate_service_insights(metrics, ticket_data)
228
  res_data = {"insights": insight_text, "generated_at": now.isoformat()}
229
 
230
- # Store with 15 minute TTL
231
- await redis.set(cache_key, json.dumps(res_data), ex=900)
 
 
 
 
232
  return res_data
233
 
234
  @router.get("/daily-stats/", dependencies=[Depends(require_roles(ALL_SC_ROLES + [UserRole.CUSTOMER_ADMIN]))])
 
218
  redis = get_redis_client()
219
  cache_key = f"insights:narrative:{target_ws}"
220
 
221
+ try:
222
+ cached_val = await redis.get(cache_key)
223
+ if cached_val:
224
+ logger.info(f"⚡ Research Cache HIT [Narrative Insights]: {target_ws}")
225
+ return json.loads(cached_val)
226
+ except Exception as e:
227
+ logger.error(f"Redis cache lookup failed for insights: {str(e)}")
228
+ # Continue to generate fresh insights if cache fails
229
 
230
  # 3. Generate Narrative
231
  insight_text = await generate_service_insights(metrics, ticket_data)
232
  res_data = {"insights": insight_text, "generated_at": now.isoformat()}
233
 
234
+ try:
235
+ # Store with 15 minute TTL
236
+ await redis.set(cache_key, json.dumps(res_data), ex=900)
237
+ except Exception as e:
238
+ logger.error(f"Redis cache storing failed for insights: {str(e)}")
239
+
240
  return res_data
241
 
242
  @router.get("/daily-stats/", dependencies=[Depends(require_roles(ALL_SC_ROLES + [UserRole.CUSTOMER_ADMIN]))])
api/v1/workspace.py CHANGED
@@ -50,7 +50,7 @@ async def list_available_workspaces(
50
  return [
51
  {
52
  **WorkspaceResponse.model_validate(row[0]).model_dump(),
53
- "role": row[1].value,
54
  "is_primary": row[2]
55
  } for row in rows
56
  ]
 
50
  return [
51
  {
52
  **WorkspaceResponse.model_validate(row[0]).model_dump(),
53
+ "role": row[1].value if hasattr(row[1], 'value') else row[1],
54
  "is_primary": row[2]
55
  } for row in rows
56
  ]