dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Metrics Collector
//!
//! This file implements a comprehensive metrics collection system for the Ri framework. It provides
//! tools for collecting, analyzing, and reporting performance and system metrics. The system includes
//! sliding window data structures for time-series data, quantile calculators for performance analysis,
//! and system metrics collectors for monitoring CPU, memory, disk, and network usage.
//!
//! ## Key Components
//!
//! - **RiSlidingWindow**: Sliding window for time-series data collection
//! - **RiQuantileCalculator**: Quantile calculator for performance metrics
//! - **RiPerformanceCollector**: Performance metrics collector with sliding window and quantile calculation
//! - **RiPerformanceMetrics**: Performance metrics snapshot structure
//! - **RiCPUMetrics**: CPU metrics structure
//! - **RiMemoryMetrics**: Memory metrics structure
//! - **RiDiskMetrics**: Disk metrics structure
//! - **RiNetworkMetrics**: Network metrics structure
//! - **RiSystemMetrics**: System metrics snapshot structure
//! - **RiSystemMetricsCollector**: System metrics collector
//!
//! ## Design Principles
//!
//! 1. **Time-Series Data**: Uses sliding windows for efficient time-series data collection
//! 2. **Performance Focus**: Includes quantile calculation for accurate performance analysis
//! 3. **System Monitoring**: Comprehensive system metrics collection
//! 4. **Serialization Support**: All metrics structures are serializable for easy reporting
//! 5. **Low Overhead**: Efficient data structures to minimize performance impact
//! 6. **Flexible Configuration**: Configurable window sizes and bucket sizes
//! 7. **Cross-Platform**: Uses sysinfo crate for cross-platform system metrics collection
//! 8. **Real-Time Analysis**: Provides real-time metrics calculation
//!
//! ## Usage
//!
//! ```rust
//! use ri::observability::{RiPerformanceCollector, RiSystemMetricsCollector};
//! use std::time::Duration;
//!
//! fn example() {
//!     // Create a performance collector with a 1-minute window and 5-second buckets
//!     let mut perf_collector = RiPerformanceCollector::new(
//!         Duration::from_secs(60),
//!         Duration::from_secs(5)
//!     );
//!     
//!     // Record a request
//!     perf_collector.record_request(12.5, false);
//!     
//!     // Get performance metrics
//!     let perf_metrics = perf_collector.get_metrics();
//!     println!("P50 latency: {}ms", perf_metrics.p50_latency_ms);
//!     println!("Throughput: {} rps", perf_metrics.throughput_rps);
//!     println!("Error rate: {:.2}%", perf_metrics.error_rate * 100.0);
//!     
//!     // Create a system metrics collector
//!     let mut sys_collector = RiSystemMetricsCollector::new();
//!     
//!     // Collect system metrics
//!     let sys_metrics = sys_collector.collect();
//!     println!("CPU usage: {:.2}%", sys_metrics.cpu.total_usage_percent);
//!     println!("Memory usage: {:.2}%", sys_metrics.memory.usage_percent);
//!     println!("Network received: {} bytes/s", sys_metrics.network.received_bytes_per_sec);
//! }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::time::{Duration, Instant};
use sysinfo::{CpuExt, DiskExt, NetworkExt, System, SystemExt};

/// Sliding window for time-series data collection.
///
/// This struct implements a sliding window for efficient time-series data collection. It divides
/// the window into buckets and automatically advances the window as time passes, removing old
/// data points.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct RiSlidingWindow<T> {
    /// Total size of the sliding window
    _window_size: Duration,
    /// Size of each bucket within the window
    bucket_size: Duration,
    /// Queue of buckets containing data points
    buckets: VecDeque<WindowBucket<T>>,
    /// Current time for window advancement
    current_time: Instant,
}

/// Internal bucket structure for the sliding window.
///
/// This struct represents a single bucket within the sliding window, containing a collection
/// of data points for a specific time interval.
#[allow(dead_code)]
#[derive(Debug, Clone)]
struct WindowBucket<T> {
    /// Start time of the bucket
    _start_time: Instant,
    /// End time of the bucket
    end_time: Instant,
    /// Data points collected in this bucket
    data_points: Vec<T>,
}

