oxirs-gql 0.2.2

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
723
724
725
726
727
//! CPU Usage Profiling Per Resolver
//!
//! Tracks CPU usage for individual GraphQL resolvers to identify
//! performance bottlenecks and optimize resolver execution.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use sysinfo::{CpuRefreshKind, Pid, ProcessRefreshKind, ProcessesToUpdate, RefreshKind, System};
use tokio::sync::RwLock;

/// CPU profiler for resolvers
pub struct CpuProfiler {
    /// Resolver profiles
    profiles: Arc<RwLock<HashMap<String, ResolverCpuProfile>>>,
    /// Completed profiles
    completed: Arc<RwLock<Vec<CompletedCpuProfile>>>,
    /// System info
    system: Arc<RwLock<System>>,
    /// Configuration
    config: CpuProfilingConfig,
    /// Process ID
    pid: Pid,
}

/// CPU profiling configuration
#[derive(Debug, Clone)]
pub struct CpuProfilingConfig {
    /// Enable detailed profiling
    pub enable_detailed_profiling: bool,
    /// Sampling interval
    pub sampling_interval: Duration,
    /// Maximum profiles to store
    pub max_profiles: usize,
    /// Retention period
    pub retention_period: Duration,
    /// CPU threshold for warnings (percentage)
    pub warning_threshold_percent: f32,
}

impl Default for CpuProfilingConfig {
    fn default() -> Self {
        Self {
            enable_detailed_profiling: true,
            sampling_interval: Duration::from_millis(100),
            max_profiles: 1000,
            retention_period: Duration::from_secs(3600), // 1 hour
            warning_threshold_percent: 80.0,
        }
    }
}

/// Resolver CPU profile (active)
#[derive(Debug, Clone)]
pub struct ResolverCpuProfile {
    /// Resolver name
    pub resolver_name: String,
    /// Field path
    pub field_path: String,
    /// Start time
    pub start_time: Instant,
    /// Start timestamp
    pub start_timestamp: u64,
    /// CPU samples
    pub samples: Vec<CpuSample>,
    /// Initial CPU usage
    pub initial_cpu_percent: f32,
    /// Peak CPU usage
    pub peak_cpu_percent: f32,
}

impl ResolverCpuProfile {
    /// Create a new resolver CPU profile
    pub fn new(resolver_name: String, field_path: String, initial_cpu: f32) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("SystemTime should be after UNIX_EPOCH")
            .as_secs();

        Self {
            resolver_name,
            field_path,
            start_time: Instant::now(),
            start_timestamp: now,
            samples: Vec::new(),
            initial_cpu_percent: initial_cpu,
            peak_cpu_percent: initial_cpu,
        }
    }

    /// Add a CPU sample
    pub fn add_sample(&mut self, sample: CpuSample) {
        self.peak_cpu_percent = self.peak_cpu_percent.max(sample.cpu_percent);
        self.samples.push(sample);
    }

    /// Get elapsed time
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Get average CPU usage
    pub fn average_cpu_percent(&self) -> f32 {
        if self.samples.is_empty() {
            self.initial_cpu_percent
        } else {
            let sum: f32 = self.samples.iter().map(|s| s.cpu_percent).sum();
            sum / self.samples.len() as f32
        }
    }
}

/// CPU sample
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CpuSample {
    /// Timestamp offset from start (milliseconds)
    pub offset_ms: u64,
    /// CPU usage percentage
    pub cpu_percent: f32,
    /// Number of threads
    pub thread_count: usize,
}

impl CpuSample {
    /// Create a new CPU sample
    pub fn new(offset_ms: u64, cpu_percent: f32, thread_count: usize) -> Self {
        Self {
            offset_ms,
            cpu_percent,
            thread_count,
        }
    }
}

