-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.rs
More file actions
697 lines (620 loc) · 19.9 KB
/
admin.rs
File metadata and controls
697 lines (620 loc) · 19.9 KB
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! Admin API for cortex-app-server.
//!
//! Provides administrative endpoints for managing sessions, viewing statistics,
//! and performing bulk operations.
use std::sync::Arc;
use axum::{
Json, Router,
extract::{Query, State},
routing::{get, post},
};
use chrono::Timelike;
use serde::{Deserialize, Serialize};
use crate::error::{AppError, AppResult};
use crate::state::AppState;
/// Create admin routes.
pub fn routes() -> Router<Arc<AppState>> {
Router::new()
// Statistics
.route("/admin/stats", get(get_stats))
.route("/admin/stats/sessions", get(get_session_stats))
.route("/admin/stats/usage", get(get_usage_stats))
// Sessions management
.route("/admin/sessions", get(list_all_sessions))
.route("/admin/sessions/bulk", post(bulk_action))
.route("/admin/sessions/export", get(export_sessions_csv))
// Shares management
.route("/admin/shares", get(list_all_shares))
.route("/admin/shares/cleanup", post(cleanup_expired_shares))
}
// ============================================================================
// Statistics Types
// ============================================================================
/// Overall server statistics.
#[derive(Debug, Serialize)]
pub struct ServerStats {
/// Server uptime in seconds.
pub uptime_seconds: u64,
/// Total number of stored sessions.
pub total_sessions: usize,
/// Number of active WebSocket connections.
pub active_connections: usize,
/// Number of active shares.
pub active_shares: usize,
/// Total number of messages across all sessions.
pub total_messages: usize,
/// Sessions created today.
pub sessions_today: usize,
/// Average messages per session.
pub avg_messages_per_session: f64,
}
/// Session statistics.
#[derive(Debug, Serialize)]
pub struct SessionStats {
/// Total sessions.
pub total: usize,
/// Sessions by status (if applicable).
pub by_status: serde_json::Value,
/// Sessions created in the last 24 hours.
pub last_24h: usize,
/// Sessions created in the last 7 days.
pub last_7d: usize,
/// Sessions created in the last 30 days.
pub last_30d: usize,
/// Top models used.
pub top_models: Vec<ModelUsage>,
}
/// Model usage statistics.
#[derive(Debug, Serialize)]
pub struct ModelUsage {
/// Model name.
pub model: String,
/// Number of sessions using this model.
pub session_count: usize,
/// Percentage of total sessions.
pub percentage: f64,
}
/// Usage statistics over time.
#[derive(Debug, Serialize)]
pub struct UsageStats {
/// Daily session counts for the last 30 days.
pub daily_sessions: Vec<DailyCount>,
/// Daily message counts for the last 30 days.
pub daily_messages: Vec<DailyCount>,
/// Peak usage hour (0-23).
pub peak_hour: u8,
/// Average sessions per day.
pub avg_sessions_per_day: f64,
}
/// Daily count entry.
#[derive(Debug, Serialize)]
pub struct DailyCount {
/// Date (YYYY-MM-DD).
pub date: String,
/// Count for the day.
pub count: usize,
}
// ============================================================================
// Session Management Types
// ============================================================================
/// Query parameters for listing sessions.
#[derive(Debug, Deserialize)]
pub struct ListSessionsQuery {
/// Search term for filtering.
#[serde(default)]
pub search: Option<String>,
/// Filter by model.
#[serde(default)]
pub model: Option<String>,
/// Filter by date range start (ISO 8601).
#[serde(default)]
pub from: Option<String>,
/// Filter by date range end (ISO 8601).
#[serde(default)]
pub to: Option<String>,
/// Page number (1-indexed).
#[serde(default = "default_page")]
pub page: usize,
/// Items per page.
#[serde(default = "default_limit")]
pub limit: usize,
/// Sort field.
#[serde(default = "default_sort")]
pub sort: String,
/// Sort order (asc/desc).
#[serde(default = "default_order")]
pub order: String,
}
fn default_page() -> usize {
1
}
fn default_limit() -> usize {
50
}
fn default_sort() -> String {
"updated_at".to_string()
}
fn default_order() -> String {
"desc".to_string()
}
/// Paginated session list response.
#[derive(Debug, Serialize)]
pub struct SessionListResponse {
/// Sessions in the current page.
pub sessions: Vec<AdminSessionInfo>,
/// Total number of sessions matching the query.
pub total: usize,
/// Current page.
pub page: usize,
/// Total number of pages.
pub total_pages: usize,
}
/// Session information for admin view.
#[derive(Debug, Serialize)]
pub struct AdminSessionInfo {
/// Session ID.
pub id: String,
/// Session title.
pub title: Option<String>,
/// Model used.
pub model: String,
/// Working directory.
pub cwd: String,
/// Number of messages.
pub message_count: usize,
/// Creation timestamp (ISO 8601).
pub created_at: String,
/// Last update timestamp (ISO 8601).
pub updated_at: String,
}
/// Bulk action request.
#[derive(Debug, Deserialize)]
pub struct BulkActionRequest {
/// Session IDs to operate on.
pub session_ids: Vec<String>,
/// Action to perform.
pub action: BulkAction,
}
/// Bulk action types.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BulkAction {
/// Delete the sessions.
Delete,
/// Export the sessions.
Export,
}
/// Bulk action response.
#[derive(Debug, Serialize)]
pub struct BulkActionResponse {
/// Whether the action succeeded.
pub success: bool,
/// Number of sessions affected.
pub affected: usize,
/// Errors encountered (if any).
#[serde(skip_serializing_if = "Vec::is_empty")]
pub errors: Vec<String>,
}
// ============================================================================
// Share Management Types
// ============================================================================
/// Query parameters for listing shares.
#[derive(Debug, Deserialize)]
pub struct ListSharesQuery {
/// Page number.
#[serde(default = "default_page")]
pub page: usize,
/// Items per page.
#[serde(default = "default_limit")]
pub limit: usize,
/// Include expired shares.
#[serde(default)]
pub include_expired: bool,
}
/// Share list response.
#[derive(Debug, Serialize)]
pub struct ShareListResponse {
/// Shares in the current page.
pub shares: Vec<AdminShareInfo>,
/// Total number of shares.
pub total: usize,
/// Current page.
pub page: usize,
/// Total pages.
pub total_pages: usize,
}
/// Share information for admin view.
#[derive(Debug, Serialize)]
pub struct AdminShareInfo {
/// Share token.
pub token: String,
/// Session ID.
pub session_id: String,
/// Session title.
pub title: Option<String>,
/// View count.
pub view_count: u32,
/// Max views (if set).
pub max_views: Option<u32>,
/// Whether the share is expired.
pub expired: bool,
/// Creation timestamp.
pub created_at: String,
/// Expiration timestamp.
pub expires_at: String,
}
/// Cleanup response.
#[derive(Debug, Serialize)]
pub struct CleanupResponse {
/// Number of items cleaned up.
pub cleaned: usize,
}
// ============================================================================
// Handlers
// ============================================================================
/// Get overall server statistics.
async fn get_stats(State(state): State<Arc<AppState>>) -> AppResult<Json<ServerStats>> {
let sessions = state
.cli_sessions
.storage()
.list_sessions()
.unwrap_or_default();
let total_sessions = sessions.len();
let active_shares = state.share_manager.count().await;
// Count messages across all sessions
let mut total_messages = 0;
for session in &sessions {
if let Ok(history) = state.cli_sessions.storage().read_history(&session.id) {
total_messages += history.len();
}
}
// Count sessions created today
let today_start = chrono::Utc::now()
.date_naive()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
let sessions_today = sessions
.iter()
.filter(|s| s.created_at >= today_start)
.count();
let avg_messages = if total_sessions > 0 {
total_messages as f64 / total_sessions as f64
} else {
0.0
};
Ok(Json(ServerStats {
uptime_seconds: state.uptime().as_secs(),
total_sessions,
active_connections: state.cli_sessions.count().await,
active_shares,
total_messages,
sessions_today,
avg_messages_per_session: (avg_messages * 100.0).round() / 100.0,
}))
}
/// Get session statistics.
async fn get_session_stats(State(state): State<Arc<AppState>>) -> AppResult<Json<SessionStats>> {
let sessions = state
.cli_sessions
.storage()
.list_sessions()
.unwrap_or_default();
let total = sessions.len();
let now = chrono::Utc::now().timestamp();
let day_secs = 24 * 60 * 60;
let last_24h = sessions
.iter()
.filter(|s| now - s.created_at < day_secs)
.count();
let last_7d = sessions
.iter()
.filter(|s| now - s.created_at < 7 * day_secs)
.count();
let last_30d = sessions
.iter()
.filter(|s| now - s.created_at < 30 * day_secs)
.count();
// Count model usage
let mut model_counts: std::collections::HashMap<String, usize> =
std::collections::HashMap::new();
for session in &sessions {
*model_counts.entry(session.model.clone()).or_insert(0) += 1;
}
let mut top_models: Vec<ModelUsage> = model_counts
.into_iter()
.map(|(model, count)| ModelUsage {
model,
session_count: count,
percentage: if total > 0 {
(count as f64 / total as f64 * 100.0 * 10.0).round() / 10.0
} else {
0.0
},
})
.collect();
top_models.sort_by(|a, b| b.session_count.cmp(&a.session_count));
top_models.truncate(10);
Ok(Json(SessionStats {
total,
by_status: serde_json::json!({
"active": total, // All stored sessions are considered "active"
}),
last_24h,
last_7d,
last_30d,
top_models,
}))
}
/// Get usage statistics over time.
async fn get_usage_stats(State(state): State<Arc<AppState>>) -> AppResult<Json<UsageStats>> {
let sessions = state
.cli_sessions
.storage()
.list_sessions()
.unwrap_or_default();
// Build daily counts for the last 30 days
let mut daily_sessions: std::collections::HashMap<String, usize> =
std::collections::HashMap::new();
let mut daily_messages: std::collections::HashMap<String, usize> =
std::collections::HashMap::new();
let mut hour_counts: [usize; 24] = [0; 24];
let now = chrono::Utc::now();
let thirty_days_ago = (now - chrono::Duration::days(30)).timestamp();
for session in &sessions {
if session.created_at >= thirty_days_ago {
// Format date
if let Some(dt) = chrono::DateTime::from_timestamp(session.created_at, 0) {
let date = dt.format("%Y-%m-%d").to_string();
*daily_sessions.entry(date.clone()).or_insert(0) += 1;
// Count hour for peak detection
let hour = dt.hour() as usize;
hour_counts[hour] += 1;
// Count messages
if let Ok(history) = state.cli_sessions.storage().read_history(&session.id) {
*daily_messages.entry(date).or_insert(0) += history.len();
}
}
}
}
// Convert to sorted vectors
let mut daily_sessions_vec: Vec<DailyCount> = daily_sessions
.into_iter()
.map(|(date, count)| DailyCount { date, count })
.collect();
daily_sessions_vec.sort_by(|a, b| a.date.cmp(&b.date));
let mut daily_messages_vec: Vec<DailyCount> = daily_messages
.into_iter()
.map(|(date, count)| DailyCount { date, count })
.collect();
daily_messages_vec.sort_by(|a, b| a.date.cmp(&b.date));
// Find peak hour
let peak_hour = hour_counts
.iter()
.enumerate()
.max_by_key(|(_, count)| *count)
.map(|(hour, _)| hour as u8)
.unwrap_or(0);
// Average sessions per day
let total_days = daily_sessions_vec.len().max(1);
let total_session_count: usize = daily_sessions_vec.iter().map(|d| d.count).sum();
let avg_sessions_per_day =
(total_session_count as f64 / total_days as f64 * 10.0).round() / 10.0;
Ok(Json(UsageStats {
daily_sessions: daily_sessions_vec,
daily_messages: daily_messages_vec,
peak_hour,
avg_sessions_per_day,
}))
}
/// List all sessions with filtering and pagination.
async fn list_all_sessions(
State(state): State<Arc<AppState>>,
Query(query): Query<ListSessionsQuery>,
) -> AppResult<Json<SessionListResponse>> {
let mut sessions = state
.cli_sessions
.storage()
.list_sessions()
.unwrap_or_default();
// Apply filters
if let Some(search) = &query.search {
let search_lower = search.to_lowercase();
sessions.retain(|s| {
s.id.to_lowercase().contains(&search_lower)
|| s.title
.as_ref()
.map(|t| t.to_lowercase().contains(&search_lower))
.unwrap_or(false)
});
}
if let Some(model) = &query.model {
sessions.retain(|s| &s.model == model);
}
if let Some(from) = &query.from
&& let Ok(from_dt) = chrono::DateTime::parse_from_rfc3339(from)
{
let from_ts = from_dt.timestamp();
sessions.retain(|s| s.created_at >= from_ts);
}
if let Some(to) = &query.to
&& let Ok(to_dt) = chrono::DateTime::parse_from_rfc3339(to)
{
let to_ts = to_dt.timestamp();
sessions.retain(|s| s.created_at <= to_ts);
}
// Sort
match (query.sort.as_str(), query.order.as_str()) {
("created_at", "asc") => sessions.sort_by_key(|s| s.created_at),
("created_at", "desc") => sessions.sort_by_key(|s| std::cmp::Reverse(s.created_at)),
("updated_at", "asc") => sessions.sort_by_key(|s| s.updated_at),
(_, _) => sessions.sort_by_key(|s| std::cmp::Reverse(s.updated_at)),
}
let total = sessions.len();
let total_pages = total.div_ceil(query.limit);
// Paginate
let start = (query.page - 1) * query.limit;
let sessions: Vec<AdminSessionInfo> = sessions
.into_iter()
.skip(start)
.take(query.limit)
.map(|s| {
let message_count = state
.cli_sessions
.storage()
.read_history(&s.id)
.map(|h| h.len())
.unwrap_or(0);
AdminSessionInfo {
id: s.id,
title: s.title,
model: s.model,
cwd: s.cwd,
message_count,
created_at: chrono::DateTime::from_timestamp(s.created_at, 0)
.map(|dt| dt.to_rfc3339())
.unwrap_or_default(),
updated_at: chrono::DateTime::from_timestamp(s.updated_at, 0)
.map(|dt| dt.to_rfc3339())
.unwrap_or_default(),
}
})
.collect();
Ok(Json(SessionListResponse {
sessions,
total,
page: query.page,
total_pages,
}))
}
/// Perform bulk actions on sessions.
async fn bulk_action(
State(state): State<Arc<AppState>>,
Json(req): Json<BulkActionRequest>,
) -> AppResult<Json<BulkActionResponse>> {
let mut affected = 0;
let mut errors = Vec::new();
match req.action {
BulkAction::Delete => {
for session_id in &req.session_ids {
match state.cli_sessions.storage().delete_session(session_id) {
Ok(_) => affected += 1,
Err(e) => errors.push(format!("{}: {}", session_id, e)),
}
}
}
BulkAction::Export => {
// Export is handled by the export endpoint
return Err(AppError::BadRequest(
"Use /admin/sessions/export endpoint for exporting".to_string(),
));
}
}
Ok(Json(BulkActionResponse {
success: errors.is_empty(),
affected,
errors,
}))
}
/// Export sessions as CSV.
async fn export_sessions_csv(
State(state): State<Arc<AppState>>,
) -> AppResult<axum::response::Response> {
use axum::http::header;
use axum::response::IntoResponse;
let sessions = state
.cli_sessions
.storage()
.list_sessions()
.unwrap_or_default();
let mut csv = String::new();
csv.push_str("id,title,model,cwd,message_count,created_at,updated_at\n");
for session in sessions {
let message_count = state
.cli_sessions
.storage()
.read_history(&session.id)
.map(|h| h.len())
.unwrap_or(0);
let title = session
.title
.as_ref()
.map(|t| format!("\"{}\"", t.replace('"', "\"\"")))
.unwrap_or_default();
let created_at = chrono::DateTime::from_timestamp(session.created_at, 0)
.map(|dt| dt.to_rfc3339())
.unwrap_or_default();
let updated_at = chrono::DateTime::from_timestamp(session.updated_at, 0)
.map(|dt| dt.to_rfc3339())
.unwrap_or_default();
csv.push_str(&format!(
"{},{},{},\"{}\",{},{},{}\n",
session.id,
title,
session.model,
session.cwd.replace('"', "\"\""),
message_count,
created_at,
updated_at,
));
}
Ok((
[
(header::CONTENT_TYPE, "text/csv"),
(
header::CONTENT_DISPOSITION,
"attachment; filename=sessions.csv",
),
],
csv,
)
.into_response())
}
/// List all shares.
async fn list_all_shares(
State(_state): State<Arc<AppState>>,
Query(query): Query<ListSharesQuery>,
) -> AppResult<Json<ShareListResponse>> {
// Get all shares from the manager (assuming it's a user, we get their shares)
// For admin, we'd need a method to get all shares regardless of user
// For now, return what's available
let _now = chrono::Utc::now().timestamp();
// Note: In a real implementation, ShareManager would have a list_all method
// For now, we use what's available
let shares: Vec<AdminShareInfo> = Vec::new(); // Placeholder
let total = shares.len();
let total_pages = (total + query.limit - 1).max(1) / query.limit.max(1);
Ok(Json(ShareListResponse {
shares,
total,
page: query.page,
total_pages,
}))
}
/// Cleanup expired shares.
async fn cleanup_expired_shares(
State(state): State<Arc<AppState>>,
) -> AppResult<Json<CleanupResponse>> {
let cleaned = state.share_manager.cleanup_expired().await;
Ok(Json(CleanupResponse { cleaned }))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bulk_action_deserialization() {
let json = r#"{"session_ids": ["a", "b"], "action": "delete"}"#;
let req: BulkActionRequest = serde_json::from_str(json).unwrap();
assert_eq!(req.session_ids.len(), 2);
assert!(matches!(req.action, BulkAction::Delete));
}
#[test]
fn test_list_sessions_query_defaults() {
let query: ListSessionsQuery = serde_json::from_str("{}").unwrap();
assert_eq!(query.page, 1);
assert_eq!(query.limit, 50);
assert_eq!(query.sort, "updated_at");
assert_eq!(query.order, "desc");
}
}