pjson-rs 0.5.2

Priority JSON Streaming Protocol - high-performance priority-based JSON streaming (requires nightly Rust)
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
//! Metrics collection and monitoring implementation

use parking_lot::RwLock;
use std::{
    collections::HashMap,
    sync::Arc,
    time::{Duration, Instant},
};

use crate::domain::{
    DomainResult,
    ports::{MetricsCollectorGat, SessionMetricsGat},
    value_objects::{SessionId, StreamId},
};

/// Performance metrics for PJS operations
#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
    pub active_sessions: usize,
    pub total_sessions_created: u64,
    pub active_streams: usize,
    pub total_streams_created: u64,
    pub average_frame_processing_time: Duration,
    pub bytes_streamed: u64,
    pub frames_processed: u64,
    pub error_count: u64,
}

impl Default for PerformanceMetrics {
    fn default() -> Self {
        Self {
            active_sessions: 0,
            total_sessions_created: 0,
            active_streams: 0,
            total_streams_created: 0,
            average_frame_processing_time: Duration::from_millis(0),
            bytes_streamed: 0,
            frames_processed: 0,
            error_count: 0,
        }
    }
}

/// In-memory metrics collector with time-series data
#[derive(Debug, Clone)]
pub struct InMemoryMetricsCollector {
    metrics: Arc<RwLock<PerformanceMetrics>>,
    session_metrics: Arc<RwLock<HashMap<SessionId, SessionMetrics>>>,
    stream_metrics: Arc<RwLock<HashMap<StreamId, StreamMetrics>>>,
    time_series: Arc<RwLock<Vec<TimestampedMetrics>>>,
    max_time_series_entries: usize,
}

#[derive(Debug, Clone)]
pub struct SessionMetrics {
    pub session_id: SessionId,
    pub created_at: Instant,
    pub last_activity: Instant,
    pub streams_created: usize,
    pub bytes_processed: u64,
    pub frames_sent: u64,
    pub errors: u64,
    pub metadata: HashMap<String, String>,
}

#[derive(Debug, Clone)]
pub struct StreamMetrics {
    pub stream_id: StreamId,
    pub session_id: SessionId,
    pub created_at: Instant,
    pub completed_at: Option<Instant>,
    pub frames_generated: u64,
    pub bytes_sent: u64,
    pub average_priority: f64,
    pub processing_times: Vec<Duration>,
}

#[derive(Debug, Clone)]
pub struct TimestampedMetrics {
    pub timestamp: Instant,
    pub metrics: PerformanceMetrics,
}

impl InMemoryMetricsCollector {
    pub fn new() -> Self {
        Self {
            metrics: Arc::new(RwLock::new(PerformanceMetrics::default())),
            session_metrics: Arc::new(RwLock::new(HashMap::new())),
            stream_metrics: Arc::new(RwLock::new(HashMap::new())),
            time_series: Arc::new(RwLock::new(Vec::new())),
            max_time_series_entries: 1000,
        }
    }

    /// Get current performance snapshot
    pub fn get_performance_snapshot(&self) -> PerformanceMetrics {
        self.metrics.read().clone()
    }

    /// Get metrics for specific session
    pub fn get_session_metrics(&self, session_id: SessionId) -> Option<SessionMetrics> {
        self.session_metrics.read().get(&session_id).cloned()
    }

    /// Get metrics for specific stream
    pub fn get_stream_metrics(&self, stream_id: StreamId) -> Option<StreamMetrics> {
        self.stream_metrics.read().get(&stream_id).cloned()
    }

    /// Get time series data for the last N minutes
    pub fn get_time_series(&self, minutes: u32) -> Vec<TimestampedMetrics> {
        let duration = Duration::from_secs(minutes as u64 * 60);
        let now = Instant::now();

        // Use checked_sub to handle Windows overflow when duration exceeds program uptime
        if let Some(cutoff) = now.checked_sub(duration) {
            self.time_series
                .read()
                .iter()
                .filter(|entry| entry.timestamp > cutoff)
                .cloned()
                .collect()
        } else {
            // Duration exceeds program uptime, return all entries
            self.time_series.read().clone()
        }
    }

    /// Clear all metrics (for testing)
    pub fn clear(&self) {
        *self.metrics.write() = PerformanceMetrics::default();
        self.session_metrics.write().clear();
        self.stream_metrics.write().clear();
        self.time_series.write().clear();
    }

