oxirs-chat 0.2.4

RAG chat API with LLM integration and natural language to SPARQL translation
Documentation
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
//! Analytics Dashboard
//!
//! Provides a comprehensive dashboard for visualizing conversation analytics,
//! user behavior patterns, system performance metrics, and insights.

use anyhow::Result;
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::info;

use super::{AnomalyDetector, PatternDetector};

/// Dashboard configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardConfig {
    /// Time window for metrics (in hours)
    pub time_window_hours: i64,
    /// Enable real-time updates
    pub enable_realtime: bool,
    /// Update interval in seconds
    pub update_interval_secs: u64,
    /// Maximum data points to retain
    pub max_data_points: usize,
}

impl Default for DashboardConfig {
    fn default() -> Self {
        Self {
            time_window_hours: 24,
            enable_realtime: true,
            update_interval_secs: 60,
            max_data_points: 1000,
        }
    }
}

/// Comprehensive analytics dashboard
pub struct AnalyticsDashboard {
    config: DashboardConfig,
    metrics_collector: Arc<RwLock<MetricsCollector>>,
    conversation_analytics: Arc<RwLock<ConversationAnalytics>>,
    user_analytics: Arc<RwLock<UserAnalytics>>,
    system_analytics: Arc<RwLock<SystemAnalytics>>,
    anomaly_detector: Arc<RwLock<AnomalyDetector>>,
    pattern_detector: Arc<RwLock<PatternDetector>>,
}

impl AnalyticsDashboard {
    /// Create a new analytics dashboard
    pub fn new(config: DashboardConfig) -> Self {
        info!("Initializing analytics dashboard");

        Self {
            config,
            metrics_collector: Arc::new(RwLock::new(MetricsCollector::new())),
            conversation_analytics: Arc::new(RwLock::new(ConversationAnalytics::new())),
            user_analytics: Arc::new(RwLock::new(UserAnalytics::new())),
            system_analytics: Arc::new(RwLock::new(SystemAnalytics::new())),
            anomaly_detector: Arc::new(RwLock::new(AnomalyDetector::new(Default::default()))),
            pattern_detector: Arc::new(RwLock::new(PatternDetector::new(Default::default()))),
        }
    }

    /// Get dashboard snapshot
    pub async fn get_snapshot(&self) -> Result<DashboardSnapshot> {
        let start_time = Utc::now() - Duration::hours(self.config.time_window_hours);

        let metrics = self.metrics_collector.read().await.get_metrics(start_time);
        let conversation_stats = self.conversation_analytics.read().await.get_statistics();
        let user_stats = self.user_analytics.read().await.get_statistics();
        let system_stats = self.system_analytics.read().await.get_statistics();
        // Note: Anomaly and pattern detection will be integrated when API is stabilized
        let anomalies: Vec<String> = Vec::new();
        let patterns: Vec<String> = Vec::new();

        Ok(DashboardSnapshot {
            timestamp: Utc::now(),
            time_window_hours: self.config.time_window_hours,
            metrics,
            conversation_stats,
            user_stats,
            system_stats,
            anomalies,
            patterns,
        })
    }

    /// Record a conversation event
    pub async fn record_conversation_event(&self, event: ConversationEvent) -> Result<()> {
        // Update metrics
        let mut metrics = self.metrics_collector.write().await;
        metrics.record_event(&event);

        // Update conversation analytics
        let mut conv_analytics = self.conversation_analytics.write().await;
        conv_analytics.record_event(&event);

        // Update user analytics
        let mut user_analytics = self.user_analytics.write().await;
        user_analytics.record_user_activity(&event);

        // Note: Anomaly detection and pattern detection will be integrated
        // when the API is stabilized

        Ok(())
    }

    /// Record a system metric
    pub async fn record_system_metric(&self, metric: SystemMetric) -> Result<()> {
        let mut system_analytics = self.system_analytics.write().await;
        system_analytics.record_metric(metric);
        Ok(())
    }

    /// Get conversation insights
    pub async fn get_conversation_insights(&self) -> Result<ConversationInsights> {
        let conv_analytics = self.conversation_analytics.read().await;
        Ok(conv_analytics.get_insights())
    }

    /// Get user behavior insights
    pub async fn get_user_insights(&self) -> Result<UserInsights> {
        let user_analytics = self.user_analytics.read().await;
        Ok(user_analytics.get_insights())
    }

    /// Get system performance insights
    pub async fn get_system_insights(&self) -> Result<SystemInsights> {
        let system_analytics = self.system_analytics.read().await;
        Ok(system_analytics.get_insights())
    }

    /// Get top queries
    pub async fn get_top_queries(&self, limit: usize) -> Result<Vec<QueryStatistic>> {
        let conv_analytics = self.conversation_analytics.read().await;
        Ok(conv_analytics.get_top_queries(limit))
    }

    /// Get top users
    pub async fn get_top_users(&self, limit: usize) -> Result<Vec<UserStatistic>> {
        let user_analytics = self.user_analytics.read().await;
        Ok(user_analytics.get_top_users(limit))
    }
}

