hojicha-runtime 0.2.2

Event handling and async runtime for Hojicha TUI framework
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
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
//! Advanced performance metrics and percentile latency tracking
//!
//! This module provides comprehensive performance monitoring capabilities
//! for production applications, including percentile latencies, throughput
//! metrics, and queue utilization statistics.

use hdrhistogram::Histogram;
use log::trace;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// Advanced event processing statistics with percentile tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdvancedEventStats {
    /// Basic event counts
    pub basic: BasicStats,

    /// Latency percentiles by priority
    pub latency: LatencyStats,

    /// Throughput metrics
    pub throughput: ThroughputStats,

    /// Queue utilization statistics
    pub queue: QueueStats,

    /// Time-windowed statistics
    pub windows: WindowedStats,
}

/// Basic event statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BasicStats {
    /// Total number of events processed
    pub total_events: usize,
    /// Number of high priority events processed
    pub high_priority_events: usize,
    /// Number of normal priority events processed
    pub normal_priority_events: usize,
    /// Number of low priority events processed
    pub low_priority_events: usize,
    /// Number of events dropped due to queue overflow
    pub dropped_events: usize,
    /// Number of times backpressure was activated
    pub backpressure_activations: usize,
}

/// Latency statistics with percentiles
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LatencyStats {
    /// High priority event latencies
    pub high_priority: LatencyPercentiles,

    /// Normal priority event latencies
    pub normal_priority: LatencyPercentiles,

    /// Low priority event latencies
    pub low_priority: LatencyPercentiles,

    /// Overall latencies across all priorities
    pub overall: LatencyPercentiles,

    /// Event type specific latencies
    pub by_type: HashMap<String, LatencyPercentiles>,
}

/// Latency percentiles in microseconds
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LatencyPercentiles {
    /// Minimum latency observed
    pub min: u64,
    /// 50th percentile (median) latency
    pub p50: u64,
    /// 75th percentile latency
    pub p75: u64,
    /// 90th percentile latency
    pub p90: u64,
    /// 95th percentile latency
    pub p95: u64,
    /// 99th percentile latency
    pub p99: u64,
    /// 99.9th percentile latency
    pub p999: u64,
    /// Maximum latency observed
    pub max: u64,
    /// Mean (average) latency
    pub mean: f64,
    /// Standard deviation of latency
    pub std_dev: f64,
    /// Number of latency measurements recorded
    pub count: u64,
}

impl Default for LatencyPercentiles {
    fn default() -> Self {
        Self {
            min: 0,
            p50: 0,
            p75: 0,
            p90: 0,
            p95: 0,
            p99: 0,
            p999: 0,
            max: 0,
            mean: 0.0,
            std_dev: 0.0,
            count: 0,
        }
    }
}

/// Throughput metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThroughputStats {
    /// Current events per second
    pub current_rate: f64,

    /// Peak rate observed
    pub peak_rate: f64,

    /// Average rate over last minute
    pub avg_rate_1m: f64,

    /// Average rate over last 5 minutes
    pub avg_rate_5m: f64,

    /// Average processing time per event (microseconds)
    pub avg_processing_time_us: f64,
}

impl Default for ThroughputStats {
    fn default() -> Self {
        Self {
            current_rate: 0.0,
            peak_rate: 0.0,
            avg_rate_1m: 0.0,
            avg_rate_5m: 0.0,
            avg_processing_time_us: 0.0,
        }
    }
}

/// Queue utilization statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueueStats {
    /// Current queue depth
    pub current_depth: usize,

    /// Maximum depth reached
    pub max_depth: usize,

    /// Average depth over time
    pub avg_depth: f64,

    /// Percentage of time at capacity
    pub saturation_percentage: f64,

    /// Queue growth rate (events/sec)
    pub growth_rate: f64,
}

impl Default for QueueStats {
    fn default() -> Self {
        Self {
            current_depth: 0,
            max_depth: 0,
            avg_depth: 0.0,
            saturation_percentage: 0.0,
            growth_rate: 0.0,
        }
    }
}

