oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//! Request Logging Handler
//!
//! Provides structured logging for HTTP requests and SPARQL queries.
//! Based on Apache Jena Fuseki's request logging system.
//!
//! GET /$/logs - Retrieve recent request logs
//! GET /$/logs/statistics - Get logging statistics

use axum::{
    extract::{Query, State},
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime};
use tracing::{debug, info};

/// Log entry for a single request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
    /// Unique request ID
    pub request_id: String,

    /// Timestamp when request started
    pub timestamp: SystemTime,

    /// HTTP method
    pub method: String,

    /// Request path
    pub path: String,

    /// Query string (if any)
    pub query_string: Option<String>,

    /// Remote client address
    pub remote_addr: Option<String>,

    /// User agent
    pub user_agent: Option<String>,

    /// Request content type
    pub content_type: Option<String>,

    /// Request body size in bytes
    pub request_size: Option<usize>,

    /// HTTP status code
    pub status_code: u16,

    /// Response body size in bytes
    pub response_size: Option<usize>,

    /// Request duration in milliseconds
    pub duration_ms: u64,

    /// SPARQL query (if applicable)
    pub sparql_query: Option<String>,

    /// Operation type (query, update, upload, etc.)
    pub operation_type: Option<String>,

    /// Error message (if failed)
    pub error: Option<String>,

    /// Additional metadata
    pub metadata: std::collections::HashMap<String, String>,
}

impl LogEntry {
    /// Create new log entry
    pub fn new(request_id: String, method: String, path: String) -> Self {
        Self {
            request_id,
            timestamp: SystemTime::now(),
            method,
            path,
            query_string: None,
            remote_addr: None,
            user_agent: None,
            content_type: None,
            request_size: None,
            status_code: 200,
            response_size: None,
            duration_ms: 0,
            sparql_query: None,
            operation_type: None,
            error: None,
            metadata: std::collections::HashMap::new(),
        }
    }

    /// Format log entry as JSON string
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).unwrap_or_else(|_| "{}".to_string())
    }

    /// Format log entry as text string
    pub fn to_text(&self) -> String {
        format!(
            "{} {} {} {} {}ms [{}]",
            self.request_id,
            self.method,
            self.path,
            self.status_code,
            self.duration_ms,
            self.timestamp
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs()
        )
    }

    /// Check if request was successful (2xx status)
    pub fn is_success(&self) -> bool {
        self.status_code >= 200 && self.status_code < 300
    }

    /// Check if request had error (4xx or 5xx status)
    pub fn is_error(&self) -> bool {
        self.status_code >= 400
    }
}

/// Request logger configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggerConfig {
    /// Enable request logging
    pub enabled: bool,

    /// Maximum number of log entries to keep in memory
    pub max_entries: usize,

    /// Log format (json or text)
    pub format: LogFormat,

    /// Minimum duration in ms to log slow queries
    pub slow_query_threshold_ms: Option<u64>,

    /// Log request bodies
    pub log_request_body: bool,

    /// Log response bodies
    pub log_response_body: bool,

    /// Log SPARQL queries
    pub log_sparql: bool,
}

impl Default for LoggerConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_entries: 10000,
            format: LogFormat::Json,
            slow_query_threshold_ms: Some(1000),
            log_request_body: false,
            log_response_body: false,
            log_sparql: true,
        }
    }
}

/// Log output format
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum LogFormat {
    Json,
    Text,
}

/// Request logger for tracking HTTP requests
#[derive(Clone)]
pub struct RequestLogger {
    config: Arc<RwLock<LoggerConfig>>,
    entries: Arc<RwLock<VecDeque<LogEntry>>>,
    statistics: Arc<RwLock<LogStatistics>>,
}

impl RequestLogger {
    /// Create new request logger with default config
    pub fn new() -> Self {
        Self::with_config(LoggerConfig::default())
    }

    /// Create request logger with custom config
    pub fn with_config(config: LoggerConfig) -> Self {
        Self {
            config: Arc::new(RwLock::new(config)),
            entries: Arc::new(RwLock::new(VecDeque::new())),
            statistics: Arc::new(RwLock::new(LogStatistics::default())),
        }
    }