    /// Export metrics to Prometheus format
    pub fn export_prometheus(&self) -> String {
        let metrics = self.metrics.read();
        format!(
            r#"# HELP pjs_active_sessions Number of active PJS sessions
# TYPE pjs_active_sessions gauge
pjs_active_sessions {{}} {}

# HELP pjs_total_sessions_created Total number of sessions created
# TYPE pjs_total_sessions_created counter
pjs_total_sessions_created {{}} {}

# HELP pjs_active_streams Number of active streams
# TYPE pjs_active_streams gauge
pjs_active_streams {{}} {}

# HELP pjs_frames_processed_total Total frames processed
# TYPE pjs_frames_processed_total counter
pjs_frames_processed_total {{}} {}

# HELP pjs_bytes_streamed_total Total bytes streamed
# TYPE pjs_bytes_streamed_total counter
pjs_bytes_streamed_total {{}} {}

# HELP pjs_errors_total Total number of errors
# TYPE pjs_errors_total counter
pjs_errors_total {{}} {}

# HELP pjs_frame_processing_time_ms Average frame processing time in milliseconds
# TYPE pjs_frame_processing_time_ms gauge
pjs_frame_processing_time_ms {{}} {}
"#,
            metrics.active_sessions,
            metrics.total_sessions_created,
            metrics.active_streams,
            metrics.frames_processed,
            metrics.bytes_streamed,
            metrics.error_count,
            metrics.average_frame_processing_time.as_millis()
        )
    }
}

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

impl MetricsCollectorGat for InMemoryMetricsCollector {
    type IncrementCounterFuture<'a>
        = impl std::future::Future<Output = DomainResult<()>> + Send + 'a
    where
        Self: 'a;

    type SetGaugeFuture<'a>
        = impl std::future::Future<Output = DomainResult<()>> + Send + 'a
    where
        Self: 'a;

    type RecordTimingFuture<'a>
        = impl std::future::Future<Output = DomainResult<()>> + Send + 'a
    where
        Self: 'a;

    fn increment_counter<'a>(
        &'a self,
        name: &'a str,
        value: u64,
        _tags: HashMap<String, String>,
    ) -> Self::IncrementCounterFuture<'a> {
        async move {
            let mut metrics = self.metrics.write();

            match name {
                "sessions_created" => metrics.total_sessions_created += value,
                "streams_created" => metrics.total_streams_created += value,
                "frames_processed" => metrics.frames_processed += value,
                "bytes_streamed" => metrics.bytes_streamed += value,
                "errors" => metrics.error_count += value,
                _ => {} // Unknown metric
            }

            Ok(())
        }
    }

    fn set_gauge<'a>(
        &'a self,
        name: &'a str,
        value: f64,
        _tags: HashMap<String, String>,
    ) -> Self::SetGaugeFuture<'a> {
        async move {
            let mut metrics = self.metrics.write();

            match name {
                "active_sessions" => metrics.active_sessions = value as usize,
                "active_streams" => metrics.active_streams = value as usize,
                "average_frame_processing_time_ms" => {
                    metrics.average_frame_processing_time = Duration::from_millis(value as u64);
                }
                _ => {} // Unknown metric
            }

            Ok(())
        }
    }

    fn record_timing<'a>(
        &'a self,
        name: &'a str,
        duration: Duration,
        tags: HashMap<String, String>,
    ) -> Self::RecordTimingFuture<'a> {
        async move {
            // Update average frame processing time
            if name == "frame_processing" {
                let mut metrics = self.metrics.write();
                let current_avg = metrics.average_frame_processing_time.as_millis() as f64;
                let new_duration = duration.as_millis() as f64;

                // Simple moving average calculation
                let processed = metrics.frames_processed as f64;
                if processed > 0.0 {
                    let new_avg = (current_avg * processed + new_duration) / (processed + 1.0);
                    metrics.average_frame_processing_time = Duration::from_millis(new_avg as u64);
                } else {
                    metrics.average_frame_processing_time = duration;
                }
            }

            // Record timing for specific session or stream
            if let Some(session_id_str) = tags.get("session_id")
                && let Ok(session_id) = SessionId::from_string(session_id_str)
            {
                let mut session_metrics = self.session_metrics.write();
                if let Some(session_metric) = session_metrics.get_mut(&session_id) {
                    session_metric.last_activity = Instant::now();
                }
            }

            if let Some(stream_id_str) = tags.get("stream_id")
                && let Ok(stream_id) = StreamId::from_string(stream_id_str)
            {
                let mut stream_metrics = self.stream_metrics.write();
                if let Some(stream_metric) = stream_metrics.get_mut(&stream_id) {
                    stream_metric.processing_times.push(duration);
                }
            }

            Ok(())
        }
    }
}

impl SessionMetricsGat for InMemoryMetricsCollector {
    type RecordSessionCreatedFuture<'a>
        = impl std::future::Future<Output = DomainResult<()>> + Send + 'a
    where
        Self: 'a;

    type RecordSessionEndedFuture<'a>
        = impl std::future::Future<Output = DomainResult<()>> + Send + 'a
    where
        Self: 'a;