/// Time-windowed statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct WindowedStats {
    /// Last 60 seconds (1-second buckets)
    pub last_minute: Vec<BucketStats>,

    /// Last hour (1-minute buckets)
    pub last_hour: Vec<BucketStats>,
}

/// Statistics for a time bucket
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BucketStats {
    /// Unix timestamp in seconds for this bucket
    pub timestamp: u64,
    /// Number of events processed in this time bucket
    pub events_processed: usize,
    /// Number of events dropped in this time bucket
    pub events_dropped: usize,
    /// Average latency in microseconds for this bucket
    pub avg_latency_us: f64,
    /// 99th percentile latency in microseconds for this bucket
    pub p99_latency_us: u64,
}

/// Configuration for metrics collection
#[derive(Debug, Clone)]
pub struct MetricsConfig {
    /// Enable percentile tracking
    pub track_percentiles: bool,

    /// Track metrics by event type
    pub track_by_type: bool,

    /// Sampling rate (0.0 to 1.0)
    pub sampling_rate: f64,

    /// Maximum histogram size (limits memory usage)
    pub max_histogram_size: u64,

    /// Window size for rate calculations
    pub rate_window: Duration,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            track_percentiles: true,
            track_by_type: false,
            sampling_rate: 1.0,
            max_histogram_size: 100_000,
            rate_window: Duration::from_secs(60),
        }
    }
}

/// Latency tracker using HDR Histogram for efficient percentile calculation
struct LatencyTracker {
    histogram: Histogram<u64>,
}

impl LatencyTracker {
    fn new(max_value: u64) -> Self {
        let histogram = Histogram::new_with_max(max_value, 3).expect("Failed to create histogram");
        Self { histogram }
    }

    fn record(&mut self, latency_us: u64) {
        let _ = self.histogram.record(latency_us);
    }

    fn percentiles(&self) -> LatencyPercentiles {
        if self.histogram.is_empty() {
            return LatencyPercentiles::default();
        }

        LatencyPercentiles {
            min: self.histogram.min(),
            p50: self.histogram.value_at_percentile(50.0),
            p75: self.histogram.value_at_percentile(75.0),
            p90: self.histogram.value_at_percentile(90.0),
            p95: self.histogram.value_at_percentile(95.0),
            p99: self.histogram.value_at_percentile(99.0),
            p999: self.histogram.value_at_percentile(99.9),
            max: self.histogram.max(),
            mean: self.histogram.mean(),
            std_dev: self.histogram.stdev(),
            count: self.histogram.len(),
        }
    }

    fn reset(&mut self) {
        self.histogram.reset();
    }
}

/// Metrics collector for event processing
pub struct MetricsCollector {
    config: MetricsConfig,
    start_time: Instant,

    // Basic counters
    basic: Arc<Mutex<BasicStats>>,

    // Latency tracking
    high_priority_latency: Arc<Mutex<LatencyTracker>>,
    normal_priority_latency: Arc<Mutex<LatencyTracker>>,
    low_priority_latency: Arc<Mutex<LatencyTracker>>,
    overall_latency: Arc<Mutex<LatencyTracker>>,
    by_type_latency: Arc<Mutex<HashMap<String, LatencyTracker>>>,

    // Throughput tracking
    event_times: Arc<Mutex<Vec<Instant>>>,
    processing_times: Arc<Mutex<Vec<Duration>>>,
    peak_rate: Arc<Mutex<f64>>,

    // Queue tracking
    queue_depths: Arc<Mutex<Vec<(Instant, usize)>>>,
    max_queue_depth: Arc<Mutex<usize>>,
    time_at_capacity: Arc<Mutex<Duration>>,
    last_capacity_check: Arc<Mutex<Instant>>,

    // Windowed stats
    minute_buckets: Arc<Mutex<Vec<BucketStats>>>,
    hour_buckets: Arc<Mutex<Vec<BucketStats>>>,
}

