oxirs-gql 0.2.4

GraphQL façade for OxiRS with automatic schema generation from RDF ontologies
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
//! Query Performance Heatmaps
//!
//! This module provides performance heatmap generation for visualizing query
//! performance patterns over time and across different dimensions.
//!
//! # Features
//!
//! - **Time-based Heatmaps**: Visualize performance over time (hourly, daily)
//! - **Operation Heatmaps**: Performance by operation type/name
//! - **Field-level Heatmaps**: Per-field resolution performance
//! - **Percentile Heatmaps**: P50/P95/P99 performance distribution
//! - **Multiple Formats**: JSON, HTML, ASCII, CSV export
//! - **Configurable Buckets**: Time windows and performance buckets
//!
//! # Example
//!
//! ```rust,ignore
//! use oxirs_gql::performance_heatmap::{HeatmapGenerator, HeatmapConfig};
//!
//! let mut generator = HeatmapGenerator::new(HeatmapConfig::default());
//! generator.record_query("GetUser", 150);
//!
//! let heatmap = generator.generate_time_heatmap()?;
//! let html = heatmap.to_html();
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, SystemTime};

/// Heatmap configuration
#[derive(Debug, Clone)]
pub struct HeatmapConfig {
    /// Time bucket size (e.g., 1 hour)
    pub time_bucket_size: Duration,
    /// Performance buckets (ms thresholds)
    pub performance_buckets: Vec<u64>,
    /// Maximum data points to retain
    pub max_data_points: usize,
    /// Percentiles to track
    pub percentiles: Vec<u8>,
}

impl HeatmapConfig {
    /// Create new heatmap configuration
    pub fn new() -> Self {
        Self {
            time_bucket_size: Duration::from_secs(3600), // 1 hour
            performance_buckets: vec![10, 50, 100, 250, 500, 1000, 2500, 5000],
            max_data_points: 10000,
            percentiles: vec![50, 95, 99],
        }
    }

    /// Set time bucket size
    pub fn with_time_bucket_size(mut self, duration: Duration) -> Self {
        self.time_bucket_size = duration;
        self
    }

    /// Set performance buckets
    pub fn with_performance_buckets(mut self, buckets: Vec<u64>) -> Self {
        self.performance_buckets = buckets;
        self
    }

    /// Set max data points
    pub fn with_max_data_points(mut self, max: usize) -> Self {
        self.max_data_points = max;
        self
    }
}

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

/// Query performance data point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceDataPoint {
    /// Timestamp
    pub timestamp: SystemTime,
    /// Operation name
    pub operation: String,
    /// Duration in milliseconds
    pub duration_ms: u64,
    /// Field name (if field-level)
    pub field: Option<String>,
}

/// Heatmap cell
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeatmapCell {
    /// Time bucket start
    pub time_bucket: u64,
    /// Performance bucket index
    pub performance_bucket: usize,
    /// Count of queries in this cell
    pub count: usize,
    /// Average duration in this cell
    pub avg_duration_ms: f64,
}

/// Heatmap data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Heatmap {
    /// Heatmap cells
    pub cells: Vec<HeatmapCell>,
    /// Time bucket size (seconds)
    pub time_bucket_size: u64,
    /// Performance buckets (ms)
    pub performance_buckets: Vec<u64>,
    /// Start time
    pub start_time: SystemTime,
    /// End time
    pub end_time: SystemTime,
}

impl Heatmap {
    /// Get maximum count across all cells
    pub fn max_count(&self) -> usize {
        self.cells.iter().map(|c| c.count).max().unwrap_or(0)
    }

    /// Get minimum count across all cells
    pub fn min_count(&self) -> usize {
        self.cells.iter().map(|c| c.count).min().unwrap_or(0)
    }

    /// Convert to JSON
    pub fn to_json(&self) -> Result<String, String> {
        serde_json::to_string_pretty(self).map_err(|e| format!("JSON serialization failed: {}", e))
    }