/// Completed CPU profile
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletedCpuProfile {
    /// Resolver name
    pub resolver_name: String,
    /// Field path
    pub field_path: String,
    /// Start time
    pub start_time: u64,
    /// End time
    pub end_time: u64,
    /// Duration in milliseconds
    pub duration_ms: u64,
    /// Initial CPU percentage
    pub initial_cpu_percent: f32,
    /// Peak CPU percentage
    pub peak_cpu_percent: f32,
    /// Average CPU percentage
    pub average_cpu_percent: f32,
    /// Number of samples
    pub sample_count: usize,
    /// CPU samples
    pub samples: Vec<CpuSample>,
    /// Metadata
    pub metadata: HashMap<String, String>,
}

impl CompletedCpuProfile {
    /// Create from active profile
    pub fn from_profile(profile: ResolverCpuProfile) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("SystemTime should be after UNIX_EPOCH")
            .as_secs();

        let duration_ms = profile.elapsed().as_millis() as u64;
        let average_cpu_percent = profile.average_cpu_percent();
        let sample_count = profile.samples.len();

        Self {
            resolver_name: profile.resolver_name,
            field_path: profile.field_path,
            start_time: profile.start_timestamp,
            end_time: now,
            duration_ms,
            initial_cpu_percent: profile.initial_cpu_percent,
            peak_cpu_percent: profile.peak_cpu_percent,
            average_cpu_percent,
            sample_count,
            samples: profile.samples,
            metadata: HashMap::new(),
        }
    }

    /// Check if CPU usage was high
    pub fn is_high_cpu(&self, threshold: f32) -> bool {
        self.average_cpu_percent >= threshold
    }
}

impl CpuProfiler {
    /// Create a new CPU profiler
    pub fn new(config: CpuProfilingConfig) -> Self {
        let pid = Pid::from_u32(std::process::id());

        let refresh_kind = RefreshKind::nothing()
            .with_cpu(CpuRefreshKind::everything())
            .with_processes(ProcessRefreshKind::nothing().with_cpu());

        Self {
            profiles: Arc::new(RwLock::new(HashMap::new())),
            completed: Arc::new(RwLock::new(Vec::new())),
            system: Arc::new(RwLock::new(System::new_with_specifics(refresh_kind))),
            config,
            pid,
        }
    }

    /// Start profiling a resolver
    pub async fn start_profiling(
        &self,
        profile_id: String,
        resolver_name: String,
        field_path: String,
    ) -> Result<()> {
        // Refresh system info
        let mut system = self.system.write().await;
        system.refresh_processes_specifics(
            ProcessesToUpdate::Some(&[self.pid]),
            false,
            ProcessRefreshKind::nothing().with_cpu(),
        );

        // Get current CPU usage
        let cpu_percent = system
            .process(self.pid)
            .map(|p| p.cpu_usage())
            .unwrap_or(0.0);

        drop(system);

        let profile = ResolverCpuProfile::new(resolver_name, field_path, cpu_percent);

        let mut profiles = self.profiles.write().await;
        profiles.insert(profile_id, profile);

        Ok(())
    }

    /// Record a CPU sample
    pub async fn record_sample(&self, profile_id: &str) -> Result<()> {
        let mut system = self.system.write().await;
        system.refresh_processes_specifics(
            ProcessesToUpdate::Some(&[self.pid]),
            false,
            ProcessRefreshKind::nothing().with_cpu(),
        );

        let cpu_percent = system
            .process(self.pid)
            .map(|p| p.cpu_usage())
            .unwrap_or(0.0);

        let thread_count = system
            .process(self.pid)
            .map(|p| p.tasks().map(|t| t.len()).unwrap_or(1))
            .unwrap_or(1);

        drop(system);

        let mut profiles = self.profiles.write().await;

        if let Some(profile) = profiles.get_mut(profile_id) {
            let offset_ms = profile.elapsed().as_millis() as u64;
            let sample = CpuSample::new(offset_ms, cpu_percent, thread_count);
            profile.add_sample(sample);

            // Check warning threshold
            if cpu_percent > self.config.warning_threshold_percent {
                tracing::warn!(
                    profile_id = %profile_id,
                    resolver = %profile.resolver_name,
                    cpu_percent = cpu_percent,
                    threshold = self.config.warning_threshold_percent,
                    "Resolver exceeded CPU warning threshold"
                );
            }
        }

        Ok(())
    }