impl MetricsCollector {
    /// Create a new metrics collector
    pub fn new(config: MetricsConfig) -> Self {
        let max_latency = 10_000_000; // 10 seconds in microseconds

        Self {
            config,
            start_time: Instant::now(),
            basic: Arc::new(Mutex::new(BasicStats::default())),
            high_priority_latency: Arc::new(Mutex::new(LatencyTracker::new(max_latency))),
            normal_priority_latency: Arc::new(Mutex::new(LatencyTracker::new(max_latency))),
            low_priority_latency: Arc::new(Mutex::new(LatencyTracker::new(max_latency))),
            overall_latency: Arc::new(Mutex::new(LatencyTracker::new(max_latency))),
            by_type_latency: Arc::new(Mutex::new(HashMap::new())),
            event_times: Arc::new(Mutex::new(Vec::new())),
            processing_times: Arc::new(Mutex::new(Vec::new())),
            peak_rate: Arc::new(Mutex::new(0.0)),
            queue_depths: Arc::new(Mutex::new(Vec::new())),
            max_queue_depth: Arc::new(Mutex::new(0)),
            time_at_capacity: Arc::new(Mutex::new(Duration::ZERO)),
            last_capacity_check: Arc::new(Mutex::new(Instant::now())),
            minute_buckets: Arc::new(Mutex::new(Vec::new())),
            hour_buckets: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Record an event being processed
    pub fn record_event(
        &self,
        priority: crate::priority_queue::Priority,
        latency: Duration,
        event_type: Option<&str>,
    ) {
        // Apply sampling
        if self.config.sampling_rate < 1.0 {
            // Simplified sampling: skip based on a simple counter
            use std::sync::atomic::{AtomicUsize, Ordering};
            static COUNTER: AtomicUsize = AtomicUsize::new(0);
            let count = COUNTER.fetch_add(1, Ordering::Relaxed);
            let sample_every = (1.0 / self.config.sampling_rate) as usize;
            if !count.is_multiple_of(sample_every) {
                return;
            }
        }

        let latency_us = latency.as_micros() as u64;

        // Update basic stats
        {
            let mut basic = self.basic.lock().unwrap();
            basic.total_events += 1;
            match priority {
                crate::priority_queue::Priority::High => basic.high_priority_events += 1,
                crate::priority_queue::Priority::Normal => basic.normal_priority_events += 1,
                crate::priority_queue::Priority::Low => basic.low_priority_events += 1,
            }
        }

        // Update latency histograms
        if self.config.track_percentiles {
            self.overall_latency.lock().unwrap().record(latency_us);

            match priority {
                crate::priority_queue::Priority::High => {
                    self.high_priority_latency
                        .lock()
                        .unwrap()
                        .record(latency_us);
                }
                crate::priority_queue::Priority::Normal => {
                    self.normal_priority_latency
                        .lock()
                        .unwrap()
                        .record(latency_us);
                }
                crate::priority_queue::Priority::Low => {
                    self.low_priority_latency.lock().unwrap().record(latency_us);
                }
            }

            if self.config.track_by_type {
                if let Some(event_type) = event_type {
                    let mut by_type = self.by_type_latency.lock().unwrap();
                    by_type
                        .entry(event_type.to_string())
                        .or_insert_with(|| LatencyTracker::new(10_000_000))
                        .record(latency_us);
                }
            }
        }

        // Update throughput tracking
        {
            let mut event_times = self.event_times.lock().unwrap();
            let now = Instant::now();
            event_times.push(now);

            // Keep only events in the rate window
            let cutoff = now - self.config.rate_window;
            event_times.retain(|t| *t > cutoff);

            // Calculate current rate
            if event_times.len() > 1 {
                let duration = now.duration_since(event_times[0]).as_secs_f64();
                if duration > 0.0 {
                    let rate = event_times.len() as f64 / duration;
                    let mut peak = self.peak_rate.lock().unwrap();
                    if rate > *peak {
                        *peak = rate;
                    }
                }
            }
        }

        // Record processing time
        self.processing_times.lock().unwrap().push(latency);

        trace!("Recorded event: priority={priority:?}, latency={latency_us}μs");
    }

    /// Record a dropped event
    pub fn record_dropped(&self) {
        self.basic.lock().unwrap().dropped_events += 1;
    }

    /// Record backpressure activation
    pub fn record_backpressure(&self) {
        self.basic.lock().unwrap().backpressure_activations += 1;
    }

    /// Update queue depth
    pub fn update_queue_depth(&self, depth: usize, capacity: usize) {
        let now = Instant::now();

        // Track queue depths over time
        {
            let mut depths = self.queue_depths.lock().unwrap();
            depths.push((now, depth));

            // Keep only recent depths
            let cutoff = now - Duration::from_secs(300); // 5 minutes
            depths.retain(|(t, _)| *t > cutoff);
        }

        // Update max depth
        {
            let mut max_depth = self.max_queue_depth.lock().unwrap();
            if depth > *max_depth {
                *max_depth = depth;
            }
        }

        // Track time at capacity
        if depth >= capacity {
            let mut last_check = self.last_capacity_check.lock().unwrap();
            let duration = now.duration_since(*last_check);
            *self.time_at_capacity.lock().unwrap() += duration;
            *last_check = now;
        }
    }

    /// Take a snapshot of current metrics
    pub fn snapshot(&self) -> AdvancedEventStats {
        let now = Instant::now();
        let elapsed = now.duration_since(self.start_time).as_secs_f64();

        // Calculate latency stats
        let latency = LatencyStats {
            high_priority: self.high_priority_latency.lock().unwrap().percentiles(),
            normal_priority: self.normal_priority_latency.lock().unwrap().percentiles(),
            low_priority: self.low_priority_latency.lock().unwrap().percentiles(),
            overall: self.overall_latency.lock().unwrap().percentiles(),
            by_type: self
                .by_type_latency
                .lock()
                .unwrap()
                .iter()
                .map(|(k, v)| (k.clone(), v.percentiles()))
                .collect(),
        };

        // Calculate throughput stats
        let throughput = {
            let event_times = self.event_times.lock().unwrap();
            let processing_times = self.processing_times.lock().unwrap();

            let current_rate = if event_times.len() > 1 {
                let duration = now.duration_since(event_times[0]).as_secs_f64();
                if duration > 0.0 {
                    event_times.len() as f64 / duration
                } else {
                    0.0
                }
            } else {
                0.0
            };

            let avg_processing = if !processing_times.is_empty() {
                let sum: Duration = processing_times.iter().sum();
                sum.as_micros() as f64 / processing_times.len() as f64
            } else {
                0.0
            };

            ThroughputStats {
                current_rate,
                peak_rate: *self.peak_rate.lock().unwrap(),
                avg_rate_1m: current_rate, // Simplified for now
                avg_rate_5m: current_rate, // Simplified for now
                avg_processing_time_us: avg_processing,
            }
        };

        // Calculate queue stats
        let queue = {
            let depths = self.queue_depths.lock().unwrap();
            let current_depth = depths.last().map(|(_, d)| *d).unwrap_or(0);

            let avg_depth = if !depths.is_empty() {
                depths.iter().map(|(_, d)| *d).sum::<usize>() as f64 / depths.len() as f64
            } else {
                0.0
            };

            let growth_rate = if depths.len() > 1 {
                let first = depths.first().unwrap().1 as f64;
                let last = depths.last().unwrap().1 as f64;
                let duration = depths
                    .last()
                    .unwrap()
                    .0
                    .duration_since(depths.first().unwrap().0)
                    .as_secs_f64();
                if duration > 0.0 {
                    (last - first) / duration
                } else {
                    0.0
                }
            } else {
                0.0
            };

            let saturation = if elapsed > 0.0 {
                self.time_at_capacity.lock().unwrap().as_secs_f64() / elapsed * 100.0
            } else {
                0.0
            };

            QueueStats {
                current_depth,
                max_depth: *self.max_queue_depth.lock().unwrap(),
                avg_depth,
                saturation_percentage: saturation,
                growth_rate,
            }
        };

        AdvancedEventStats {
            basic: self.basic.lock().unwrap().clone(),
            latency,
            throughput,
            queue,
            windows: WindowedStats::default(), // Simplified for now
        }
    }

    /// Reset all metrics
    pub fn reset(&self) {
        *self.basic.lock().unwrap() = BasicStats::default();
        self.high_priority_latency.lock().unwrap().reset();
        self.normal_priority_latency.lock().unwrap().reset();
        self.low_priority_latency.lock().unwrap().reset();
        self.overall_latency.lock().unwrap().reset();
        self.by_type_latency.lock().unwrap().clear();
        self.event_times.lock().unwrap().clear();
        self.processing_times.lock().unwrap().clear();
        *self.peak_rate.lock().unwrap() = 0.0;
        self.queue_depths.lock().unwrap().clear();
        *self.max_queue_depth.lock().unwrap() = 0;
        *self.time_at_capacity.lock().unwrap() = Duration::ZERO;
        self.minute_buckets.lock().unwrap().clear();
        self.hour_buckets.lock().unwrap().clear();
    }

    /// Export metrics in JSON format
    pub fn export_json(&self) -> String {
        let stats = self.snapshot();
        stats.export(ExportFormat::Json)
    }

    /// Export metrics in Prometheus format
    pub fn export_prometheus(&self) -> String {
        let stats = self.snapshot();
        stats.export(ExportFormat::Prometheus)
    }

    /// Export metrics in plain text format
    pub fn export_text(&self) -> String {
        let stats = self.snapshot();
        stats.export(ExportFormat::PlainText)
    }
}

/// Export format for metrics
#[derive(Debug, Clone, Copy)]
pub enum ExportFormat {
    /// Export as JSON format
    Json,
    /// Export in Prometheus metrics format
    Prometheus,
    /// Export as plain text
    PlainText,
}

impl AdvancedEventStats {
    /// Export metrics in the specified format
    pub fn export(&self, format: ExportFormat) -> String {
        match format {
            ExportFormat::Json => self.to_json(),
            ExportFormat::Prometheus => self.to_prometheus(),
            ExportFormat::PlainText => self.to_plain_text(),
        }
    }

    fn to_json(&self) -> String {
        serde_json::to_string_pretty(self)
            .unwrap_or_else(|e| format!("Failed to serialize metrics: {e}"))
    }

    fn to_prometheus(&self) -> String {
        let mut output = String::new();

        // Basic metrics
        output.push_str("# HELP hojicha_events_total Total events processed\n");
        output.push_str("# TYPE hojicha_events_total counter\n");
        output.push_str(&format!(
            "hojicha_events_total {{}} {}\n",
            self.basic.total_events
        ));
        output.push_str(&format!(
            "hojicha_events_total {{priority=\"high\"}} {}\n",
            self.basic.high_priority_events
        ));
        output.push_str(&format!(
            "hojicha_events_total {{priority=\"normal\"}} {}\n",
            self.basic.normal_priority_events
        ));
        output.push_str(&format!(
            "hojicha_events_total {{priority=\"low\"}} {}\n",
            self.basic.low_priority_events
        ));

        // Dropped events
        output.push_str("# HELP hojicha_events_dropped Total events dropped\n");
        output.push_str("# TYPE hojicha_events_dropped counter\n");
        output.push_str(&format!(
            "hojicha_events_dropped {{}} {}\n",
            self.basic.dropped_events
        ));

        // Latency metrics
        output.push_str("# HELP hojicha_event_latency_microseconds Event processing latency\n");
        output.push_str("# TYPE hojicha_event_latency_microseconds summary\n");

        for (priority, stats) in [
            ("high", &self.latency.high_priority),
            ("normal", &self.latency.normal_priority),
            ("low", &self.latency.low_priority),
        ] {
            output.push_str(&format!(
                "hojicha_event_latency_microseconds {{priority=\"{}\",quantile=\"0.5\"}} {}\n",
                priority, stats.p50
            ));
            output.push_str(&format!(
                "hojicha_event_latency_microseconds {{priority=\"{}\",quantile=\"0.9\"}} {}\n",
                priority, stats.p90
            ));
            output.push_str(&format!(
                "hojicha_event_latency_microseconds {{priority=\"{}\",quantile=\"0.95\"}} {}\n",
                priority, stats.p95
            ));
            output.push_str(&format!(
                "hojicha_event_latency_microseconds {{priority=\"{}\",quantile=\"0.99\"}} {}\n",
                priority, stats.p99
            ));
            output.push_str(&format!(
                "hojicha_event_latency_microseconds {{priority=\"{}\",quantile=\"0.999\"}} {}\n",
                priority, stats.p999
            ));
        }

        // Throughput metrics
        output.push_str("# HELP hojicha_throughput_rate Events per second\n");
        output.push_str("# TYPE hojicha_throughput_rate gauge\n");
        output.push_str(&format!(
            "hojicha_throughput_rate {{type=\"current\"}} {}\n",
            self.throughput.current_rate
        ));
        output.push_str(&format!(
            "hojicha_throughput_rate {{type=\"peak\"}} {}\n",
            self.throughput.peak_rate
        ));

        // Queue metrics
        output.push_str("# HELP hojicha_queue_depth Current queue depth\n");
        output.push_str("# TYPE hojicha_queue_depth gauge\n");
        output.push_str(&format!(
            "hojicha_queue_depth {{}} {}\n",
            self.queue.current_depth
        ));

        output.push_str("# HELP hojicha_queue_saturation Queue saturation percentage\n");
        output.push_str("# TYPE hojicha_queue_saturation gauge\n");
        output.push_str(&format!(
            "hojicha_queue_saturation {{}} {}\n",
            self.queue.saturation_percentage
        ));

        output
    }

    fn to_plain_text(&self) -> String {
        format!(
            "Event Processing Metrics\n\
            ========================\n\
            Total Events: {}\n\
            - High Priority: {}\n\
            - Normal Priority: {}\n\
            - Low Priority: {}\n\
            - Dropped: {}\n\n\
            Latency (μs):\n\
            - High Priority:   p50={} p95={} p99={} max={}\n\
            - Normal Priority: p50={} p95={} p99={} max={}\n\
            - Low Priority:    p50={} p95={} p99={} max={}\n\n\
            Throughput:\n\
            - Current Rate: {:.1} events/sec\n\
            - Peak Rate: {:.1} events/sec\n\
            - Avg Processing Time: {:.1} μs\n\n\
            Queue:\n\
            - Current Depth: {}\n\
            - Max Depth: {}\n\
            - Saturation: {:.1}%\n\
            - Growth Rate: {:.1} events/sec",
            self.basic.total_events,
            self.basic.high_priority_events,
            self.basic.normal_priority_events,
            self.basic.low_priority_events,
            self.basic.dropped_events,
            self.latency.high_priority.p50,
            self.latency.high_priority.p95,
            self.latency.high_priority.p99,
            self.latency.high_priority.max,
            self.latency.normal_priority.p50,
            self.latency.normal_priority.p95,
            self.latency.normal_priority.p99,
            self.latency.normal_priority.max,
            self.latency.low_priority.p50,
            self.latency.low_priority.p95,
            self.latency.low_priority.p99,
            self.latency.low_priority.max,
            self.throughput.current_rate,
            self.throughput.peak_rate,
            self.throughput.avg_processing_time_us,
            self.queue.current_depth,
            self.queue.max_depth,
            self.queue.saturation_percentage,
            self.queue.growth_rate,
        )
    }
}

/// Print a formatted metrics dashboard
pub fn print_metrics_dashboard(stats: &AdvancedEventStats) {
    eprintln!("╔══════════════════════════════════════════════════════════════╗");
    eprintln!("║                  Event Processing Metrics Dashboard          ║");
    eprintln!("╠══════════════════════════════════════════════════════════════╣");
    eprintln!("║ Throughput:                                                  ║");
    eprintln!(
        "║   Current: {:>8.1} evt/s   Peak: {:>8.1} evt/s           ║",
        stats.throughput.current_rate, stats.throughput.peak_rate
    );
    eprintln!(
        "║   Processing Time: {:>8.1} μs average                       ║",
        stats.throughput.avg_processing_time_us
    );
    eprintln!("║                                                              ║");
    eprintln!("║ Latencies (μs):      P50      P95      P99      Max         ║");
    eprintln!(
        "║   High Priority:  {:>7} {:>7} {:>7} {:>7}",
        stats.latency.high_priority.p50,
        stats.latency.high_priority.p95,
        stats.latency.high_priority.p99,
        stats.latency.high_priority.max
    );
    eprintln!(
        "║   Normal Priority:{:>7} {:>7} {:>7} {:>7}",
        stats.latency.normal_priority.p50,
        stats.latency.normal_priority.p95,
        stats.latency.normal_priority.p99,
        stats.latency.normal_priority.max
    );
    eprintln!(
        "║   Low Priority:   {:>7} {:>7} {:>7} {:>7}",
        stats.latency.low_priority.p50,
        stats.latency.low_priority.p95,
        stats.latency.low_priority.p99,
        stats.latency.low_priority.max
    );
    eprintln!("║                                                              ║");
    eprintln!("║ Queue:                                                       ║");
    eprintln!(
        "║   Depth: {:>5} (max: {:>5})   Saturation: {:>5.1}%         ║",
        stats.queue.current_depth, stats.queue.max_depth, stats.queue.saturation_percentage
    );
    eprintln!(
        "║   Growth Rate: {:>8.1} events/sec                          ║",
        stats.queue.growth_rate
    );
    eprintln!("║                                                              ║");
    eprintln!("║ Events:                                                      ║");
    eprintln!(
        "║   Total: {:>8}  Dropped: {:>6}  Backpressure: {:>6}",
        stats.basic.total_events, stats.basic.dropped_events, stats.basic.backpressure_activations
    );
    eprintln!("╚══════════════════════════════════════════════════════════════╝");
}

/// Display metrics dashboard  
pub fn display_dashboard(stats: &AdvancedEventStats) -> String {
    let mut output = String::new();
    use std::fmt::Write;

    let _ = writeln!(
        output,
        "╔══════════════════════════════════════════════════════════════╗"
    );
    let _ = writeln!(
        output,
        "║                    METRICS DASHBOARD                         ║"
    );
    let _ = writeln!(
        output,
        "╠══════════════════════════════════════════════════════════════╣"
    );

    // Event counts
    let _ = writeln!(
        output,
        "║ Events Processed:  {:>10}                                ║",
        stats.basic.total_events
    );
    let _ = writeln!(
        output,
        "║ Events Dropped:    {:>10}                                ║",
        stats.basic.dropped_events
    );
    let _ = writeln!(
        output,
        "║ Backpressure:      {:>10}                                ║",
        stats.basic.backpressure_activations
    );

    let _ = writeln!(
        output,
        "╠══════════════════════════════════════════════════════════════╣"
    );

    // Latency percentiles (overall)
    let _ = writeln!(
        output,
        "║ LATENCY (μs) - Overall                                       ║"
    );
    let _ = writeln!(
        output,
        "║   p50:  {:>8.1}    p75:  {:>8.1}    p90:  {:>8.1}",
        stats.latency.overall.p50, stats.latency.overall.p75, stats.latency.overall.p90
    );
    let _ = writeln!(
        output,
        "║   p95:  {:>8.1}    p99:  {:>8.1}    p999: {:>8.1}",
        stats.latency.overall.p95, stats.latency.overall.p99, stats.latency.overall.p999
    );
    let _ = writeln!(
        output,
        "║   min:  {:>8.1}    max:  {:>8.1}",
        stats.latency.overall.min, stats.latency.overall.max
    );

    let _ = writeln!(
        output,
        "╠══════════════════════════════════════════════════════════════╣"
    );

    // Throughput
    let _ = writeln!(
        output,
        "║ THROUGHPUT                                                   ║"
    );
    let _ = writeln!(
        output,
        "║   Current rate:   {:>10.1} events/sec                   ║",
        stats.throughput.current_rate
    );
    let _ = writeln!(
        output,
        "║   Peak rate:      {:>10.1} events/sec                   ║",
        stats.throughput.peak_rate
    );

    let _ = writeln!(
        output,
        "╠══════════════════════════════════════════════════════════════╣"
    );

    // Queue stats
    let _ = writeln!(
        output,
        "║ QUEUE                                                        ║"
    );
    let _ = writeln!(
        output,
        "║   Current depth:    {:>6}",
        stats.queue.current_depth
    );
    let _ = writeln!(
        output,
        "║   Max depth:        {:>6}",
        stats.queue.max_depth
    );
    let _ = writeln!(
        output,
        "║   Avg depth:        {:>6.1}",
        stats.queue.avg_depth
    );
    let _ = writeln!(
        output,
        "║   Saturation:       {:>6.1}%                                ║",
        stats.queue.saturation_percentage
    );

    let _ = writeln!(
        output,
        "╚══════════════════════════════════════════════════════════════╝"
    );

    output
}

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

    #[test]
    fn test_metrics_collector_creation() {
        let collector = MetricsCollector::new(MetricsConfig::default());
        let stats = collector.snapshot();

        assert_eq!(stats.basic.total_events, 0);
        assert_eq!(stats.latency.high_priority.count, 0);
    }

    #[test]
    fn test_event_recording() {
        let collector = MetricsCollector::new(MetricsConfig::default());

        // Record some events
        collector.record_event(Priority::High, Duration::from_micros(100), Some("test"));
        collector.record_event(Priority::Normal, Duration::from_micros(200), Some("test"));
        collector.record_event(Priority::Low, Duration::from_micros(300), None);

        let stats = collector.snapshot();

        assert_eq!(stats.basic.total_events, 3);
        assert_eq!(stats.basic.high_priority_events, 1);
        assert_eq!(stats.basic.normal_priority_events, 1);
        assert_eq!(stats.basic.low_priority_events, 1);

        // Check that latencies were recorded
        assert!(stats.latency.high_priority.count > 0);
        assert!(stats.latency.high_priority.mean > 0.0);
    }

    #[test]
    fn test_percentile_calculation() {
        let collector = MetricsCollector::new(MetricsConfig::default());

        // Record many events with known latencies
        for i in 1..=100 {
            collector.record_event(Priority::High, Duration::from_micros(i * 10), None);
        }

        let stats = collector.snapshot();
        let percentiles = &stats.latency.high_priority;

        // P50 should be around 500μs (50th value * 10)
        assert!(percentiles.p50 >= 490 && percentiles.p50 <= 510);

        // P99 should be around 990μs (99th value * 10)
        assert!(percentiles.p99 >= 980 && percentiles.p99 <= 1000);

        assert_eq!(percentiles.count, 100);
    }

    #[test]
    fn test_export_formats() {
        let collector = MetricsCollector::new(MetricsConfig::default());
        collector.record_event(Priority::High, Duration::from_micros(100), None);

        let stats = collector.snapshot();

        // Test JSON export
        let json = stats.export(ExportFormat::Json);
        assert!(json.contains("\"total_events\": 1"));

        // Test Prometheus export
        let prometheus = stats.export(ExportFormat::Prometheus);
        assert!(prometheus.contains("hojicha_events_total"));
        assert!(prometheus.contains("hojicha_event_latency_microseconds"));

        // Test plain text export
        let text = stats.export(ExportFormat::PlainText);
        assert!(text.contains("Total Events: 1"));
    }

    #[test]
    fn test_metrics_reset() {
        let collector = MetricsCollector::new(MetricsConfig::default());

        // Record some events
        for _ in 0..10 {
            collector.record_event(Priority::High, Duration::from_micros(100), None);
        }

        let stats = collector.snapshot();
        assert_eq!(stats.basic.total_events, 10);

        // Reset and verify
        collector.reset();
        let stats = collector.snapshot();
        assert_eq!(stats.basic.total_events, 0);
        assert_eq!(stats.latency.high_priority.count, 0);
    }
}