    /// Log a request
    pub fn log_request(&self, entry: LogEntry) -> Result<(), LogError> {
        let config = self
            .config
            .read()
            .map_err(|e| LogError::Internal(format!("Lock error: {}", e)))?;

        if !config.enabled {
            return Ok(());
        }

        // Check slow query threshold
        if let Some(threshold) = config.slow_query_threshold_ms {
            if entry.duration_ms >= threshold {
                info!(
                    "Slow request: {} {} {}ms",
                    entry.method, entry.path, entry.duration_ms
                );
            }
        }

        // Update statistics
        {
            let mut stats = self
                .statistics
                .write()
                .map_err(|e| LogError::Internal(format!("Lock error: {}", e)))?;

            stats.total_requests += 1;

            if entry.is_success() {
                stats.successful_requests += 1;
            } else if entry.is_error() {
                stats.failed_requests += 1;
            }

            stats.total_duration_ms += entry.duration_ms;
            if entry.duration_ms > stats.max_duration_ms {
                stats.max_duration_ms = entry.duration_ms;
            }
            if entry.duration_ms < stats.min_duration_ms || stats.min_duration_ms == 0 {
                stats.min_duration_ms = entry.duration_ms;
            }

            if let Some(size) = entry.request_size {
                stats.total_request_bytes += size;
            }
            if let Some(size) = entry.response_size {
                stats.total_response_bytes += size;
            }
        }

        // Store entry
        let mut entries = self
            .entries
            .write()
            .map_err(|e| LogError::Internal(format!("Lock error: {}", e)))?;

        // Remove oldest if at capacity
        if entries.len() >= config.max_entries {
            entries.pop_front();
        }

        // Format and log
        match config.format {
            LogFormat::Json => debug!("Request: {}", entry.to_json()),
            LogFormat::Text => debug!("Request: {}", entry.to_text()),
        }

        entries.push_back(entry);

        Ok(())
    }

    /// Get recent log entries
    pub fn get_logs(
        &self,
        limit: Option<usize>,
        filter: Option<LogFilter>,
    ) -> Result<Vec<LogEntry>, LogError> {
        let entries = self
            .entries
            .read()
            .map_err(|e| LogError::Internal(format!("Lock error: {}", e)))?;

        let mut logs: Vec<LogEntry> = entries.iter().cloned().collect();

        // Apply filters
        if let Some(filter) = filter {
            if let Some(method) = filter.method {
                logs.retain(|e| e.method.eq_ignore_ascii_case(&method));
            }
            if let Some(min_duration) = filter.min_duration_ms {
                logs.retain(|e| e.duration_ms >= min_duration);
            }
            if let Some(status_code) = filter.status_code {
                logs.retain(|e| e.status_code == status_code);
            }
            if filter.errors_only.unwrap_or(false) {
                logs.retain(|e| e.is_error());
            }
        }

        // Apply limit (take most recent)
        let limit = limit.unwrap_or(logs.len());
        logs.reverse(); // Most recent first
        logs.truncate(limit);

        Ok(logs)
    }

    /// Get logging statistics
    pub fn get_statistics(&self) -> Result<LogStatistics, LogError> {
        let stats = self
            .statistics
            .read()
            .map_err(|e| LogError::Internal(format!("Lock error: {}", e)))?;

        Ok(stats.clone())
    }

    /// Clear all log entries
    pub fn clear_logs(&self) -> Result<(), LogError> {
        let mut entries = self
            .entries
            .write()
            .map_err(|e| LogError::Internal(format!("Lock error: {}", e)))?;

        entries.clear();
        info!("Cleared all log entries");

        Ok(())
    }

    /// Get current configuration
    pub fn get_config(&self) -> Result<LoggerConfig, LogError> {
        let config = self
            .config
            .read()
            .map_err(|e| LogError::Internal(format!("Lock error: {}", e)))?;

        Ok(config.clone())
    }

    /// Update configuration
    pub fn update_config(&self, new_config: LoggerConfig) -> Result<(), LogError> {
        let mut config = self
            .config
            .write()
            .map_err(|e| LogError::Internal(format!("Lock error: {}", e)))?;

        *config = new_config;
        info!("Updated logger configuration");

        Ok(())
    }
}

impl Default for RequestLogger {
    fn default() -> Self {
        Self::new()
    }
}

/// Logging statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LogStatistics {
    pub total_requests: u64,
    pub successful_requests: u64,
    pub failed_requests: u64,
    pub total_duration_ms: u64,
    pub min_duration_ms: u64,
    pub max_duration_ms: u64,
    pub total_request_bytes: usize,
    pub total_response_bytes: usize,
}

impl LogStatistics {
    /// Calculate average request duration
    pub fn avg_duration_ms(&self) -> f64 {
        if self.total_requests > 0 {
            self.total_duration_ms as f64 / self.total_requests as f64
        } else {
            0.0
        }
    }

    /// Calculate success rate percentage
    pub fn success_rate(&self) -> f64 {
        if self.total_requests > 0 {
            (self.successful_requests as f64 / self.total_requests as f64) * 100.0
        } else {
            0.0
        }
    }
}