    /// Stop profiling and get completed profile
    pub async fn stop_profiling(&self, profile_id: &str) -> Result<CompletedCpuProfile> {
        let mut profiles = self.profiles.write().await;

        let profile = profiles
            .remove(profile_id)
            .ok_or_else(|| anyhow::anyhow!("Profile not found: {}", profile_id))?;

        let completed_profile = CompletedCpuProfile::from_profile(profile);

        // Store in completed
        let mut completed = self.completed.write().await;
        completed.push(completed_profile.clone());

        // Cleanup old profiles
        self.cleanup_old_profiles(&mut completed).await;

        Ok(completed_profile)
    }

    /// Get current profile
    pub async fn get_profile(&self, profile_id: &str) -> Option<ResolverCpuProfile> {
        let profiles = self.profiles.read().await;
        profiles.get(profile_id).cloned()
    }

    /// Get completed profiles
    pub async fn get_completed_profiles(&self) -> Vec<CompletedCpuProfile> {
        let completed = self.completed.read().await;
        completed.clone()
    }

    /// Get profiles by resolver
    pub async fn get_profiles_by_resolver(&self, resolver_name: &str) -> Vec<CompletedCpuProfile> {
        let completed = self.completed.read().await;
        completed
            .iter()
            .filter(|p| p.resolver_name == resolver_name)
            .cloned()
            .collect()
    }

    /// Get top CPU consumers
    pub async fn get_top_consumers(&self, limit: usize) -> Vec<CompletedCpuProfile> {
        let completed = self.completed.read().await;

        let mut sorted = completed.clone();
        sorted.sort_by(|a, b| {
            b.average_cpu_percent
                .partial_cmp(&a.average_cpu_percent)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        sorted.truncate(limit);
        sorted
    }

    /// Get CPU statistics
    pub async fn get_statistics(&self) -> CpuStatistics {
        let profiles = self.profiles.read().await;
        let completed = self.completed.read().await;

        let active_profiles = profiles.len();
        let completed_profiles = completed.len();

        let mut total_cpu_percent = 0.0;
        let mut high_cpu_count = 0;

        for profile in completed.iter() {
            total_cpu_percent += profile.average_cpu_percent;
            if profile.is_high_cpu(self.config.warning_threshold_percent) {
                high_cpu_count += 1;
            }
        }

        let avg_cpu_percent = if completed_profiles > 0 {
            total_cpu_percent / completed_profiles as f32
        } else {
            0.0
        };

        CpuStatistics {
            active_profiles,
            completed_profiles,
            avg_cpu_percent,
            high_cpu_count,
        }
    }

    /// Cleanup old profiles
    async fn cleanup_old_profiles(&self, completed: &mut Vec<CompletedCpuProfile>) {
        let retention_secs = self.config.retention_period.as_secs();
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("SystemTime should be after UNIX_EPOCH")
            .as_secs();

        completed.retain(|p| now - p.end_time < retention_secs);

        // Also limit by count
        if completed.len() > self.config.max_profiles {
            let excess = completed.len() - self.config.max_profiles;
            completed.drain(0..excess);
        }
    }
}

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

/// CPU statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CpuStatistics {
    /// Active profiles
    pub active_profiles: usize,
    /// Completed profiles
    pub completed_profiles: usize,
    /// Average CPU percentage
    pub avg_cpu_percent: f32,
    /// High CPU count
    pub high_cpu_count: usize,
}

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

    #[test]
    fn test_resolver_cpu_profile_creation() {
        let profile =
            ResolverCpuProfile::new("UserResolver".to_string(), "Query.user".to_string(), 10.0);

        assert_eq!(profile.resolver_name, "UserResolver");
        assert_eq!(profile.field_path, "Query.user");
        assert_eq!(profile.initial_cpu_percent, 10.0);
        assert_eq!(profile.peak_cpu_percent, 10.0);
        assert_eq!(profile.samples.len(), 0);
    }