    type RecordStreamCreatedFuture<'a>
        = impl std::future::Future<Output = DomainResult<()>> + Send + 'a
    where
        Self: 'a;

    type RecordStreamCompletedFuture<'a>
        = impl std::future::Future<Output = DomainResult<()>> + Send + 'a
    where
        Self: 'a;

    fn record_session_created(
        &self,
        session_id: SessionId,
        metadata: HashMap<String, String>,
    ) -> Self::RecordSessionCreatedFuture<'_> {
        async move {
            // Update global metrics
            {
                let mut metrics = self.metrics.write();
                metrics.total_sessions_created += 1;
                metrics.active_sessions += 1;
            }

            // Create session metrics
            let session_metric = SessionMetrics {
                session_id,
                created_at: Instant::now(),
                last_activity: Instant::now(),
                streams_created: 0,
                bytes_processed: 0,
                frames_sent: 0,
                errors: 0,
                metadata,
            };

            self.session_metrics
                .write()
                .insert(session_id, session_metric);

            // Record in time series
            self.record_time_series_snapshot().await?;

            Ok(())
        }
    }

    fn record_session_ended(&self, session_id: SessionId) -> Self::RecordSessionEndedFuture<'_> {
        async move {
            // Update global metrics
            {
                let mut metrics = self.metrics.write();
                if metrics.active_sessions > 0 {
                    metrics.active_sessions -= 1;
                }
            }

            // Remove session metrics
            self.session_metrics.write().remove(&session_id);

            // Record in time series
            self.record_time_series_snapshot().await?;

            Ok(())
        }
    }

    fn record_stream_created(
        &self,
        stream_id: StreamId,
        session_id: SessionId,
    ) -> Self::RecordStreamCreatedFuture<'_> {
        async move {
            // Update global metrics
            {
                let mut metrics = self.metrics.write();
                metrics.total_streams_created += 1;
                metrics.active_streams += 1;
            }

            // Update session metrics
            {
                let mut session_metrics = self.session_metrics.write();
                if let Some(session_metric) = session_metrics.get_mut(&session_id) {
                    session_metric.streams_created += 1;
                    session_metric.last_activity = Instant::now();
                }
            }

            // Create stream metrics
            let stream_metric = StreamMetrics {
                stream_id,
                session_id,
                created_at: Instant::now(),
                completed_at: None,
                frames_generated: 0,
                bytes_sent: 0,
                average_priority: 0.0,
                processing_times: Vec::new(),
            };

            self.stream_metrics.write().insert(stream_id, stream_metric);

            Ok(())
        }
    }

    fn record_stream_completed(
        &self,
        stream_id: StreamId,
    ) -> Self::RecordStreamCompletedFuture<'_> {
        async move {
            // Update global metrics
            {
                let mut metrics = self.metrics.write();
                if metrics.active_streams > 0 {
                    metrics.active_streams -= 1;
                }
            }

            // Update stream metrics
            {
                let mut stream_metrics = self.stream_metrics.write();
                if let Some(stream_metric) = stream_metrics.get_mut(&stream_id) {
                    stream_metric.completed_at = Some(Instant::now());
                }
            }

            Ok(())
        }
    }
}