#[allow(dead_code)]
impl<T> RiSlidingWindow<T> {
    /// Creates a new sliding window with the specified window size and bucket size.
    ///
    /// # Parameters
    ///
    /// - `window_size`: Total size of the sliding window
    /// - `bucket_size`: Size of each bucket within the window
    ///
    /// # Returns
    ///
    /// A new RiSlidingWindow instance
    pub fn new(window_size: Duration, bucket_size: Duration) -> Self {
        let bucket_count = (window_size.as_millis() / bucket_size.as_millis()).max(1) as usize;
        let mut buckets = VecDeque::with_capacity(bucket_count);

        let now = Instant::now();
        for i in 0..bucket_count {
            let bucket_start = now - window_size
                + Duration::from_millis(i as u64 * bucket_size.as_millis() as u64);
            buckets.push_back(WindowBucket {
                _start_time: bucket_start,
                end_time: bucket_start + bucket_size,
                data_points: Vec::new(),
            });
        }

        Self {
            _window_size: window_size,
            bucket_size,
            buckets,
            current_time: now,
        }
    }

    /// Adds a data point to the current window.
    ///
    /// # Parameters
    ///
    /// - `value`: The data point to add
    pub fn add(&mut self, value: T) {
        self.advance_window();

        if let Some(current_bucket) = self.buckets.back_mut() {
            current_bucket.data_points.push(value);
        }
    }

    /// Gets all data points in the current window.
    ///
    /// # Returns
    ///
    /// A vector of references to all data points in the current window
    pub fn get_data_points(&self) -> Vec<&T> {
        self.buckets
            .iter()
            .flat_map(|bucket| &bucket.data_points)
            .collect()
    }

    /// Gets the count of data points in the current window.
    ///
    /// # Returns
    ///
    /// The number of data points in the current window
    pub fn count(&self) -> usize {
        self.buckets
            .iter()
            .map(|bucket| bucket.data_points.len())
            .sum()
    }

    /// Advances the window to the current time, removing old buckets.
    fn advance_window(&mut self) {
        let now = Instant::now();
        let elapsed = now.duration_since(self.current_time);

        if elapsed >= self.bucket_size {
            let buckets_to_advance = (elapsed.as_millis() / self.bucket_size.as_millis()) as usize;

            for _ in 0..buckets_to_advance.min(self.buckets.len()) {
                self.buckets.pop_front();

                let new_bucket_start = match self.buckets.back() {
                    Some(bucket) => bucket.end_time,
                    None => {
                        continue;
                    }
                };
                self.buckets.push_back(WindowBucket {
                    _start_time: new_bucket_start,
                    end_time: new_bucket_start + self.bucket_size,
                    data_points: Vec::new(),
                });
            }

            self.current_time = now;
        }
    }
}

/// Quantile calculator for performance metrics.
///
/// This struct provides methods for calculating quantiles (percentiles) from a set of data points.
/// It sorts the data and uses linear interpolation for non-integer indices.
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiQuantileCalculator {
    /// Sorted list of data points
    sorted_data: Vec<f64>,
}

#[allow(dead_code)]
impl Default for RiQuantileCalculator {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(dead_code)]
impl RiQuantileCalculator {
    /// Creates a new quantile calculator.
    ///
    /// # Returns
    ///
    /// A new RiQuantileCalculator instance
    pub fn new() -> Self {
        Self {
            sorted_data: Vec::new(),
        }
    }

    /// Adds a data point to the calculator.
    ///
    /// # Parameters
    ///
    /// - `value`: The data point to add
    pub fn add(&mut self, value: f64) {
        self.sorted_data.push(value);
    }

    /// Calculates the specified quantile (0.0 to 1.0).
    ///
    /// # Parameters
    ///
    /// - `q`: The quantile to calculate (0.0 to 1.0)
    ///
    /// # Returns
    ///
    /// An `Option<f64>` containing the calculated quantile, or None if the data is empty or q is out of range
    pub fn quantile(&mut self, q: f64) -> Option<f64> {
        if self.sorted_data.is_empty() || !(0.0..=1.0).contains(&q) {
            return None;
        }

        self.sorted_data.sort_by(|a, b| {
            a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
        });

        let n = self.sorted_data.len();
        let index = q * (n - 1) as f64;
        let lower = index.floor() as usize;
        let upper = index.ceil() as usize;

        if lower == upper {
            Some(self.sorted_data[lower])
        } else {
            let weight = index - lower as f64;
            let lower_val = self.sorted_data[lower];
            let upper_val = self.sorted_data[upper];
            Some(lower_val + weight * (upper_val - lower_val))
        }
    }