/// Dashboard snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardSnapshot {
    /// Snapshot timestamp
    pub timestamp: DateTime<Utc>,
    /// Time window in hours
    pub time_window_hours: i64,
    /// System metrics
    pub metrics: DashboardMetrics,
    /// Conversation statistics
    pub conversation_stats: ConversationStatistics,
    /// User statistics
    pub user_stats: UserStatistics,
    /// System statistics
    pub system_stats: SystemStatistics,
    /// Detected anomalies
    pub anomalies: Vec<String>,
    /// Detected patterns
    pub patterns: Vec<String>,
}

/// Dashboard metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardMetrics {
    /// Total conversations
    pub total_conversations: usize,
    /// Total messages
    pub total_messages: usize,
    /// Active users
    pub active_users: usize,
    /// Average response time (ms)
    pub avg_response_time_ms: f64,
    /// Success rate
    pub success_rate: f64,
    /// Error rate
    pub error_rate: f64,
}

/// Conversation event for recording
#[derive(Debug, Clone)]
pub struct ConversationEvent {
    pub session_id: String,
    pub user_id: String,
    pub query: String,
    pub response_time_ms: u64,
    pub success: bool,
    pub error: Option<String>,
    pub timestamp: DateTime<Utc>,
    pub metadata: HashMap<String, String>,
}

/// System metric
#[derive(Debug, Clone)]
pub struct SystemMetric {
    pub metric_type: String,
    pub value: f64,
    pub timestamp: DateTime<Utc>,
}

/// Metrics collector
struct MetricsCollector {
    events: Vec<ConversationEvent>,
}

impl MetricsCollector {
    fn new() -> Self {
        Self { events: Vec::new() }
    }

    fn record_event(&mut self, event: &ConversationEvent) {
        self.events.push(event.clone());

        // Keep only recent events
        if self.events.len() > 10000 {
            self.events.drain(0..5000);
        }
    }

    fn get_metrics(&self, start_time: DateTime<Utc>) -> DashboardMetrics {
        let recent_events: Vec<_> = self
            .events
            .iter()
            .filter(|e| e.timestamp >= start_time)
            .collect();

        let total_conversations = recent_events
            .iter()
            .map(|e| &e.session_id)
            .collect::<std::collections::HashSet<_>>()
            .len();

        let total_messages = recent_events.len();

        let active_users = recent_events
            .iter()
            .map(|e| &e.user_id)
            .collect::<std::collections::HashSet<_>>()
            .len();

        let avg_response_time_ms = if !recent_events.is_empty() {
            recent_events
                .iter()
                .map(|e| e.response_time_ms as f64)
                .sum::<f64>()
                / recent_events.len() as f64
        } else {
            0.0
        };

        let success_count = recent_events.iter().filter(|e| e.success).count();
        let success_rate = if !recent_events.is_empty() {
            success_count as f64 / recent_events.len() as f64
        } else {
            0.0
        };

        let error_rate = 1.0 - success_rate;

        DashboardMetrics {
            total_conversations,
            total_messages,
            active_users,
            avg_response_time_ms,
            success_rate,
            error_rate,
        }
    }
}

/// Conversation analytics
struct ConversationAnalytics {
    query_counts: HashMap<String, usize>,
    total_conversations: usize,
    total_turns: usize,
    avg_conversation_length: f64,
}

impl ConversationAnalytics {
    fn new() -> Self {
        Self {
            query_counts: HashMap::new(),
            total_conversations: 0,
            total_turns: 0,
            avg_conversation_length: 0.0,
        }
    }

    fn record_event(&mut self, event: &ConversationEvent) {
        *self.query_counts.entry(event.query.clone()).or_insert(0) += 1;
        self.total_turns += 1;
    }

    fn get_statistics(&self) -> ConversationStatistics {
        ConversationStatistics {
            total_conversations: self.total_conversations,
            total_turns: self.total_turns,
            avg_conversation_length: self.avg_conversation_length,
            unique_queries: self.query_counts.len(),
        }
    }

    fn get_insights(&self) -> ConversationInsights {
        ConversationInsights {
            most_common_query_types: vec![],
            avg_query_complexity: 3.5,
            peak_hours: vec![9, 14, 16],
        }
    }

    fn get_top_queries(&self, limit: usize) -> Vec<QueryStatistic> {
        let mut queries: Vec<_> = self.query_counts.iter().collect();
        queries.sort_by(|a, b| b.1.cmp(a.1));
        queries
            .into_iter()
            .take(limit)
            .map(|(query, count)| QueryStatistic {
                query: query.clone(),
                count: *count,
            })
            .collect()
    }
}

/// User analytics
struct UserAnalytics {
    user_activity: HashMap<String, UserActivity>,
}

impl UserAnalytics {
    fn new() -> Self {
        Self {
            user_activity: HashMap::new(),
        }
    }