    /// Convert to HTML heatmap visualization
    pub fn to_html(&self) -> String {
        let mut html = String::from("<html><head><style>\n");
        html.push_str(".heatmap { display: grid; gap: 2px; }\n");
        html.push_str(".cell { padding: 10px; text-align: center; border: 1px solid #ddd; }\n");
        html.push_str(".hot { background-color: #ff0000; color: white; }\n");
        html.push_str(".warm { background-color: #ffaa00; }\n");
        html.push_str(".cool { background-color: #00ff00; }\n");
        html.push_str(".cold { background-color: #e0e0e0; }\n");
        html.push_str("</style></head><body>\n");
        html.push_str("<h1>Performance Heatmap</h1>\n");
        html.push_str("<div class='heatmap'>\n");

        let max_count = self.max_count() as f64;

        for cell in &self.cells {
            let intensity = if max_count > 0.0 {
                cell.count as f64 / max_count
            } else {
                0.0
            };

            let class = if intensity > 0.75 {
                "hot"
            } else if intensity > 0.5 {
                "warm"
            } else if intensity > 0.25 {
                "cool"
            } else {
                "cold"
            };

            html.push_str(&format!(
                "<div class='cell {}'>Count: {}<br>Avg: {:.1}ms</div>\n",
                class, cell.count, cell.avg_duration_ms
            ));
        }

        html.push_str("</div></body></html>");
        html
    }

    /// Convert to ASCII art
    pub fn to_ascii(&self) -> String {
        let max_count = self.max_count();
        let mut output = String::from("Performance Heatmap:\n");

        for cell in &self.cells {
            let intensity = (cell.count * 10)
                .checked_div(max_count)
                .unwrap_or(0)
                .min(10);

            let bar = "".repeat(intensity);
            output.push_str(&format!(
                "Bucket {}: {} ({} queries, avg {:.1}ms)\n",
                cell.performance_bucket, bar, cell.count, cell.avg_duration_ms
            ));
        }

        output
    }

    /// Convert to CSV
    pub fn to_csv(&self) -> String {
        let mut csv = String::from("time_bucket,performance_bucket,count,avg_duration_ms\n");

        for cell in &self.cells {
            csv.push_str(&format!(
                "{},{},{},{:.2}\n",
                cell.time_bucket, cell.performance_bucket, cell.count, cell.avg_duration_ms
            ));
        }

        csv
    }
}

/// Heatmap generator
pub struct HeatmapGenerator {
    config: HeatmapConfig,
    data_points: Vec<PerformanceDataPoint>,
}

impl HeatmapGenerator {
    /// Create new heatmap generator
    pub fn new(config: HeatmapConfig) -> Self {
        Self {
            config,
            data_points: Vec::new(),
        }
    }

    /// Record a query performance data point
    pub fn record_query(&mut self, operation: impl Into<String>, duration_ms: u64) {
        self.record_query_at(operation, duration_ms, SystemTime::now());
    }

    /// Record query at specific time
    pub fn record_query_at(
        &mut self,
        operation: impl Into<String>,
        duration_ms: u64,
        timestamp: SystemTime,
    ) {
        self.data_points.push(PerformanceDataPoint {
            timestamp,
            operation: operation.into(),
            duration_ms,
            field: None,
        });

        // Trim if exceeds max
        if self.data_points.len() > self.config.max_data_points {
            let excess = self.data_points.len() - self.config.max_data_points;
            self.data_points.drain(0..excess);
        }
    }

    /// Record field-level performance
    pub fn record_field(
        &mut self,
        operation: impl Into<String>,
        field: impl Into<String>,
        duration_ms: u64,
    ) {
        self.data_points.push(PerformanceDataPoint {
            timestamp: SystemTime::now(),
            operation: operation.into(),
            duration_ms,
            field: Some(field.into()),
        });

        if self.data_points.len() > self.config.max_data_points {
            let excess = self.data_points.len() - self.config.max_data_points;
            self.data_points.drain(0..excess);
        }
    }

