Yash030 Claude Opus 4.7 commited on
Commit
f3220aa
·
1 Parent(s): 28e4b90

Add admin dashboard for session monitoring

Browse files

- Create api/admin.py with /admin and /api/admin/sessions endpoints
- Add Bootstrap-styled templates/admin.html dashboard
- Auto-refresh every 10 seconds for real-time monitoring
- Shows session info, provider load, and summary stats

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

Files changed (3) hide show
  1. api/admin.py +118 -0
  2. api/app.py +2 -0
  3. templates/admin.html +138 -0
api/admin.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Admin dashboard for session monitoring."""
2
+
3
+ import time
4
+ from typing import Any
5
+
6
+ from fastapi import APIRouter, Depends, Request
7
+ from starlette.responses import HTMLResponse
8
+ from starlette.templating import Jinja2Templates
9
+
10
+ from core.session_tracker import SessionTracker
11
+
12
+ from .dependencies import require_api_key
13
+
14
+ router = APIRouter()
15
+
16
+ templates = Jinja2Templates(directory="templates")
17
+
18
+
19
+ def _get_blocked_providers() -> set[str]:
20
+ """Get currently blocked provider IDs. Placeholder - returns empty set."""
21
+ return set()
22
+
23
+
24
+ def _format_time(seconds: float) -> str:
25
+ """Format seconds as human-readable string."""
26
+ if seconds < 60:
27
+ return f"{int(seconds)}s ago"
28
+ elif seconds < 3600:
29
+ return f"{int(seconds // 60)}m ago"
30
+ else:
31
+ return f"{int(seconds // 3600)}h ago"
32
+
33
+
34
+ def _get_admin_data() -> dict[str, Any]:
35
+ """Gather all data for the admin dashboard."""
36
+ tracker = SessionTracker.get_instance()
37
+ blocked = _get_blocked_providers()
38
+
39
+ session_loads = tracker.get_all_session_loads()
40
+ provider_loads = tracker.get_all_provider_loads(blocked)
41
+
42
+ now = time.monotonic()
43
+
44
+ total_requests = 0
45
+ all_providers = set()
46
+ sessions_list = []
47
+
48
+ for session_id, load in session_loads.items():
49
+ total_requests += load.total_requests
50
+ all_providers.update(load.providers.keys())
51
+
52
+ state = tracker._sessions.get(session_id)
53
+ last_activity_seconds = 0.0
54
+ if state:
55
+ last_activity_seconds = now - state.last_request_time
56
+
57
+ req_per_min = 0.0
58
+ if state and state.requests_in_window > 0:
59
+ req_per_min = (state.requests_in_window / tracker._window_seconds) * 60
60
+
61
+ sessions_list.append(
62
+ {
63
+ "session_id": session_id,
64
+ "total_requests": load.total_requests,
65
+ "providers": list(load.providers.keys()),
66
+ "last_activity_seconds": last_activity_seconds,
67
+ "last_activity_display": _format_time(last_activity_seconds),
68
+ "requests_per_minute": int(req_per_min),
69
+ }
70
+ )
71
+
72
+ providers_list = []
73
+ for provider_id, load in provider_loads.items():
74
+ remaining = 0
75
+ if provider_id in blocked:
76
+ remaining = 30 # placeholder, real impl would check rate limiter
77
+ status = "Rate Limited" if not load.is_healthy else "Healthy"
78
+ if not load.is_healthy and remaining > 0:
79
+ status += f" ({remaining}s remaining)"
80
+
81
+ providers_list.append(
82
+ {
83
+ "provider_id": provider_id,
84
+ "active_requests": load.active_requests,
85
+ "sessions_count": load.session_count,
86
+ "requests_per_minute": int(load.requests_per_minute),
87
+ "is_healthy": load.is_healthy,
88
+ "status": status,
89
+ }
90
+ )
91
+
92
+ return {
93
+ "summary": {
94
+ "active_sessions": len(session_loads),
95
+ "total_requests": total_requests,
96
+ "providers": len(all_providers),
97
+ },
98
+ "sessions": sessions_list,
99
+ "providers": providers_list,
100
+ }
101
+
102
+
103
+ @router.get("/admin", response_class=HTMLResponse)
104
+ async def admin_dashboard(
105
+ request: Request,
106
+ _auth=Depends(require_api_key),
107
+ ):
108
+ """Admin dashboard HTML page."""
109
+ data = _get_admin_data()
110
+ return templates.TemplateResponse("admin.html", {"request": request, **data})
111
+
112
+
113
+ @router.get("/api/admin/sessions")
114
+ async def admin_sessions_json(
115
+ _auth=Depends(require_api_key),
116
+ ):
117
+ """JSON API for session data (for external tools)."""
118
+ return _get_admin_data()
api/app.py CHANGED
@@ -15,6 +15,7 @@ from config.logging_config import configure_logging
15
  from config.settings import get_settings
16
  from providers.exceptions import ProviderError
17
 
 
18
  from .routes import router
19
  from .runtime import AppRuntime, startup_failure_message
20
  from .validation_log import summarize_request_validation_body
@@ -96,6 +97,7 @@ def create_app(*, lifespan_enabled: bool = True) -> FastAPI:
96
 
97
  # Register routes
98
  app.include_router(router)
 
99
 
100
  # Exception handlers
101
  @app.exception_handler(RequestValidationError)
 
15
  from config.settings import get_settings
16
  from providers.exceptions import ProviderError
17
 
18
+ from .admin import router as admin_router
19
  from .routes import router
20
  from .runtime import AppRuntime, startup_failure_message
21
  from .validation_log import summarize_request_validation_body
 
97
 
98
  # Register routes
99
  app.include_router(router)
100
+ app.include_router(admin_router)
101
 
102
  # Exception handlers
103
  @app.exception_handler(RequestValidationError)
templates/admin.html ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <meta http-equiv="refresh" content="10">
7
+ <title>Claude Code Proxy - Admin Dashboard</title>
8
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
9
+ <style>
10
+ body { background-color: #f8f9fa; }
11
+ .dashboard-header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 2rem 0; margin-bottom: 2rem; }
12
+ .stat-card { border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
13
+ .table-container { background: white; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.05); padding: 1.5rem; margin-bottom: 2rem; }
14
+ .section-title { color: #495057; font-weight: 600; margin-bottom: 1rem; border-bottom: 2px solid #dee2e6; padding-bottom: 0.5rem; }
15
+ .status-healthy { color: #198754; font-weight: 500; }
16
+ .status-limited { color: #dc3545; font-weight: 500; }
17
+ .session-id { font-family: monospace; font-size: 0.9em; }
18
+ </style>
19
+ </head>
20
+ <body>
21
+ <div class="dashboard-header">
22
+ <div class="container">
23
+ <h1 class="mb-0">Claude Code Proxy - Admin Dashboard</h1>
24
+ <p class="mb-0 mt-2 opacity-75">Session Monitoring & Provider Load</p>
25
+ </div>
26
+ </div>
27
+
28
+ <div class="container">
29
+ <!-- Summary Stats -->
30
+ <div class="row mb-4">
31
+ <div class="col-md-4">
32
+ <div class="card stat-card bg-primary text-white">
33
+ <div class="card-body">
34
+ <h5 class="card-title">Active Sessions</h5>
35
+ <h2 class="mb-0">{{ summary.active_sessions }}</h2>
36
+ </div>
37
+ </div>
38
+ </div>
39
+ <div class="col-md-4">
40
+ <div class="card stat-card bg-success text-white">
41
+ <div class="card-body">
42
+ <h5 class="card-title">Total Requests</h5>
43
+ <h2 class="mb-0">{{ summary.total_requests }}</h2>
44
+ </div>
45
+ </div>
46
+ </div>
47
+ <div class="col-md-4">
48
+ <div class="card stat-card bg-info text-white">
49
+ <div class="card-body">
50
+ <h5 class="card-title">Providers</h5>
51
+ <h2 class="mb-0">{{ summary.providers }}</h2>
52
+ </div>
53
+ </div>
54
+ </div>
55
+ </div>
56
+
57
+ <!-- Sessions Table -->
58
+ <div class="table-container">
59
+ <h3 class="section-title">Sessions</h3>
60
+ {% if sessions %}
61
+ <div class="table-responsive">
62
+ <table class="table table-hover">
63
+ <thead class="table-light">
64
+ <tr>
65
+ <th>Session ID</th>
66
+ <th>Requests</th>
67
+ <th>Providers</th>
68
+ <th>Last Active</th>
69
+ <th>Req/Min</th>
70
+ </tr>
71
+ </thead>
72
+ <tbody>
73
+ {% for session in sessions %}
74
+ <tr>
75
+ <td class="session-id" title="{{ session.session_id }}">{{ session.session_id[:12] }}...</td>
76
+ <td>{{ session.total_requests }}</td>
77
+ <td>
78
+ {% for provider in session.providers %}
79
+ <span class="badge bg-secondary">{{ provider }}</span>
80
+ {% endfor %}
81
+ </td>
82
+ <td>{{ session.last_activity_display }}</td>
83
+ <td>{{ session.requests_per_minute }}</td>
84
+ </tr>
85
+ {% endfor %}
86
+ </tbody>
87
+ </table>
88
+ </div>
89
+ {% else %}
90
+ <p class="text-muted">No active sessions</p>
91
+ {% endif %}
92
+ </div>
93
+
94
+ <!-- Provider Load Table -->
95
+ <div class="table-container">
96
+ <h3 class="section-title">Provider Load</h3>
97
+ {% if providers %}
98
+ <div class="table-responsive">
99
+ <table class="table table-hover">
100
+ <thead class="table-light">
101
+ <tr>
102
+ <th>Provider</th>
103
+ <th>Active</th>
104
+ <th>Sessions</th>
105
+ <th>Req/Min</th>
106
+ <th>Status</th>
107
+ </tr>
108
+ </thead>
109
+ <tbody>
110
+ {% for provider in providers %}
111
+ <tr>
112
+ <td class="session-id">{{ provider.provider_id }}</td>
113
+ <td>{{ provider.active_requests }}</td>
114
+ <td>{{ provider.sessions_count }}</td>
115
+ <td>{{ provider.requests_per_minute }}</td>
116
+ <td>
117
+ {% if provider.is_healthy %}
118
+ <span class="status-healthy">✓ {{ provider.status }}</span>
119
+ {% else %}
120
+ <span class="status-limited">⚠ {{ provider.status }}</span>
121
+ {% endif %}
122
+ </td>
123
+ </tr>
124
+ {% endfor %}
125
+ </tbody>
126
+ </table>
127
+ </div>
128
+ {% else %}
129
+ <p class="text-muted">No provider data</p>
130
+ {% endif %}
131
+ </div>
132
+
133
+ <footer class="text-center text-muted py-3">
134
+ <small>Auto-refreshes every 10 seconds • <a href="/api/admin/sessions">JSON API</a></small>
135
+ </footer>
136
+ </div>
137
+ </body>
138
+ </html>