    #[test]
    fn test_resolver_cpu_profile_add_sample() {
        let mut profile =
            ResolverCpuProfile::new("UserResolver".to_string(), "Query.user".to_string(), 10.0);

        profile.add_sample(CpuSample::new(100, 20.0, 4));
        profile.add_sample(CpuSample::new(200, 30.0, 4));

        assert_eq!(profile.samples.len(), 2);
        assert_eq!(profile.peak_cpu_percent, 30.0);
    }

    #[test]
    fn test_resolver_cpu_profile_average() {
        let mut profile =
            ResolverCpuProfile::new("UserResolver".to_string(), "Query.user".to_string(), 10.0);

        profile.add_sample(CpuSample::new(100, 20.0, 4));
        profile.add_sample(CpuSample::new(200, 30.0, 4));
        profile.add_sample(CpuSample::new(300, 40.0, 4));

        assert_eq!(profile.average_cpu_percent(), 30.0);
    }

    #[test]
    fn test_cpu_sample_creation() {
        let sample = CpuSample::new(100, 25.5, 4);

        assert_eq!(sample.offset_ms, 100);
        assert_eq!(sample.cpu_percent, 25.5);
        assert_eq!(sample.thread_count, 4);
    }

    #[test]
    fn test_completed_cpu_profile_from_profile() {
        let mut profile =
            ResolverCpuProfile::new("UserResolver".to_string(), "Query.user".to_string(), 10.0);

        profile.add_sample(CpuSample::new(100, 20.0, 4));
        profile.add_sample(CpuSample::new(200, 30.0, 4));

        let completed = CompletedCpuProfile::from_profile(profile);

        assert_eq!(completed.resolver_name, "UserResolver");
        assert_eq!(completed.field_path, "Query.user");
        assert_eq!(completed.initial_cpu_percent, 10.0);
        assert_eq!(completed.peak_cpu_percent, 30.0);
        assert_eq!(completed.average_cpu_percent, 25.0);
        assert_eq!(completed.sample_count, 2);
    }

    #[test]
    fn test_completed_cpu_profile_is_high_cpu() {
        let mut profile =
            ResolverCpuProfile::new("UserResolver".to_string(), "Query.user".to_string(), 10.0);

        profile.add_sample(CpuSample::new(100, 80.0, 4));
        profile.add_sample(CpuSample::new(200, 90.0, 4));

        let completed = CompletedCpuProfile::from_profile(profile);

        assert!(completed.is_high_cpu(80.0));
        assert!(!completed.is_high_cpu(90.0));
    }

    #[tokio::test]
    async fn test_cpu_profiler_start_stop() {
        let profiler = CpuProfiler::default();

        profiler
            .start_profiling(
                "profile-1".to_string(),
                "UserResolver".to_string(),
                "Query.user".to_string(),
            )
            .await
            .expect("should succeed");

        let profile = profiler.get_profile("profile-1").await;
        assert!(profile.is_some());

        let completed = profiler
            .stop_profiling("profile-1")
            .await
            .expect("should succeed");
        assert_eq!(completed.resolver_name, "UserResolver");

        let profile = profiler.get_profile("profile-1").await;
        assert!(profile.is_none());
    }

    #[tokio::test]
    async fn test_cpu_profiler_record_sample() {
        let profiler = CpuProfiler::default();

        profiler
            .start_profiling(
                "profile-1".to_string(),
                "UserResolver".to_string(),
                "Query.user".to_string(),
            )
            .await
            .expect("should succeed");

        profiler
            .record_sample("profile-1")
            .await
            .expect("should succeed");
        profiler
            .record_sample("profile-1")
            .await
            .expect("should succeed");

        let profile = profiler
            .get_profile("profile-1")
            .await
            .expect("should succeed");
        assert!(profile.samples.len() >= 2);
    }

    #[tokio::test]
    async fn test_cpu_profiler_completed_profiles() {
        let profiler = CpuProfiler::default();

        profiler
            .start_profiling(
                "profile-1".to_string(),
                "UserResolver".to_string(),
                "Query.user".to_string(),
            )
            .await
            .expect("should succeed");

        profiler
            .stop_profiling("profile-1")
            .await
            .expect("should succeed");

        let completed = profiler.get_completed_profiles().await;
        assert_eq!(completed.len(), 1);
        assert_eq!(completed[0].resolver_name, "UserResolver");
    }