    /// Calculates multiple quantiles at once.
    ///
    /// # Parameters
    ///
    /// - `quantiles`: A slice of quantiles to calculate (0.0 to 1.0)
    ///
    /// # Returns
    ///
    /// A vector of `Option<f64>` containing the calculated quantiles
    pub fn quantiles(&mut self, quantiles: &[f64]) -> Vec<Option<f64>> {
        quantiles.iter().map(|&q| self.quantile(q)).collect()
    }

    /// Gets the minimum value in the data set.
    ///
    /// # Returns
    ///
    /// An `Option<f64>` containing the minimum value, or None if the data is empty
    pub fn min(&self) -> Option<f64> {
        self.sorted_data.iter().fold(None, |min, &val| match min {
            None => Some(val),
            Some(m) => Some(m.min(val)),
        })
    }

    /// Gets the maximum value in the data set.
    ///
    /// # Returns
    ///
    /// An `Option<f64>` containing the maximum value, or None if the data is empty
    pub fn max(&self) -> Option<f64> {
        self.sorted_data.iter().fold(None, |max, &val| match max {
            None => Some(val),
            Some(m) => Some(m.max(val)),
        })
    }

    /// Gets the mean value of the data set.
    ///
    /// # Returns
    ///
    /// An `Option<f64>` containing the mean value, or None if the data is empty
    pub fn mean(&self) -> Option<f64> {
        if self.sorted_data.is_empty() {
            return None;
        }

        let sum: f64 = self.sorted_data.iter().sum();
        Some(sum / self.sorted_data.len() as f64)
    }

    /// Gets the standard deviation of the data set.
    ///
    /// # Returns
    ///
    /// An `Option<f64>` containing the standard deviation, or None if the data is empty
    pub fn std_dev(&self) -> Option<f64> {
        let mean = self.mean()?;
        if self.sorted_data.len() <= 1 {
            return Some(0.0);
        }

        let variance: f64 = self
            .sorted_data
            .iter()
            .map(|&x| (x - mean).powi(2))
            .sum::<f64>()
            / (self.sorted_data.len() - 1) as f64;

        Some(variance.sqrt())
    }

    /// Clears all data from the calculator.
    pub fn clear(&mut self) {
        self.sorted_data.clear();
    }
}

/// Performance metrics collector with sliding window and quantile calculation.
///
/// This struct collects performance metrics using sliding windows and provides quantile-based
/// performance analysis.
#[allow(dead_code)]
pub struct RiPerformanceCollector {
    /// Sliding window for latency data
    latency_window: RiSlidingWindow<f64>,
    /// Sliding window for throughput data
    throughput_window: RiSlidingWindow<u64>,
    /// Sliding window for error rate data
    error_rate_window: RiSlidingWindow<bool>,
    /// Quantile calculator for performance analysis
    quantile_calculator: RiQuantileCalculator,
}

#[allow(dead_code)]
impl RiPerformanceCollector {
    /// Creates a new performance collector with the specified window size and bucket size.
    ///
    /// # Parameters
    ///
    /// - `window_size`: Total size of the sliding window
    /// - `bucket_size`: Size of each bucket within the window
    ///
    /// # Returns
    ///
    /// A new RiPerformanceCollector instance
    pub fn new(window_size: Duration, bucket_size: Duration) -> Self {
        Self {
            latency_window: RiSlidingWindow::new(window_size, bucket_size),
            throughput_window: RiSlidingWindow::new(window_size, bucket_size),
            error_rate_window: RiSlidingWindow::new(window_size, bucket_size),
            quantile_calculator: RiQuantileCalculator::new(),
        }
    }

    /// Records a request with latency and error status.
    ///
    /// # Parameters
    ///
    /// - `latency_ms`: The request latency in milliseconds
    /// - `is_error`: Whether the request resulted in an error
    pub fn record_request(&mut self, latency_ms: f64, is_error: bool) {
        self.latency_window.add(latency_ms);
        self.throughput_window.add(1);
        self.error_rate_window.add(is_error);
    }