/// Log filter criteria
#[derive(Debug, Clone, Deserialize)]
pub struct LogFilter {
    pub method: Option<String>,
    pub min_duration_ms: Option<u64>,
    pub status_code: Option<u16>,
    pub errors_only: Option<bool>,
}

/// Log error types
#[derive(Debug, thiserror::Error)]
pub enum LogError {
    #[error("Internal error: {0}")]
    Internal(String),

    #[error("Invalid filter: {0}")]
    InvalidFilter(String),
}

impl LogError {
    fn status_code(&self) -> StatusCode {
        match self {
            LogError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
            LogError::InvalidFilter(_) => StatusCode::BAD_REQUEST,
        }
    }
}

impl IntoResponse for LogError {
    fn into_response(self) -> Response {
        let status = self.status_code();
        let message = self.to_string();

        (
            status,
            Json(serde_json::json!({
                "error": message,
                "status": status.as_u16(),
            })),
        )
            .into_response()
    }
}

/// Query parameters for log retrieval
#[derive(Debug, Clone, Deserialize)]
pub struct LogQuery {
    pub limit: Option<usize>,
    pub method: Option<String>,
    pub min_duration_ms: Option<u64>,
    pub status_code: Option<u16>,
    pub errors_only: Option<bool>,
}

impl From<LogQuery> for Option<LogFilter> {
    fn from(query: LogQuery) -> Self {
        if query.method.is_none()
            && query.min_duration_ms.is_none()
            && query.status_code.is_none()
            && query.errors_only.is_none()
        {
            None
        } else {
            Some(LogFilter {
                method: query.method,
                min_duration_ms: query.min_duration_ms,
                status_code: query.status_code,
                errors_only: query.errors_only,
            })
        }
    }
}

/// Get recent request logs
///
/// GET /$/logs?limit=100&errors_only=true
pub async fn get_logs(
    Query(params): Query<LogQuery>,
    State(logger): State<Arc<RequestLogger>>,
) -> Result<Response, LogError> {
    info!("Get logs request (limit: {:?})", params.limit);

    let filter = params.clone().into();
    let logs = logger.get_logs(params.limit, filter)?;

    debug!("Returning {} log entries", logs.len());

    Ok((StatusCode::OK, Json(logs)).into_response())
}

/// Get logging statistics
///
/// GET /$/logs/statistics
pub async fn get_log_statistics(
    State(logger): State<Arc<RequestLogger>>,
) -> Result<Response, LogError> {
    info!("Get log statistics request");

    let stats = logger.get_statistics()?;

    debug!("Log statistics: {:?}", stats);

    Ok((StatusCode::OK, Json(stats)).into_response())
}

/// Clear all logs
///
/// DELETE /$/logs
pub async fn clear_logs(State(logger): State<Arc<RequestLogger>>) -> Result<Response, LogError> {
    info!("Clear logs request");

    logger.clear_logs()?;

    Ok((StatusCode::NO_CONTENT, ()).into_response())
}

/// Get logger configuration
///
/// GET /$/logs/config
pub async fn get_log_config(
    State(logger): State<Arc<RequestLogger>>,
) -> Result<Response, LogError> {
    info!("Get log config request");

    let config = logger.get_config()?;

    Ok((StatusCode::OK, Json(config)).into_response())
}