    /// Generate time-based heatmap
    pub fn generate_time_heatmap(&self) -> Result<Heatmap, String> {
        if self.data_points.is_empty() {
            return Err("No data points available".to_string());
        }

        let start_time = self
            .data_points
            .iter()
            .map(|p| p.timestamp)
            .min()
            .expect("collection should not be empty");
        let end_time = self
            .data_points
            .iter()
            .map(|p| p.timestamp)
            .max()
            .expect("collection should not be empty");

        let mut cells_map: HashMap<(u64, usize), Vec<u64>> = HashMap::new();

        for point in &self.data_points {
            let time_bucket = self.get_time_bucket(point.timestamp, start_time);
            let perf_bucket = self.get_performance_bucket(point.duration_ms);

            cells_map
                .entry((time_bucket, perf_bucket))
                .or_default()
                .push(point.duration_ms);
        }

        let mut cells: Vec<HeatmapCell> = cells_map
            .into_iter()
            .map(|((time_bucket, perf_bucket), durations)| {
                let count = durations.len();
                let avg_duration_ms = durations.iter().sum::<u64>() as f64 / count as f64;

                HeatmapCell {
                    time_bucket,
                    performance_bucket: perf_bucket,
                    count,
                    avg_duration_ms,
                }
            })
            .collect();

        cells.sort_by_key(|c| (c.time_bucket, c.performance_bucket));

        Ok(Heatmap {
            cells,
            time_bucket_size: self.config.time_bucket_size.as_secs(),
            performance_buckets: self.config.performance_buckets.clone(),
            start_time,
            end_time,
        })
    }

    /// Generate operation-based heatmap
    pub fn generate_operation_heatmap(&self) -> HashMap<String, Vec<HeatmapCell>> {
        let mut operation_maps: HashMap<String, HashMap<usize, Vec<u64>>> = HashMap::new();

        for point in &self.data_points {
            let perf_bucket = self.get_performance_bucket(point.duration_ms);

            operation_maps
                .entry(point.operation.clone())
                .or_default()
                .entry(perf_bucket)
                .or_default()
                .push(point.duration_ms);
        }

        operation_maps
            .into_iter()
            .map(|(operation, buckets)| {
                let cells: Vec<HeatmapCell> = buckets
                    .into_iter()
                    .map(|(perf_bucket, durations)| {
                        let count = durations.len();
                        let avg_duration_ms = durations.iter().sum::<u64>() as f64 / count as f64;

                        HeatmapCell {
                            time_bucket: 0,
                            performance_bucket: perf_bucket,
                            count,
                            avg_duration_ms,
                        }
                    })
                    .collect();

                (operation, cells)
            })
            .collect()
    }

    /// Get time bucket for timestamp
    fn get_time_bucket(&self, timestamp: SystemTime, start_time: SystemTime) -> u64 {
        let elapsed = timestamp
            .duration_since(start_time)
            .unwrap_or_default()
            .as_secs();
        elapsed / self.config.time_bucket_size.as_secs()
    }

    /// Get performance bucket index
    fn get_performance_bucket(&self, duration_ms: u64) -> usize {
        self.config
            .performance_buckets
            .iter()
            .position(|&threshold| duration_ms <= threshold)
            .unwrap_or(self.config.performance_buckets.len())
    }

    /// Get percentile statistics
    pub fn get_percentile_stats(&self) -> HashMap<u8, u64> {
        if self.data_points.is_empty() {
            return HashMap::new();
        }

        let mut durations: Vec<u64> = self.data_points.iter().map(|p| p.duration_ms).collect();
        durations.sort_unstable();

        let mut stats = HashMap::new();
        for &percentile in &self.config.percentiles {
            let index = (durations.len() * percentile as usize / 100).min(durations.len() - 1);
            stats.insert(percentile, durations[index]);
        }

        stats
    }

    /// Clear all data points
    pub fn clear(&mut self) {
        self.data_points.clear();
    }

