File size: 12,137 Bytes
141cc25 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | # Analytics Routes Documentation
These routes provide comprehensive analytics functionality for the Auto-Analyst backend, including dashboard summaries, user analytics, model performance metrics, cost analysis, and system monitoring.
## Authentication
All analytics endpoints require admin authentication via API key:
```python
ADMIN_API_KEY = os.getenv("ADMIN_API_KEY", "default-admin-key-change-me")
```
The API key can be provided via:
- **Header:** `X-Admin-API-Key`
- **Query parameter:** `admin_api_key`
---
## Dashboard Endpoints
### **GET /analytics/dashboard**
Returns comprehensive dashboard data combining usage statistics, model performance, and user activity.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"total_tokens": 123456,
"total_cost": 25.50,
"total_requests": 1000,
"total_users": 50,
"daily_usage": [
{
"date": "2023-05-01",
"tokens": 5000,
"cost": 1.25,
"requests": 100
}
],
"model_usage": [
{
"model_name": "claude-3-sonnet-20241022",
"tokens": 10000,
"cost": 10.00,
"requests": 200
}
],
"top_users": [
{
"user_id": "123",
"tokens": 5000,
"cost": 5.00,
"requests": 50
}
],
"start_date": "2023-04-01",
"end_date": "2023-05-01"
}
```
### **WebSocket /analytics/dashboard/realtime**
WebSocket endpoint for real-time dashboard updates. Accepts connections and maintains them for broadcasting live data updates.
---
## User Analytics Endpoints
### **GET /analytics/users**
Returns user list with usage statistics from the past 7 days.
**Query Parameters:**
- `limit` (optional): Maximum users to return (default: `100`)
- `offset` (optional): Pagination offset (default: `0`)
**Response:**
```json
{
"users": [
{
"user_id": "123",
"tokens": 5000,
"cost": 5.00,
"requests": 50,
"first_seen": "2023-04-01T12:00:00Z",
"last_seen": "2023-05-01T12:00:00Z"
}
],
"total": 200,
"limit": 100,
"offset": 0
}
```
### **GET /analytics/users/activity**
Returns daily user activity metrics with new user tracking.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"user_activity": [
{
"date": "2023-05-01",
"activeUsers": 20,
"newUsers": 5,
"sessions": 30
}
]
}
```
### **GET /analytics/users/sessions/stats**
Returns session statistics including total users, active users today, average queries per session, and average session time.
**Response:**
```json
{
"totalUsers": 500,
"activeToday": 25,
"avgQueriesPerSession": 3.2,
"avgSessionTime": 300
}
```
### **WebSocket /analytics/realtime**
WebSocket endpoint for real-time user analytics updates.
---
## Model Analytics Endpoints
### **GET /analytics/usage/models**
Returns model usage breakdown with performance metrics.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"model_usage": [
{
"model_name": "claude-3-sonnet-20241022",
"tokens": 10000,
"cost": 10.00,
"requests": 200,
"avg_response_time": 1.5
}
]
}
```
### **GET /analytics/models/history**
Returns daily model usage history with trend data.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"model_history": [
{
"date": "2023-05-01",
"models": [
{
"name": "claude-3-sonnet-20241022",
"tokens": 5000,
"requests": 100
}
]
}
]
}
```
### **GET /analytics/models/metrics**
Returns model performance metrics including success rates and response times.
**Response:**
```json
{
"model_metrics": [
{
"name": "claude-3-sonnet-20241022",
"avg_tokens": 250.5,
"avg_response_time": 1.2,
"success_rate": 0.95
}
]
}
```
---
## Cost Analytics Endpoints
### **GET /analytics/costs/summary**
Returns cost summary with averages and totals.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"totalCost": 25.50,
"totalTokens": 100000,
"totalRequests": 1000,
"avgDailyCost": 0.85,
"costPerThousandTokens": 0.255,
"daysInPeriod": 30,
"startDate": "2023-04-01",
"endDate": "2023-05-01"
}
```
### **GET /analytics/costs/daily**
Returns daily cost breakdown with filled gaps for missing dates.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"daily_costs": [
{
"date": "2023-05-01",
"cost": 1.25,
"tokens": 5000
}
]
}
```
### **GET /analytics/costs/models**
Returns cost breakdown by model.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"model_costs": [
{
"model_name": "claude-3-sonnet-20241022",
"cost": 15.50,
"tokens": 50000,
"requests": 500
}
]
}
```
### **GET /analytics/costs/projections**
Returns cost projections based on last 30 days usage.
**Response:**
```json
{
"nextMonth": 75.00,
"next3Months": 225.00,
"nextYear": 900.00,
"tokensNextMonth": 300000,
"dailyCost": 2.50,
"dailyTokens": 10000,
"baselineDays": 30
}
```
### **GET /analytics/costs/today**
Returns today's cost data.
**Response:**
```json
{
"date": "2023-05-01",
"cost": 2.50,
"tokens": 10000,
"requests": 100
}
```
---
## Tier Analytics Endpoints
### **GET /analytics/tiers/usage**
Returns usage data categorized by model tiers with aggregated statistics.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"tier_data": {
"tier_1": {
"name": "Basic",
"credits": 1,
"total_tokens": 50000,
"total_requests": 500,
"total_cost": 5.00,
"avg_tokens_per_query": 100,
"cost_per_1k_tokens": 0.10,
"total_credit_cost": 500,
"cost_per_credit": 0.01,
"models": [...]
}
},
"period": "30d",
"start_date": "2023-04-01",
"end_date": "2023-05-01"
}
```
### **GET /analytics/tiers/projections**
Returns tier-based cost and usage projections.
**Response:**
```json
{
"daily_usage": {...},
"projections": {
"monthly": {...},
"quarterly": {...},
"yearly": {...}
},
"tier_definitions": {...}
}
```
### **GET /analytics/tiers/efficiency**
Returns efficiency metrics by tier including cost per credit and tokens per credit.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"efficiency_data": {...},
"most_efficient_tier": "tier_2",
"best_value_tier": "tier_1",
"period": "30d",
"start_date": "2023-04-01",
"end_date": "2023-05-01"
}
```
---
## Code Execution Analytics Endpoints
### **GET /analytics/code-executions/summary**
Returns code execution statistics including success rates and model performance.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"period": "30d",
"start_date": "2023-04-01",
"end_date": "2023-05-01",
"overall_stats": {
"total_executions": 1000,
"successful_executions": 950,
"failed_executions": 50,
"success_rate": 0.95,
"total_users": 100,
"total_chats": 200
},
"model_performance": [...],
"failed_agents": [...]
}
```
### **GET /analytics/code-executions/detailed**
Returns detailed code execution records with filtering options.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
- `success_filter` (optional): Filter by success status (boolean)
- `user_id` (optional): Filter by user ID
- `model_name` (optional): Filter by model name
- `limit` (optional): Maximum results (default: `100`)
**Response:**
```json
{
"period": "30d",
"start_date": "2023-04-01",
"end_date": "2023-05-01",
"count": 50,
"executions": [...]
}
```
### **GET /analytics/code-executions/users**
Returns code execution statistics grouped by user.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
- `limit` (optional): Maximum users (default: `50`)
**Response:**
```json
{
"period": "30d",
"start_date": "2023-04-01",
"end_date": "2023-05-01",
"users": [...]
}
```
### **GET /analytics/code-executions/error-analysis**
Returns error analysis with categorized error types and agent failure patterns.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"period": "30d",
"start_date": "2023-04-01",
"end_date": "2023-05-01",
"total_failed_executions": 50,
"error_types": [...],
"error_by_agent": [...]
}
```
---
## Feedback Analytics Endpoints
### **GET /analytics/feedback/summary**
Returns feedback summary statistics including rating distributions and trends.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
**Response:**
```json
{
"period": "30d",
"start_date": "2023-04-01",
"end_date": "2023-05-01",
"total_feedback": 500,
"avg_rating": 4.2,
"chats_with_feedback": 200,
"ratings_distribution": [
{"rating": 1, "count": 10},
{"rating": 2, "count": 20},
{"rating": 3, "count": 50},
{"rating": 4, "count": 200},
{"rating": 5, "count": 220}
],
"models_data": [...],
"feedback_trend": [...]
}
```
### **GET /analytics/feedback/detailed**
Returns detailed feedback records with filtering and pagination.
**Query Parameters:**
- `period` (optional): Time period (`7d`, `30d`, `90d`, default: `30d`)
- `min_rating` (optional): Minimum rating filter
- `max_rating` (optional): Maximum rating filter
- `model_name` (optional): Filter by model name
- `limit` (optional): Maximum results (default: `100`)
- `offset` (optional): Pagination offset (default: `0`)
**Response:**
```json
{
"period": "30d",
"start_date": "2023-04-01",
"end_date": "2023-05-01",
"total": 500,
"count": 100,
"offset": 0,
"limit": 100,
"feedback": [...]
}
```
---
## Public Endpoints
### **GET /analytics/public/ticker**
Returns public ticker data for landing page statistics. **No authentication required.**
**Response:**
```json
{
"total_signups": 1000,
"total_tokens": 5000000,
"total_requests": 50000,
"last_updated": "2023-05-01T12:00:00Z"
}
```
---
## Utility Endpoints
### **GET /analytics/usage/summary**
Returns overall usage summary (legacy endpoint, calls dashboard with 30d period).
### **GET /analytics/debug/model_usage**
Debug endpoint for testing admin API key validation.
**Response:**
```json
{
"status": "success",
"message": "Admin API key validated successfully"
}
```
---
## Error Categorization
The system automatically categorizes code execution errors into the following types:
- **NameError**: Variable or function name not found
- **SyntaxError**: Invalid Python syntax
- **TypeError**: Type-related errors
- **AttributeError**: Attribute access errors
- **IndexError/KeyError**: Index or key access errors
- **ImportError**: Module import errors
- **ValueError**: Invalid values passed to functions
- **OperationError**: Unsupported operations
- **IndentationError**: Python indentation errors
- **PermissionError**: File/system permission errors
- **FileNotFoundError**: File access errors
- **MemoryError**: Memory allocation errors
- **TimeoutError**: Operation timeout errors
- **OtherError**: Uncategorized errors
## Real-time Updates
The analytics system supports real-time updates through WebSocket connections:
- **Dashboard updates**: Broadcasted when new model usage is recorded
- **User activity updates**: Broadcasted for user activity changes
- **Model performance updates**: Broadcasted for model-specific metrics
All real-time updates are sent as JSON messages with `type` field indicating the update category and `metrics` containing the delta or new values.
|