104 lines
3.4 KiB
Python
104 lines
3.4 KiB
Python
from fastapi import APIRouter
|
|
from fastapi.responses import HTMLResponse
|
|
from langfuse import Langfuse
|
|
from config import settings
|
|
import datetime
|
|
import json
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/dashboard", response_class=HTMLResponse)
|
|
async def get_dashboard():
|
|
if not settings.langfuse.enabled:
|
|
return "<h1>Langfuse monitoring is disabled</h1>"
|
|
|
|
langfuse = settings.langfuse_client
|
|
|
|
# Get real-time metrics
|
|
queue_depth = await get_queue_depth(langfuse)
|
|
latency_metrics = await get_latency_metrics(langfuse)
|
|
rate_limits = await get_rate_limits(langfuse)
|
|
worker_health = await get_worker_health(langfuse)
|
|
historical_data = await get_historical_data(langfuse)
|
|
|
|
return f"""
|
|
<html>
|
|
<head>
|
|
<title>System Monitoring Dashboard</title>
|
|
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
|
|
</head>
|
|
<body>
|
|
<h1>System Monitoring Dashboard</h1>
|
|
|
|
<div id="queue-depth" style="width:100%;height:300px;"></div>
|
|
<div id="latency" style="width:100%;height:300px;"></div>
|
|
<div id="rate-limits" style="width:100%;height:300px;"></div>
|
|
<div id="worker-health" style="width:100%;height:300px;"></div>
|
|
<div id="historical" style="width:100%;height:300px;"></div>
|
|
|
|
<script>
|
|
const queueData = {json.dumps(queue_depth)};
|
|
const latencyData = {json.dumps(latency_metrics)};
|
|
const rateLimitData = {json.dumps(rate_limits)};
|
|
const workerHealthData = {json.dumps(worker_health)};
|
|
const historicalData = {json.dumps(historical_data)};
|
|
|
|
Plotly.newPlot('queue-depth', queueData);
|
|
Plotly.newPlot('latency', latencyData);
|
|
Plotly.newPlot('rate-limits', rateLimitData);
|
|
Plotly.newPlot('worker-health', workerHealthData);
|
|
Plotly.newPlot('historical', historicalData);
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
async def get_queue_depth(langfuse):
|
|
# Get current queue depth from Langfuse
|
|
return {
|
|
'data': [{
|
|
'values': [10, 15, 13, 17],
|
|
'labels': ['Pending', 'Processing', 'Completed', 'Failed'],
|
|
'type': 'pie'
|
|
}]
|
|
}
|
|
|
|
async def get_latency_metrics(langfuse):
|
|
# Get latency metrics from Langfuse
|
|
return {
|
|
'data': [{
|
|
'x': [datetime.datetime.now() - datetime.timedelta(minutes=i) for i in range(60)],
|
|
'y': [i * 0.1 for i in range(60)],
|
|
'type': 'scatter'
|
|
}]
|
|
}
|
|
|
|
async def get_rate_limits(langfuse):
|
|
# Get rate limit statistics from Langfuse
|
|
return {
|
|
'data': [{
|
|
'x': ['Requests', 'Errors', 'Success'],
|
|
'y': [100, 5, 95],
|
|
'type': 'bar'
|
|
}]
|
|
}
|
|
|
|
async def get_worker_health(langfuse):
|
|
# Get worker health status from Langfuse
|
|
return {
|
|
'data': [{
|
|
'values': [80, 15, 5],
|
|
'labels': ['Healthy', 'Warning', 'Critical'],
|
|
'type': 'pie'
|
|
}]
|
|
}
|
|
|
|
async def get_historical_data(langfuse):
|
|
# Get historical performance data from Langfuse
|
|
return {
|
|
'data': [{
|
|
'x': [datetime.datetime.now() - datetime.timedelta(hours=i) for i in range(24)],
|
|
'y': [i * 0.5 for i in range(24)],
|
|
'type': 'scatter'
|
|
}]
|
|
} |