/// Update logger configuration
///
/// PUT /$/logs/config
pub async fn update_log_config(
    State(logger): State<Arc<RequestLogger>>,
    Json(config): Json<LoggerConfig>,
) -> Result<Response, LogError> {
    info!("Update log config request");

    logger.update_config(config.clone())?;

    debug!("Updated config: {:?}", config);

    Ok((StatusCode::OK, Json(config)).into_response())
}

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

    #[test]
    fn test_log_entry_creation() {
        let entry = LogEntry::new(
            "req-123".to_string(),
            "GET".to_string(),
            "/query".to_string(),
        );

        assert_eq!(entry.request_id, "req-123");
        assert_eq!(entry.method, "GET");
        assert_eq!(entry.path, "/query");
        assert_eq!(entry.status_code, 200);
    }

    #[test]
    fn test_log_entry_success_check() {
        let mut entry = LogEntry::new("req-1".to_string(), "GET".to_string(), "/".to_string());

        entry.status_code = 200;
        assert!(entry.is_success());
        assert!(!entry.is_error());

        entry.status_code = 404;
        assert!(!entry.is_success());
        assert!(entry.is_error());

        entry.status_code = 500;
        assert!(!entry.is_success());
        assert!(entry.is_error());
    }

    #[tokio::test]
    async fn test_logger_creation() {
        let logger = RequestLogger::new();
        let config = logger.get_config().unwrap();

        assert!(config.enabled);
        assert_eq!(config.format, LogFormat::Json);
    }

    #[tokio::test]
    async fn test_log_request() {
        let logger = RequestLogger::new();

        let entry = LogEntry::new("req-1".to_string(), "GET".to_string(), "/query".to_string());

        logger.log_request(entry).unwrap();

        let logs = logger.get_logs(None, None).unwrap();
        assert_eq!(logs.len(), 1);
    }

    #[tokio::test]
    async fn test_log_statistics() {
        let logger = RequestLogger::new();

        // Log successful request
        let mut entry1 =
            LogEntry::new("req-1".to_string(), "GET".to_string(), "/query".to_string());
        entry1.duration_ms = 100;
        entry1.status_code = 200;
        logger.log_request(entry1).unwrap();

        // Log failed request
        let mut entry2 = LogEntry::new(
            "req-2".to_string(),
            "POST".to_string(),
            "/update".to_string(),
        );
        entry2.duration_ms = 50;
        entry2.status_code = 500;
        logger.log_request(entry2).unwrap();

        let stats = logger.get_statistics().unwrap();
        assert_eq!(stats.total_requests, 2);
        assert_eq!(stats.successful_requests, 1);
        assert_eq!(stats.failed_requests, 1);
        assert_eq!(stats.total_duration_ms, 150);
        assert_eq!(stats.max_duration_ms, 100);
        assert_eq!(stats.min_duration_ms, 50);
    }

    #[tokio::test]
    async fn test_log_limit() {
        let config = LoggerConfig {
            max_entries: 5,
            ..Default::default()
        };
        let logger = RequestLogger::with_config(config);

        // Log 10 entries
        for i in 0..10 {
            let entry = LogEntry::new(format!("req-{}", i), "GET".to_string(), "/test".to_string());
            logger.log_request(entry).unwrap();
        }

        let logs = logger.get_logs(None, None).unwrap();
        assert_eq!(logs.len(), 5); // Should only keep last 5
    }

    #[tokio::test]
    async fn test_log_filtering() {
        let logger = RequestLogger::new();

        // Log various requests
        let mut entry1 =
            LogEntry::new("req-1".to_string(), "GET".to_string(), "/query".to_string());
        entry1.duration_ms = 100;
        logger.log_request(entry1).unwrap();

        let mut entry2 = LogEntry::new(
            "req-2".to_string(),
            "POST".to_string(),
            "/update".to_string(),
        );
        entry2.duration_ms = 500;
        logger.log_request(entry2).unwrap();

        let mut entry3 =
            LogEntry::new("req-3".to_string(), "GET".to_string(), "/query".to_string());
        entry3.duration_ms = 50;
        logger.log_request(entry3).unwrap();

        // Filter by method
        let filter = LogFilter {
            method: Some("GET".to_string()),
            min_duration_ms: None,
            status_code: None,
            errors_only: None,
        };
        let logs = logger.get_logs(None, Some(filter)).unwrap();
        assert_eq!(logs.len(), 2);

        // Filter by duration
        let filter = LogFilter {
            method: None,
            min_duration_ms: Some(100),
            status_code: None,
            errors_only: None,
        };
        let logs = logger.get_logs(None, Some(filter)).unwrap();
        assert_eq!(logs.len(), 2);
    }

    #[tokio::test]
    async fn test_clear_logs() {
        let logger = RequestLogger::new();

        // Log some entries
        for i in 0..5 {
            let entry = LogEntry::new(format!("req-{}", i), "GET".to_string(), "/test".to_string());
            logger.log_request(entry).unwrap();
        }

        assert_eq!(logger.get_logs(None, None).unwrap().len(), 5);

        logger.clear_logs().unwrap();

        assert_eq!(logger.get_logs(None, None).unwrap().len(), 0);
    }

    #[tokio::test]
    async fn test_statistics_calculations() {
        let logger = RequestLogger::new();

        // Log requests with known durations
        for i in 0..10 {
            let mut entry =
                LogEntry::new(format!("req-{}", i), "GET".to_string(), "/test".to_string());
            entry.duration_ms = 100;
            entry.status_code = if i < 8 { 200 } else { 500 };
            logger.log_request(entry).unwrap();
        }

        let stats = logger.get_statistics().unwrap();
        assert_eq!(stats.avg_duration_ms(), 100.0);
        assert_eq!(stats.success_rate(), 80.0);
    }
}