    /// Gets the current performance metrics.
    ///
    /// # Returns
    ///
    /// A RiPerformanceMetrics instance containing the current performance metrics
    pub fn get_metrics(&mut self) -> RiPerformanceMetrics {
        let latencies: Vec<f64> = self
            .latency_window
            .get_data_points()
            .iter()
            .map(|&&x| x)
            .collect();

        // Update quantile calculator with current data
        self.quantile_calculator.clear();
        for &latency in &latencies {
            self.quantile_calculator.add(latency);
        }

        let p50 = self.quantile_calculator.quantile(0.5).unwrap_or(0.0);
        let p95 = self.quantile_calculator.quantile(0.95).unwrap_or(0.0);
        let p99 = self.quantile_calculator.quantile(0.99).unwrap_or(0.0);

        let errors: Vec<bool> = self
            .error_rate_window
            .get_data_points()
            .iter()
            .map(|&&x| x)
            .collect();

        let error_count = errors.iter().filter(|&&x| x).count();
        let total_requests = errors.len();
        let error_rate = if total_requests > 0 {
            error_count as f64 / total_requests as f64
        } else {
            0.0
        };

        RiPerformanceMetrics {
            p50_latency_ms: p50,
            p95_latency_ms: p95,
            p99_latency_ms: p99,
            mean_latency_ms: self.quantile_calculator.mean().unwrap_or(0.0),
            min_latency_ms: self.quantile_calculator.min().unwrap_or(0.0),
            max_latency_ms: self.quantile_calculator.max().unwrap_or(0.0),
            throughput_rps: self.throughput_window.count() as f64,
            error_rate,
            total_requests: total_requests as u64,
        }
    }
}

/// Performance metrics snapshot.
///
/// This struct represents a snapshot of performance metrics at a specific point in time.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiPerformanceMetrics {
    /// 50th percentile latency in milliseconds
    pub p50_latency_ms: f64,
    /// 95th percentile latency in milliseconds
    pub p95_latency_ms: f64,
    /// 99th percentile latency in milliseconds
    pub p99_latency_ms: f64,
    /// Mean latency in milliseconds
    pub mean_latency_ms: f64,
    /// Minimum latency in milliseconds
    pub min_latency_ms: f64,
    /// Maximum latency in milliseconds
    pub max_latency_ms: f64,
    /// Throughput in requests per second
    pub throughput_rps: f64,
    /// Error rate (0.0 to 1.0)
    pub error_rate: f64,
    /// Total number of requests
    pub total_requests: u64,
}

/// CPU metrics.
///
/// This struct represents CPU usage metrics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass(get_all))]
pub struct RiCPUMetrics {
    /// Total CPU usage percentage
    pub total_usage_percent: f64,
    /// Per-core CPU usage percentages
    pub per_core_usage: Vec<f64>,
    /// Number of context switches (platform dependent)
    pub context_switches: u64,
    /// Number of interrupts (platform dependent)
    pub interrupts: u64,
}

/// Memory metrics.
///
/// This struct represents memory usage metrics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass(get_all))]
pub struct RiMemoryMetrics {
    /// Total memory in bytes
    pub total_bytes: u64,
    /// Used memory in bytes
    pub used_bytes: u64,
    /// Free memory in bytes
    pub free_bytes: u64,
    /// Memory usage percentage
    pub usage_percent: f64,
    /// Total swap memory in bytes
    pub swap_total_bytes: u64,
    /// Used swap memory in bytes
    pub swap_used_bytes: u64,
    /// Free swap memory in bytes
    pub swap_free_bytes: u64,
    /// Swap usage percentage
    pub swap_usage_percent: f64,
}

/// Disk metrics.
///
/// This struct represents disk usage metrics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass(get_all))]
pub struct RiDiskMetrics {
    /// Total disk space in bytes
    pub total_bytes: u64,
    /// Used disk space in bytes
    pub used_bytes: u64,
    /// Free disk space in bytes
    pub free_bytes: u64,
    /// Disk usage percentage
    pub usage_percent: f64,
    /// Total bytes read (platform dependent)
    pub read_bytes: u64,
    /// Total bytes written (platform dependent)
    pub write_bytes: u64,
    /// Total read operations (platform dependent)
    pub read_count: u64,
    /// Total write operations (platform dependent)
    pub write_count: u64,
}

/// Network metrics.
///
/// This struct represents network usage metrics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass(get_all))]
pub struct RiNetworkMetrics {
    /// Total bytes received
    pub total_received_bytes: u64,
    /// Total bytes transmitted
    pub total_transmitted_bytes: u64,
    /// Bytes received per second
    pub received_bytes_per_sec: u64,
    /// Bytes transmitted per second
    pub transmitted_bytes_per_sec: u64,
    /// Total packets received
    pub total_received_packets: u64,
    /// Total packets transmitted
    pub total_transmitted_packets: u64,
    /// Packets received per second
    pub received_packets_per_sec: u64,
    /// Packets transmitted per second
    pub transmitted_packets_per_sec: u64,
}