    /// Get data point count
    pub fn data_point_count(&self) -> usize {
        self.data_points.len()
    }
}

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

    #[test]
    fn test_heatmap_config_creation() {
        let config = HeatmapConfig::new()
            .with_time_bucket_size(Duration::from_secs(1800))
            .with_performance_buckets(vec![100, 500, 1000])
            .with_max_data_points(5000);

        assert_eq!(config.time_bucket_size.as_secs(), 1800);
        assert_eq!(config.performance_buckets.len(), 3);
        assert_eq!(config.max_data_points, 5000);
    }

    #[test]
    fn test_heatmap_config_default() {
        let config = HeatmapConfig::default();

        assert_eq!(config.time_bucket_size.as_secs(), 3600);
        assert!(!config.performance_buckets.is_empty());
        assert_eq!(config.max_data_points, 10000);
    }

    #[test]
    fn test_performance_data_point() {
        let point = PerformanceDataPoint {
            timestamp: SystemTime::now(),
            operation: "GetUser".to_string(),
            duration_ms: 150,
            field: Some("user".to_string()),
        };

        assert_eq!(point.operation, "GetUser");
        assert_eq!(point.duration_ms, 150);
        assert_eq!(point.field, Some("user".to_string()));
    }

    #[test]
    fn test_heatmap_generator_creation() {
        let config = HeatmapConfig::default();
        let generator = HeatmapGenerator::new(config);

        assert_eq!(generator.data_point_count(), 0);
    }

    #[test]
    fn test_record_query() {
        let mut generator = HeatmapGenerator::new(HeatmapConfig::default());

        generator.record_query("GetUser", 100);
        generator.record_query("GetPosts", 250);

        assert_eq!(generator.data_point_count(), 2);
    }

    #[test]
    fn test_record_field() {
        let mut generator = HeatmapGenerator::new(HeatmapConfig::default());

        generator.record_field("GetUser", "profile", 50);

        assert_eq!(generator.data_point_count(), 1);
        assert!(generator.data_points[0].field.is_some());
    }

    #[test]
    fn test_max_data_points() {
        let config = HeatmapConfig::default().with_max_data_points(5);
        let mut generator = HeatmapGenerator::new(config);

        for i in 0..10 {
            generator.record_query(format!("Query{}", i), 100);
        }

        assert_eq!(generator.data_point_count(), 5);
    }

    #[test]
    fn test_generate_time_heatmap() {
        let mut generator = HeatmapGenerator::new(HeatmapConfig::default());

        let now = SystemTime::now();
        generator.record_query_at("Query1", 50, now);
        generator.record_query_at("Query2", 150, now);
        generator.record_query_at("Query3", 500, now);

        let heatmap = generator.generate_time_heatmap().expect("should succeed");

        assert!(!heatmap.cells.is_empty());
        assert_eq!(heatmap.start_time, now);
    }

    #[test]
    fn test_generate_time_heatmap_empty() {
        let generator = HeatmapGenerator::new(HeatmapConfig::default());

        let result = generator.generate_time_heatmap();

        assert!(result.is_err());
    }

    #[test]
    fn test_generate_operation_heatmap() {
        let mut generator = HeatmapGenerator::new(HeatmapConfig::default());

        generator.record_query("GetUser", 100);
        generator.record_query("GetUser", 150);
        generator.record_query("GetPosts", 500);

        let op_heatmap = generator.generate_operation_heatmap();

        assert!(op_heatmap.contains_key("GetUser"));
        assert!(op_heatmap.contains_key("GetPosts"));
    }

    #[test]
    fn test_performance_bucket() {
        let config = HeatmapConfig::default().with_performance_buckets(vec![100, 500, 1000]);
        let generator = HeatmapGenerator::new(config);

        assert_eq!(generator.get_performance_bucket(50), 0);
        assert_eq!(generator.get_performance_bucket(250), 1);
        assert_eq!(generator.get_performance_bucket(750), 2);
        assert_eq!(generator.get_performance_bucket(2000), 3);
    }

    #[test]
    fn test_get_percentile_stats() {
        let mut generator = HeatmapGenerator::new(HeatmapConfig::default());

        for i in 1..=100 {
            generator.record_query("Query", i * 10);
        }

        let stats = generator.get_percentile_stats();

        assert!(stats.contains_key(&50));
        assert!(stats.contains_key(&95));
        assert!(stats.contains_key(&99));

        let p50 = stats.get(&50).expect("should succeed");
        assert!(*p50 >= 400 && *p50 <= 600);
    }

    #[test]
    fn test_percentile_stats_empty() {
        let generator = HeatmapGenerator::new(HeatmapConfig::default());

        let stats = generator.get_percentile_stats();

        assert!(stats.is_empty());
    }

    #[test]
    fn test_clear() {
        let mut generator = HeatmapGenerator::new(HeatmapConfig::default());

        generator.record_query("Query1", 100);
        generator.record_query("Query2", 200);

        assert_eq!(generator.data_point_count(), 2);

        generator.clear();

        assert_eq!(generator.data_point_count(), 0);
    }

    #[test]
    fn test_heatmap_max_min_count() {
        let cells = vec![
            HeatmapCell {
                time_bucket: 0,
                performance_bucket: 0,
                count: 10,
                avg_duration_ms: 100.0,
            },
            HeatmapCell {
                time_bucket: 1,
                performance_bucket: 1,
                count: 50,
                avg_duration_ms: 200.0,
            },
            HeatmapCell {
                time_bucket: 2,
                performance_bucket: 2,
                count: 25,
                avg_duration_ms: 300.0,
            },
        ];

        let heatmap = Heatmap {
            cells,
            time_bucket_size: 3600,
            performance_buckets: vec![100, 500, 1000],
            start_time: SystemTime::now(),
            end_time: SystemTime::now(),
        };

        assert_eq!(heatmap.max_count(), 50);
        assert_eq!(heatmap.min_count(), 10);
    }

    #[test]
    fn test_heatmap_to_json() {
        let heatmap = Heatmap {
            cells: vec![HeatmapCell {
                time_bucket: 0,
                performance_bucket: 0,
                count: 10,
                avg_duration_ms: 100.0,
            }],
            time_bucket_size: 3600,
            performance_buckets: vec![100, 500],
            start_time: SystemTime::UNIX_EPOCH,
            end_time: SystemTime::UNIX_EPOCH,
        };

        let json = heatmap.to_json().expect("should succeed");

        assert!(json.contains("cells"));
        assert!(json.contains("time_bucket_size"));
    }

    #[test]
    fn test_heatmap_to_html() {
        let heatmap = Heatmap {
            cells: vec![HeatmapCell {
                time_bucket: 0,
                performance_bucket: 0,
                count: 10,
                avg_duration_ms: 100.0,
            }],
            time_bucket_size: 3600,
            performance_buckets: vec![100, 500],
            start_time: SystemTime::UNIX_EPOCH,
            end_time: SystemTime::UNIX_EPOCH,
        };

        let html = heatmap.to_html();

        assert!(html.contains("<html>"));
        assert!(html.contains("heatmap"));
        assert!(html.contains("Count: 10"));
    }

    #[test]
    fn test_heatmap_to_ascii() {
        let heatmap = Heatmap {
            cells: vec![HeatmapCell {
                time_bucket: 0,
                performance_bucket: 0,
                count: 10,
                avg_duration_ms: 100.0,
            }],
            time_bucket_size: 3600,
            performance_buckets: vec![100, 500],
            start_time: SystemTime::UNIX_EPOCH,
            end_time: SystemTime::UNIX_EPOCH,
        };

        let ascii = heatmap.to_ascii();

        assert!(ascii.contains("Performance Heatmap"));
        assert!(ascii.contains("Bucket"));
    }

    #[test]
    fn test_heatmap_to_csv() {
        let heatmap = Heatmap {
            cells: vec![HeatmapCell {
                time_bucket: 0,
                performance_bucket: 0,
                count: 10,
                avg_duration_ms: 100.0,
            }],
            time_bucket_size: 3600,
            performance_buckets: vec![100, 500],
            start_time: SystemTime::UNIX_EPOCH,
            end_time: SystemTime::UNIX_EPOCH,
        };

        let csv = heatmap.to_csv();

        assert!(csv.contains("time_bucket,performance_bucket,count,avg_duration_ms"));
        assert!(csv.contains("0,0,10,100"));
    }

    #[test]
    fn test_time_bucket_calculation() {
        let config = HeatmapConfig::default().with_time_bucket_size(Duration::from_secs(60));
        let generator = HeatmapGenerator::new(config);

        let start = UNIX_EPOCH;
        let t1 = start + Duration::from_secs(30);
        let t2 = start + Duration::from_secs(90);
        let t3 = start + Duration::from_secs(150);

        assert_eq!(generator.get_time_bucket(t1, start), 0);
        assert_eq!(generator.get_time_bucket(t2, start), 1);
        assert_eq!(generator.get_time_bucket(t3, start), 2);
    }
}