impl InMemoryMetricsCollector {
    async fn record_time_series_snapshot(&self) -> DomainResult<()> {
        let snapshot = TimestampedMetrics {
            timestamp: Instant::now(),
            metrics: self.metrics.read().clone(),
        };

        let mut time_series = self.time_series.write();
        time_series.push(snapshot);

        // Keep only recent entries
        if time_series.len() > self.max_time_series_entries {
            time_series.drain(..100); // Remove oldest 100 entries
        }

        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_in_memory_metrics_collector() {
        let collector = InMemoryMetricsCollector::new();
        let session_id = SessionId::new();

        // Test session creation
        collector
            .record_session_created(session_id, HashMap::new())
            .await
            .unwrap();

        let snapshot = collector.get_performance_snapshot();
        assert_eq!(snapshot.active_sessions, 1);
        assert_eq!(snapshot.total_sessions_created, 1);

        // Test session metrics
        let session_metrics = collector.get_session_metrics(session_id).unwrap();
        assert_eq!(session_metrics.session_id, session_id);

        // Test session ending
        collector.record_session_ended(session_id).await.unwrap();
        let snapshot = collector.get_performance_snapshot();
        assert_eq!(snapshot.active_sessions, 0);
    }

    #[tokio::test]
    async fn test_prometheus_export() {
        let collector = InMemoryMetricsCollector::new();

        collector
            .increment_counter("frames_processed", 100, HashMap::new())
            .await
            .unwrap();
        collector
            .set_gauge("active_sessions", 5.0, HashMap::new())
            .await
            .unwrap();

        let prometheus_output = collector.export_prometheus();
        assert!(prometheus_output.contains("pjs_frames_processed_total {} 100"));
        assert!(prometheus_output.contains("pjs_active_sessions {} 5"));
    }

    #[tokio::test]
    async fn test_record_stream_created() {
        let collector = InMemoryMetricsCollector::new();
        let session_id = SessionId::new();
        let stream_id = StreamId::new();

        // Create session first
        collector
            .record_session_created(session_id, HashMap::new())
            .await
            .unwrap();

        // Record stream creation
        collector
            .record_stream_created(stream_id, session_id)
            .await
            .unwrap();

        let snapshot = collector.get_performance_snapshot();
        assert_eq!(snapshot.active_streams, 1);
        assert_eq!(snapshot.total_streams_created, 1);

        // Verify session metrics updated
        let session_metrics = collector.get_session_metrics(session_id).unwrap();
        assert_eq!(session_metrics.streams_created, 1);

        // Verify stream metrics created
        let stream_metrics = collector.get_stream_metrics(stream_id).unwrap();
        assert_eq!(stream_metrics.stream_id, stream_id);
        assert_eq!(stream_metrics.session_id, session_id);
    }

    #[tokio::test]
    async fn test_record_stream_completed() {
        let collector = InMemoryMetricsCollector::new();
        let session_id = SessionId::new();
        let stream_id = StreamId::new();

        // Setup: create session and stream
        collector
            .record_session_created(session_id, HashMap::new())
            .await
            .unwrap();
        collector
            .record_stream_created(stream_id, session_id)
            .await
            .unwrap();

        assert_eq!(collector.get_performance_snapshot().active_streams, 1);

        // Complete the stream
        collector.record_stream_completed(stream_id).await.unwrap();

        let snapshot = collector.get_performance_snapshot();
        assert_eq!(snapshot.active_streams, 0);

        // Verify stream metrics marked as completed
        let stream_metrics = collector.get_stream_metrics(stream_id).unwrap();
        assert!(stream_metrics.completed_at.is_some());
    }

    #[tokio::test]
    async fn test_record_timing_with_session_tags() {
        let collector = InMemoryMetricsCollector::new();
        let session_id = SessionId::new();

        // Create session
        collector
            .record_session_created(session_id, HashMap::new())
            .await
            .unwrap();

        let session_metrics_before = collector.get_session_metrics(session_id).unwrap();
        let last_activity_before = session_metrics_before.last_activity;

        // Wait briefly to ensure time difference
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;

        // Record timing with session tag
        let mut tags = HashMap::new();
        tags.insert("session_id".to_string(), session_id.to_string());

        collector
            .record_timing("operation", std::time::Duration::from_millis(50), tags)
            .await
            .unwrap();

        // Verify last_activity was updated
        let session_metrics_after = collector.get_session_metrics(session_id).unwrap();
        assert!(session_metrics_after.last_activity > last_activity_before);
    }

    #[tokio::test]
    async fn test_record_timing_with_stream_tags() {
        let collector = InMemoryMetricsCollector::new();
        let session_id = SessionId::new();
        let stream_id = StreamId::new();

        // Setup: create session and stream
        collector
            .record_session_created(session_id, HashMap::new())
            .await
            .unwrap();
        collector
            .record_stream_created(stream_id, session_id)
            .await
            .unwrap();

        // Record timing with stream tag
        let mut tags = HashMap::new();
        tags.insert("stream_id".to_string(), stream_id.to_string());

        collector
            .record_timing(
                "stream_operation",
                std::time::Duration::from_millis(75),
                tags,
            )
            .await
            .unwrap();

        // Verify processing_times was updated
        let stream_metrics = collector.get_stream_metrics(stream_id).unwrap();
        assert_eq!(stream_metrics.processing_times.len(), 1);
        assert_eq!(
            stream_metrics.processing_times[0],
            std::time::Duration::from_millis(75)
        );
    }

    #[tokio::test]
    async fn test_time_series_snapshot_boundary() {
        let collector = InMemoryMetricsCollector::new();

        // Record multiple snapshots
        for i in 0..5 {
            let session_id = SessionId::new();
            let mut metadata = HashMap::new();
            metadata.insert("iteration".to_string(), i.to_string());

            collector
                .record_session_created(session_id, metadata)
                .await
                .unwrap();
        }

        // Verify time series contains snapshots
        let time_series = collector.get_time_series(60);
        assert!(time_series.len() >= 5);

        // Verify each snapshot has increasing session counts
        for (i, snapshot) in time_series.iter().enumerate() {
            assert!(snapshot.metrics.total_sessions_created >= (i + 1) as u64);
        }
    }
}