/// System metrics snapshot.
///
/// This struct represents a snapshot of system metrics at a specific point in time.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiSystemMetrics {
    /// CPU metrics
    pub cpu: RiCPUMetrics,
    /// Memory metrics
    pub memory: RiMemoryMetrics,
    /// Disk metrics
    pub disk: RiDiskMetrics,
    /// Network metrics
    pub network: RiNetworkMetrics,
    /// Timestamp of the metrics collection (Unix timestamp in milliseconds)
    pub timestamp: u64,
}

/// System metrics collector.
///
/// This struct collects system metrics using the sysinfo crate, providing cross-platform
/// system monitoring capabilities.
#[allow(dead_code)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiSystemMetricsCollector {
    /// sysinfo System instance for collecting metrics
    system: System,
    /// Last network bytes received
    last_network_received: u64,
    /// Last network bytes transmitted
    last_network_transmitted: u64,
    /// Last network packets received
    last_network_received_packets: u64,
    /// Last network packets transmitted
    last_network_transmitted_packets: u64,
    /// Last network metrics collection time
    last_network_time: Instant,
}

#[allow(dead_code)]
impl Default for RiSystemMetricsCollector {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(dead_code)]
impl RiSystemMetricsCollector {
    /// Creates a new system metrics collector.
    ///
    /// # Returns
    ///
    /// A new RiSystemMetricsCollector instance
    pub fn new() -> Self {
        let mut system = System::new_all();
        system.refresh_all();

        let (received_bytes, transmitted_bytes, received_packets, transmitted_packets) =
            Self::get_network_total(&system);

        Self {
            system,
            last_network_received: received_bytes,
            last_network_transmitted: transmitted_bytes,
            last_network_received_packets: received_packets,
            last_network_transmitted_packets: transmitted_packets,
            last_network_time: Instant::now(),
        }
    }

    /// Collects the current system metrics.
    ///
    /// # Returns
    ///
    /// A RiSystemMetrics instance containing the current system metrics
    pub fn collect(&mut self) -> RiSystemMetrics {
        self.system.refresh_all();

        let cpu = self.get_cpu_metrics();
        let memory = self.get_memory_metrics();
        let disk = self.get_disk_metrics();
        let network = self.get_network_metrics();

        let timestamp = chrono::Utc::now().timestamp_millis() as u64;
        
        RiSystemMetrics {
            cpu,
            memory,
            disk,
            network,
            timestamp,
        }
    }

    /// Gets CPU metrics from the system.
    ///
    /// # Returns
    ///
    /// A RiCPUMetrics instance containing the CPU metrics
    fn get_cpu_metrics(&self) -> RiCPUMetrics {
        let total_usage = self.system.global_cpu_info().cpu_usage();
        let per_core_usage: Vec<f64> = self
            .system
            .cpus()
            .iter()
            .map(|cpu| cpu.cpu_usage() as f64)
            .collect();

        // Note: sysinfo crate doesn't expose context switches and interrupts on all platforms
        // These values will be 0 on platforms where they're not available
        RiCPUMetrics {
            total_usage_percent: total_usage as f64,
            per_core_usage,
            context_switches: 0,
            interrupts: 0,
        }
    }

    /// Gets memory metrics from the system.
    ///
    /// # Returns
    ///
    /// A RiMemoryMetrics instance containing the memory metrics
    fn get_memory_metrics(&self) -> RiMemoryMetrics {
        let total = self.system.total_memory();
        let used = self.system.used_memory();
        let free = self.system.free_memory();
        let usage_percent = (used as f64 / total as f64) * 100.0;

        let swap_total = self.system.total_swap();
        let swap_used = self.system.used_swap();
        let swap_free = self.system.free_swap();
        let swap_usage_percent = if swap_total > 0 {
            (swap_used as f64 / swap_total as f64) * 100.0
        } else {
            0.0
        };

        RiMemoryMetrics {
            total_bytes: total,
            used_bytes: used,
            free_bytes: free,
            usage_percent,
            swap_total_bytes: swap_total,
            swap_used_bytes: swap_used,
            swap_free_bytes: swap_free,
            swap_usage_percent,
        }
    }