    #[tokio::test]
    async fn test_cpu_profiler_profiles_by_resolver() {
        let profiler = CpuProfiler::default();

        profiler
            .start_profiling(
                "profile-1".to_string(),
                "UserResolver".to_string(),
                "Query.user".to_string(),
            )
            .await
            .expect("should succeed");

        profiler
            .stop_profiling("profile-1")
            .await
            .expect("should succeed");

        profiler
            .start_profiling(
                "profile-2".to_string(),
                "PostResolver".to_string(),
                "Query.posts".to_string(),
            )
            .await
            .expect("should succeed");

        profiler
            .stop_profiling("profile-2")
            .await
            .expect("should succeed");

        let profiles = profiler.get_profiles_by_resolver("UserResolver").await;
        assert_eq!(profiles.len(), 1);
        assert_eq!(profiles[0].resolver_name, "UserResolver");
    }

    #[tokio::test]
    async fn test_cpu_profiler_top_consumers() {
        let profiler = CpuProfiler::default();

        // Profile 1 - low CPU
        profiler
            .start_profiling(
                "profile-1".to_string(),
                "Resolver1".to_string(),
                "Query.low".to_string(),
            )
            .await
            .expect("should succeed");
        profiler
            .stop_profiling("profile-1")
            .await
            .expect("should succeed");

        // Profile 2 - high CPU
        profiler
            .start_profiling(
                "profile-2".to_string(),
                "Resolver2".to_string(),
                "Query.high".to_string(),
            )
            .await
            .expect("should succeed");
        profiler
            .stop_profiling("profile-2")
            .await
            .expect("should succeed");

        let top = profiler.get_top_consumers(1).await;
        assert_eq!(top.len(), 1);
    }

    #[tokio::test]
    async fn test_cpu_profiler_statistics() {
        let profiler = CpuProfiler::default();

        profiler
            .start_profiling(
                "profile-1".to_string(),
                "UserResolver".to_string(),
                "Query.user".to_string(),
            )
            .await
            .expect("should succeed");

        profiler
            .start_profiling(
                "profile-2".to_string(),
                "PostResolver".to_string(),
                "Query.posts".to_string(),
            )
            .await
            .expect("should succeed");

        profiler
            .stop_profiling("profile-1")
            .await
            .expect("should succeed");

        let stats = profiler.get_statistics().await;
        assert_eq!(stats.active_profiles, 1); // profile-2 still active
        assert_eq!(stats.completed_profiles, 1);
    }

    #[tokio::test]
    async fn test_cpu_profiler_not_found() {
        let profiler = CpuProfiler::default();

        let result = profiler.stop_profiling("nonexistent").await;
        assert!(result.is_err());

        let profile = profiler.get_profile("nonexistent").await;
        assert!(profile.is_none());
    }

    #[test]
    fn test_cpu_profiling_config() {
        let config = CpuProfilingConfig::default();

        assert!(config.enable_detailed_profiling);
        assert_eq!(config.max_profiles, 1000);
        assert_eq!(config.warning_threshold_percent, 80.0);
    }

    #[test]
    fn test_resolver_cpu_profile_no_samples_average() {
        let profile =
            ResolverCpuProfile::new("UserResolver".to_string(), "Query.user".to_string(), 15.0);

        // With no samples, average should be initial CPU
        assert_eq!(profile.average_cpu_percent(), 15.0);
    }

    #[test]
    fn test_completed_cpu_profile_metadata() {
        let profile =
            ResolverCpuProfile::new("UserResolver".to_string(), "Query.user".to_string(), 10.0);

        let mut completed = CompletedCpuProfile::from_profile(profile);

        completed
            .metadata
            .insert("user_id".to_string(), "123".to_string());
        completed
            .metadata
            .insert("query_type".to_string(), "query".to_string());

        assert_eq!(completed.metadata.get("user_id"), Some(&"123".to_string()));
        assert_eq!(
            completed.metadata.get("query_type"),
            Some(&"query".to_string())
        );
    }
}