Spaces:
Sleeping
Sleeping
Update observability_router.py
Browse files- observability_router.py +106 -73
observability_router.py
CHANGED
|
@@ -1,73 +1,106 @@
|
|
| 1 |
-
## OBSERVABILITY
|
| 2 |
-
from uuid import uuid4
|
| 3 |
-
import csv
|
| 4 |
-
from io import StringIO
|
| 5 |
-
from fastapi import APIRouter, HTTPException
|
| 6 |
-
from fastapi.responses import StreamingResponse
|
| 7 |
-
from pydantic import BaseModel
|
| 8 |
-
from typing import List, Dict, Optional
|
| 9 |
-
from observability import LLMObservabilityManager
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
router = APIRouter(
|
| 13 |
-
prefix="/observability",
|
| 14 |
-
tags=["observability"]
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
class ObservationResponse(BaseModel):
|
| 20 |
-
observations: List[Dict]
|
| 21 |
-
|
| 22 |
-
def create_csv_response(observations: List[Dict]) -> StreamingResponse:
|
| 23 |
-
def iter_csv(data):
|
| 24 |
-
output = StringIO()
|
| 25 |
-
writer = csv.DictWriter(output, fieldnames=data[0].keys() if data else [])
|
| 26 |
-
writer.writeheader()
|
| 27 |
-
for row in data:
|
| 28 |
-
writer.writerow(row)
|
| 29 |
-
output.seek(0)
|
| 30 |
-
yield output.read()
|
| 31 |
-
|
| 32 |
-
headers = {
|
| 33 |
-
'Content-Disposition': 'attachment; filename="observations.csv"'
|
| 34 |
-
}
|
| 35 |
-
return StreamingResponse(iter_csv(observations), media_type="text/csv", headers=headers)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
@router.get("/last-observations/{limit}")
|
| 39 |
-
async def get_last_observations(limit: int = 10, format: str = "json"):
|
| 40 |
-
observability_manager = LLMObservabilityManager()
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
# Get all observations, sorted by created_at in descending order
|
| 44 |
-
all_observations = observability_manager.get_observations()
|
| 45 |
-
all_observations.sort(key=lambda x: x['created_at'], reverse=True)
|
| 46 |
-
|
| 47 |
-
# Get the last conversation_id
|
| 48 |
-
if all_observations:
|
| 49 |
-
last_conversation_id = all_observations[0]['conversation_id']
|
| 50 |
-
|
| 51 |
-
# Filter observations for the last conversation
|
| 52 |
-
last_conversation_observations = [
|
| 53 |
-
obs for obs in all_observations
|
| 54 |
-
if obs['conversation_id'] == last_conversation_id
|
| 55 |
-
][:limit]
|
| 56 |
-
|
| 57 |
-
if format.lower() == "csv":
|
| 58 |
-
return create_csv_response(last_conversation_observations)
|
| 59 |
-
else:
|
| 60 |
-
return ObservationResponse(observations=last_conversation_observations)
|
| 61 |
-
else:
|
| 62 |
-
if format.lower() == "csv":
|
| 63 |
-
return create_csv_response([])
|
| 64 |
-
else:
|
| 65 |
-
return ObservationResponse(observations=[])
|
| 66 |
-
except Exception as e:
|
| 67 |
-
raise HTTPException(status_code=500, detail=f"Failed to retrieve observations: {str(e)}")
|
| 68 |
-
|
| 69 |
-
@router.get("/all-unique-observations")
|
| 70 |
-
async def get_all_unique_observations(limit: Optional[int] = None):
|
| 71 |
-
observability_manager = LLMObservabilityManager()
|
| 72 |
-
return ObservationResponse(observations=observability_manager.get_all_unique_conversation_observations(limit))
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## OBSERVABILITY
|
| 2 |
+
from uuid import uuid4
|
| 3 |
+
import csv
|
| 4 |
+
from io import StringIO
|
| 5 |
+
from fastapi import APIRouter, HTTPException
|
| 6 |
+
from fastapi.responses import StreamingResponse
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from typing import List, Dict, Optional
|
| 9 |
+
from observability import LLMObservabilityManager
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
router = APIRouter(
|
| 13 |
+
prefix="/observability",
|
| 14 |
+
tags=["observability"]
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class ObservationResponse(BaseModel):
|
| 20 |
+
observations: List[Dict]
|
| 21 |
+
|
| 22 |
+
def create_csv_response(observations: List[Dict]) -> StreamingResponse:
|
| 23 |
+
def iter_csv(data):
|
| 24 |
+
output = StringIO()
|
| 25 |
+
writer = csv.DictWriter(output, fieldnames=data[0].keys() if data else [])
|
| 26 |
+
writer.writeheader()
|
| 27 |
+
for row in data:
|
| 28 |
+
writer.writerow(row)
|
| 29 |
+
output.seek(0)
|
| 30 |
+
yield output.read()
|
| 31 |
+
|
| 32 |
+
headers = {
|
| 33 |
+
'Content-Disposition': 'attachment; filename="observations.csv"'
|
| 34 |
+
}
|
| 35 |
+
return StreamingResponse(iter_csv(observations), media_type="text/csv", headers=headers)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@router.get("/last-observations/{limit}")
|
| 39 |
+
async def get_last_observations(limit: int = 10, format: str = "json"):
|
| 40 |
+
observability_manager = LLMObservabilityManager()
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Get all observations, sorted by created_at in descending order
|
| 44 |
+
all_observations = observability_manager.get_observations()
|
| 45 |
+
all_observations.sort(key=lambda x: x['created_at'], reverse=True)
|
| 46 |
+
|
| 47 |
+
# Get the last conversation_id
|
| 48 |
+
if all_observations:
|
| 49 |
+
last_conversation_id = all_observations[0]['conversation_id']
|
| 50 |
+
|
| 51 |
+
# Filter observations for the last conversation
|
| 52 |
+
last_conversation_observations = [
|
| 53 |
+
obs for obs in all_observations
|
| 54 |
+
if obs['conversation_id'] == last_conversation_id
|
| 55 |
+
][:limit]
|
| 56 |
+
|
| 57 |
+
if format.lower() == "csv":
|
| 58 |
+
return create_csv_response(last_conversation_observations)
|
| 59 |
+
else:
|
| 60 |
+
return ObservationResponse(observations=last_conversation_observations)
|
| 61 |
+
else:
|
| 62 |
+
if format.lower() == "csv":
|
| 63 |
+
return create_csv_response([])
|
| 64 |
+
else:
|
| 65 |
+
return ObservationResponse(observations=[])
|
| 66 |
+
except Exception as e:
|
| 67 |
+
raise HTTPException(status_code=500, detail=f"Failed to retrieve observations: {str(e)}")
|
| 68 |
+
|
| 69 |
+
@router.get("/all-unique-observations")
|
| 70 |
+
async def get_all_unique_observations(limit: Optional[int] = None):
|
| 71 |
+
observability_manager = LLMObservabilityManager()
|
| 72 |
+
return ObservationResponse(observations=observability_manager.get_all_unique_conversation_observations(limit))
|
| 73 |
+
|
| 74 |
+
class StatisticsResponse(BaseModel):
|
| 75 |
+
statistics: Dict[str, Any]
|
| 76 |
+
|
| 77 |
+
@router.get("/export-statistics")
|
| 78 |
+
async def get_dashboard_data(
|
| 79 |
+
days: Optional[int] = 30,
|
| 80 |
+
time_series_interval: str = 'day',
|
| 81 |
+
):
|
| 82 |
+
"""
|
| 83 |
+
Get comprehensive Analytics data including statistics.
|
| 84 |
+
|
| 85 |
+
Args:
|
| 86 |
+
days: Number of days to look back for statistics
|
| 87 |
+
time_series_interval: Interval for time series data ('hour', 'day', 'week', 'month')
|
| 88 |
+
"""
|
| 89 |
+
try:
|
| 90 |
+
observability_manager = LLMObservabilityManager()
|
| 91 |
+
|
| 92 |
+
# Get dashboard statistics
|
| 93 |
+
statistics = observability_manager.get_dashboard_statistics(
|
| 94 |
+
days=days,
|
| 95 |
+
time_series_interval=time_series_interval
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
return StatisticsResponse(
|
| 99 |
+
statistics=statistics,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
raise HTTPException(
|
| 104 |
+
status_code=500,
|
| 105 |
+
detail=f"Failed to retrieve dashboard data: {str(e)}"
|
| 106 |
+
)
|