    /// Gets disk metrics from the system.
    ///
    /// # Returns
    ///
    /// A RiDiskMetrics instance containing the disk metrics
    fn get_disk_metrics(&self) -> RiDiskMetrics {
        // Get first disk for now
        if let Some(disk) = self.system.disks().first() {
            let total = disk.total_space();
            let available = disk.available_space();
            let used = total - available;
            let usage_percent = (used as f64 / total as f64) * 100.0;

            // Note: sysinfo crate doesn't expose I/O statistics on all platforms
            // These values will be 0 on platforms where they're not available
            RiDiskMetrics {
                total_bytes: total,
                used_bytes: used,
                free_bytes: available,
                usage_percent,
                read_bytes: 0,
                write_bytes: 0,
                read_count: 0,
                write_count: 0,
            }
        } else {
            RiDiskMetrics {
                total_bytes: 0,
                used_bytes: 0,
                free_bytes: 0,
                usage_percent: 0.0,
                read_bytes: 0,
                write_bytes: 0,
                read_count: 0,
                write_count: 0,
            }
        }
    }

    /// Gets network metrics from the system.
    ///
    /// # Returns
    ///
    /// A RiNetworkMetrics instance containing the network metrics
    fn get_network_metrics(&mut self) -> RiNetworkMetrics {
        let (received_bytes, transmitted_bytes, received_packets, transmitted_packets) =
            Self::get_network_total(&self.system);

        let now = Instant::now();
        let elapsed = now
            .duration_since(self.last_network_time)
            .as_secs_f64()
            .max(1.0);

        let received_bytes_per_sec =
            ((received_bytes - self.last_network_received) as f64 / elapsed) as u64;
        let transmitted_bytes_per_sec =
            ((transmitted_bytes - self.last_network_transmitted) as f64 / elapsed) as u64;
        let received_packets_per_sec =
            ((received_packets - self.last_network_received_packets) as f64 / elapsed) as u64;
        let transmitted_packets_per_sec =
            ((transmitted_packets - self.last_network_transmitted_packets) as f64 / elapsed) as u64;

        // Update last values
        self.last_network_received = received_bytes;
        self.last_network_transmitted = transmitted_bytes;
        self.last_network_received_packets = received_packets;
        self.last_network_transmitted_packets = transmitted_packets;
        self.last_network_time = now;

        RiNetworkMetrics {
            total_received_bytes: received_bytes,
            total_transmitted_bytes: transmitted_bytes,
            received_bytes_per_sec,
            transmitted_bytes_per_sec,
            total_received_packets: received_packets,
            total_transmitted_packets: transmitted_packets,
            received_packets_per_sec,
            transmitted_packets_per_sec,
        }
    }

    /// Gets total network metrics from all interfaces.
    ///
    /// # Parameters
    ///
    /// - `system`: The sysinfo System instance
    ///
    /// # Returns
    ///
    /// A tuple containing (received_bytes, transmitted_bytes, received_packets, transmitted_packets)
    fn get_network_total(system: &System) -> (u64, u64, u64, u64) {
        let mut received_bytes = 0;
        let mut transmitted_bytes = 0;
        let mut received_packets = 0;
        let mut transmitted_packets = 0;

        for (_, data) in system.networks() {
            received_bytes += data.received();
            transmitted_bytes += data.transmitted();
            received_packets += data.packets_received();
            transmitted_packets += data.packets_transmitted();
        }

        (
            received_bytes,
            transmitted_bytes,
            received_packets,
            transmitted_packets,
        )
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiSystemMetricsCollector {
    #[new]
    fn py_new() -> Self {
        Self::new()
    }
    
    #[pyo3(name = "collect")]
    fn py_collect(&mut self) -> RiSystemMetrics {
        self.collect()
    }
    
    #[pyo3(name = "refresh")]
    fn py_refresh(&mut self) {
        self.system.refresh_all();
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiSystemMetrics {
    #[pyo3(name = "cpu")]
    fn py_cpu(&self) -> RiCPUMetrics {
        self.cpu.clone()
    }
    
    #[pyo3(name = "memory")]
    fn py_memory(&self) -> RiMemoryMetrics {
        self.memory.clone()
    }
    
    #[pyo3(name = "disk")]
    fn py_disk(&self) -> RiDiskMetrics {
        self.disk.clone()
    }
    
    #[pyo3(name = "network")]
    fn py_network(&self) -> RiNetworkMetrics {
        self.network.clone()
    }
    
    #[pyo3(name = "timestamp")]
    fn py_timestamp(&self) -> u64 {
        self.timestamp
    }
}