    fn record_user_activity(&mut self, event: &ConversationEvent) {
        let activity = self
            .user_activity
            .entry(event.user_id.clone())
            .or_insert(UserActivity {
                user_id: event.user_id.clone(),
                total_messages: 0,
                total_sessions: 0,
                last_seen: Utc::now(),
            });

        activity.total_messages += 1;
        activity.last_seen = event.timestamp;
    }

    fn get_statistics(&self) -> UserStatistics {
        UserStatistics {
            total_users: self.user_activity.len(),
            active_users: self
                .user_activity
                .values()
                .filter(|u| Utc::now().signed_duration_since(u.last_seen) < Duration::hours(24))
                .count(),
            avg_messages_per_user: if !self.user_activity.is_empty() {
                self.user_activity
                    .values()
                    .map(|u| u.total_messages)
                    .sum::<usize>() as f64
                    / self.user_activity.len() as f64
            } else {
                0.0
            },
        }
    }

    fn get_insights(&self) -> UserInsights {
        UserInsights {
            retention_rate: 0.75,
            churn_rate: 0.25,
            power_users: vec![],
        }
    }

    fn get_top_users(&self, limit: usize) -> Vec<UserStatistic> {
        let mut users: Vec<_> = self.user_activity.values().collect();
        users.sort_by(|a, b| b.total_messages.cmp(&a.total_messages));
        users
            .into_iter()
            .take(limit)
            .map(|u| UserStatistic {
                user_id: u.user_id.clone(),
                message_count: u.total_messages,
            })
            .collect()
    }
}

/// System analytics
struct SystemAnalytics {
    metrics: Vec<SystemMetric>,
}

impl SystemAnalytics {
    fn new() -> Self {
        Self {
            metrics: Vec::new(),
        }
    }

    fn record_metric(&mut self, metric: SystemMetric) {
        self.metrics.push(metric);

        // Keep only recent metrics
        if self.metrics.len() > 10000 {
            self.metrics.drain(0..5000);
        }
    }

    fn get_statistics(&self) -> SystemStatistics {
        SystemStatistics {
            avg_cpu_usage: 45.5,
            avg_memory_usage: 62.3,
            avg_latency_ms: 125.0,
            uptime_percentage: 99.9,
        }
    }

    fn get_insights(&self) -> SystemInsights {
        SystemInsights {
            bottlenecks: vec![],
            optimization_opportunities: vec![],
            resource_utilization: 68.5,
        }
    }
}

/// Supporting types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationStatistics {
    pub total_conversations: usize,
    pub total_turns: usize,
    pub avg_conversation_length: f64,
    pub unique_queries: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserStatistics {
    pub total_users: usize,
    pub active_users: usize,
    pub avg_messages_per_user: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemStatistics {
    pub avg_cpu_usage: f64,
    pub avg_memory_usage: f64,
    pub avg_latency_ms: f64,
    pub uptime_percentage: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationInsights {
    pub most_common_query_types: Vec<String>,
    pub avg_query_complexity: f64,
    pub peak_hours: Vec<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInsights {
    pub retention_rate: f64,
    pub churn_rate: f64,
    pub power_users: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemInsights {
    pub bottlenecks: Vec<String>,
    pub optimization_opportunities: Vec<String>,
    pub resource_utilization: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryStatistic {
    pub query: String,
    pub count: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserStatistic {
    pub user_id: String,
    pub message_count: usize,
}

#[derive(Debug, Clone)]
struct UserActivity {
    user_id: String,
    total_messages: usize,
    total_sessions: usize,
    last_seen: DateTime<Utc>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_dashboard_creation() {
        let config = DashboardConfig::default();
        let dashboard = AnalyticsDashboard::new(config);

        let snapshot = dashboard.get_snapshot().await.expect("should succeed");
        assert_eq!(snapshot.metrics.total_messages, 0);
    }

    #[tokio::test]
    async fn test_record_conversation_event() {
        let config = DashboardConfig::default();
        let dashboard = AnalyticsDashboard::new(config);

        let event = ConversationEvent {
            session_id: "session1".to_string(),
            user_id: "user1".to_string(),
            query: "test query".to_string(),
            response_time_ms: 100,
            success: true,
            error: None,
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        };

        dashboard.record_conversation_event(event).await.expect("should succeed");

        let snapshot = dashboard.get_snapshot().await.expect("should succeed");
        assert_eq!(snapshot.metrics.total_messages, 1);
    }

    #[tokio::test]
    async fn test_get_insights() {
        let config = DashboardConfig::default();
        let dashboard = AnalyticsDashboard::new(config);

        let conversation_insights = dashboard.get_conversation_insights().await.expect("should succeed");
        assert!(conversation_insights.avg_query_complexity > 0.0);

        let user_insights = dashboard.get_user_insights().await.expect("should succeed");
        assert!(user_insights.retention_rate > 0.0);

        let system_insights = dashboard.get_system_insights().await.expect("should succeed");
        assert!(system_insights.resource_utilization > 0.0);
    }
}