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
//! Service responsible for use case optimization strategies
//!
//! This service focuses on applying different optimization strategies
//! based on specific streaming use cases and requirements.

use crate::{application::ApplicationResult, domain::value_objects::Priority};

/// Service for optimization strategies and use case handling
#[derive(Debug)]
pub struct OptimizationService {
    custom_strategies: std::collections::HashMap<String, OptimizationStrategy>,
}

/// Supported streaming use cases
#[derive(Debug, Clone, PartialEq)]
pub enum StreamingUseCase {
    /// Real-time dashboard updates requiring low latency
    RealTimeDashboard,
    /// Bulk data transfer prioritizing throughput
    BulkDataTransfer,
    /// Mobile application with network constraints
    MobileApp,
    /// Progressive web application balancing UX and performance
    ProgressiveWebApp,
    /// IoT device streaming with power constraints
    IoTDevice,
    /// Live streaming with audience interaction
    LiveStreaming,
    /// Custom use case with specific requirements
    Custom(String),
}

/// Optimization strategy configuration
#[derive(Debug, Clone)]
pub struct OptimizationStrategy {
    pub priority_threshold: Priority,
    pub max_frame_size: usize,
    pub batch_size: usize,
    pub compression_enabled: bool,
    pub adaptive_quality: bool,
    pub description: String,
    pub target_latency_ms: f64,
    pub target_throughput_mbps: f64,
}

/// Result of use case optimization
#[derive(Debug, Clone)]
pub struct UseCaseOptimizationResult {
    pub use_case: StreamingUseCase,
    pub strategy_applied: OptimizationStrategy,
    pub frames_generated: Vec<crate::domain::entities::Frame>,
    pub optimization_metrics: OptimizationMetrics,
}

/// Metrics about the optimization performance
#[derive(Debug, Clone)]
pub struct OptimizationMetrics {
    pub efficiency_score: f64,
    pub latency_improvement: f64,
    pub throughput_improvement: f64,
    pub resource_utilization: f64,
    pub quality_score: f64,
}

impl OptimizationService {
    pub fn new() -> Self {
        Self {
            custom_strategies: std::collections::HashMap::new(),
        }
    }

    /// Get optimization strategy for a specific use case
    pub fn get_strategy_for_use_case(
        &self,
        use_case: &StreamingUseCase,
    ) -> ApplicationResult<OptimizationStrategy> {
        match use_case {
            StreamingUseCase::RealTimeDashboard => Ok(Self::create_realtime_dashboard_strategy()),
            StreamingUseCase::BulkDataTransfer => Ok(Self::create_bulk_transfer_strategy()),
            StreamingUseCase::MobileApp => Ok(Self::create_mobile_app_strategy()),
            StreamingUseCase::ProgressiveWebApp => Ok(Self::create_pwa_strategy()),
            StreamingUseCase::IoTDevice => Ok(Self::create_iot_device_strategy()),
            StreamingUseCase::LiveStreaming => Ok(Self::create_live_streaming_strategy()),
            StreamingUseCase::Custom(name) => {
                self.custom_strategies.get(name).cloned().ok_or_else(|| {
                    crate::application::ApplicationError::Logic(format!(
                        "Custom strategy '{name}' not found"
                    ))
                })
            }
        }
    }

    /// Register a custom optimization strategy
    pub fn register_custom_strategy(
        &mut self,
        name: String,
        strategy: OptimizationStrategy,
    ) -> ApplicationResult<()> {
        self.custom_strategies.insert(name, strategy);
        Ok(())
    }

    /// Optimize strategy based on performance context
    pub fn optimize_strategy_for_context(
        &self,
        base_strategy: OptimizationStrategy,
        context: &crate::application::services::prioritization_service::PerformanceContext,
    ) -> ApplicationResult<OptimizationStrategy> {
        let mut optimized = base_strategy;

        // Adjust priority threshold based on performance
        if context.error_rate > 0.05 {
            optimized.priority_threshold = optimized.priority_threshold.increase_by(20);
        } else if context.error_rate < 0.01 && context.average_latency_ms < 100.0 {
            optimized.priority_threshold = optimized.priority_threshold.decrease_by(10);
        }

        // Adjust batch size based on latency and bandwidth
        if context.average_latency_ms > 1000.0 {
            optimized.batch_size = (optimized.batch_size as f64 * 0.7) as usize;
        } else if context.average_latency_ms < 100.0 && context.available_bandwidth_mbps > 10.0 {
            optimized.batch_size = (optimized.batch_size as f64 * 1.3) as usize;
        }

        // Adjust frame size based on bandwidth
        if context.available_bandwidth_mbps < 2.0 {
            optimized.max_frame_size = (optimized.max_frame_size as f64 * 0.8) as usize;
        } else if context.available_bandwidth_mbps > 20.0 {
            optimized.max_frame_size = (optimized.max_frame_size as f64 * 1.2) as usize;
        }

        // Enable compression for low bandwidth
        if context.available_bandwidth_mbps < 5.0 {
            optimized.compression_enabled = true;
        }

        // Adjust adaptive quality based on CPU usage
        if context.cpu_usage > 0.8 {
            optimized.adaptive_quality = false; // Disable to reduce CPU load
        }

        Ok(optimized)
    }

    /// Calculate optimization metrics for a given strategy and results
    pub fn calculate_optimization_metrics(
        &self,
        strategy: &OptimizationStrategy,
        frames: &[crate::domain::entities::Frame],
        context: &crate::application::services::prioritization_service::PerformanceContext,
    ) -> ApplicationResult<OptimizationMetrics> {
        // Calculate efficiency score based on frame priority distribution
        let efficiency_score = self.calculate_efficiency_score(frames, strategy);

        // Estimate latency improvement
        let latency_improvement = self.estimate_latency_improvement(strategy, context);

        // Estimate throughput improvement
        let throughput_improvement = self.estimate_throughput_improvement(strategy, context);

        // Calculate resource utilization score
        let resource_utilization = self.calculate_resource_utilization(strategy, context);

        // Calculate quality score based on frame completeness
        let quality_score = self.calculate_quality_score(frames, strategy);

        Ok(OptimizationMetrics {
            efficiency_score,
            latency_improvement,
            throughput_improvement,
            resource_utilization,
            quality_score,
        })
    }

    /// Recommend strategy adjustments based on analysis
    pub fn recommend_strategy_adjustments(
        &self,
        current_strategy: &OptimizationStrategy,
        performance_report: &crate::application::services::performance_analysis_service::PerformanceAnalysisReport,
    ) -> ApplicationResult<Vec<StrategyAdjustmentRecommendation>> {
        let mut recommendations = Vec::new();

        // Analyze latency performance
        if performance_report.latency_analysis.average > current_strategy.target_latency_ms * 1.5 {
            recommendations.push(StrategyAdjustmentRecommendation {
                adjustment_type: AdjustmentType::PriorityIncrease,
                description: "Increase priority threshold to reduce latency".to_string(),
                expected_impact: "Reduce latency by 20-40%".to_string(),
                confidence: 0.8,
                urgency: AdjustmentUrgency::High,
            });
        }

        // Analyze throughput performance
        if performance_report.throughput_analysis.average_mbps
            < current_strategy.target_throughput_mbps * 0.7
        {
            recommendations.push(StrategyAdjustmentRecommendation {
                adjustment_type: AdjustmentType::BatchSizeIncrease,
                description: "Increase batch size to improve throughput".to_string(),
                expected_impact: "Improve throughput by 15-30%".to_string(),
                confidence: 0.7,
                urgency: AdjustmentUrgency::Medium,
            });
        }

        // Analyze error rate
        if performance_report.error_analysis.error_rate > 0.05 {
            recommendations.push(StrategyAdjustmentRecommendation {
                adjustment_type: AdjustmentType::QualityReduction,
                description: "Enable adaptive quality to reduce errors".to_string(),
                expected_impact: "Reduce error rate by 30-50%".to_string(),
                confidence: 0.9,
                urgency: AdjustmentUrgency::High,
            });
        }

        // Analyze resource usage
        if performance_report.resource_analysis.current_cpu_usage > 0.8 {
            recommendations.push(StrategyAdjustmentRecommendation {
                adjustment_type: AdjustmentType::CompressionDisable,
                description: "Disable compression to reduce CPU load".to_string(),
                expected_impact: "Reduce CPU usage by 10-20%".to_string(),
                confidence: 0.8,
                urgency: AdjustmentUrgency::Medium,
            });
        }

        Ok(recommendations)
    }

    // Private implementation methods for creating specific strategies

    fn create_realtime_dashboard_strategy() -> OptimizationStrategy {
        OptimizationStrategy {
            priority_threshold: Priority::HIGH,
            max_frame_size: 16 * 1024, // 16KB for low latency
            batch_size: 5,
            compression_enabled: false, // Avoid compression latency
            adaptive_quality: true,
            description: "Optimized for real-time dashboard updates with minimal latency"
                .to_string(),
            target_latency_ms: 100.0,
            target_throughput_mbps: 5.0,
        }
    }

    fn create_bulk_transfer_strategy() -> OptimizationStrategy {
        OptimizationStrategy {
            priority_threshold: Priority::MEDIUM,
            max_frame_size: 256 * 1024, // 256KB for throughput
            batch_size: 20,
            compression_enabled: true,
            adaptive_quality: false, // Consistent quality for bulk
            description: "Optimized for bulk data transfer with maximum throughput".to_string(),
            target_latency_ms: 1000.0,
            target_throughput_mbps: 50.0,
        }
    }

    fn create_mobile_app_strategy() -> OptimizationStrategy {
        OptimizationStrategy {
            priority_threshold: Priority::HIGH,
            max_frame_size: 8 * 1024, // 8KB for mobile networks
            batch_size: 3,
            compression_enabled: true, // Important for mobile data usage
            adaptive_quality: true,
            description: "Optimized for mobile network constraints and battery life".to_string(),
            target_latency_ms: 300.0,
            target_throughput_mbps: 2.0,
        }
    }

    fn create_pwa_strategy() -> OptimizationStrategy {
        OptimizationStrategy {
            priority_threshold: Priority::CRITICAL,
            max_frame_size: 32 * 1024, // 32KB balanced
            batch_size: 8,
            compression_enabled: true,
            adaptive_quality: true,
            description: "Optimized for progressive web app user experience".to_string(),
            target_latency_ms: 200.0,
            target_throughput_mbps: 10.0,
        }
    }

    fn create_iot_device_strategy() -> OptimizationStrategy {
        OptimizationStrategy {
            priority_threshold: Priority::CRITICAL,
            max_frame_size: 4 * 1024, // 4KB for constrained devices
            batch_size: 2,
            compression_enabled: true, // Critical for bandwidth savings
            adaptive_quality: false,   // Consistent behavior
            description: "Optimized for IoT devices with power and bandwidth constraints"
                .to_string(),
            target_latency_ms: 500.0,
            target_throughput_mbps: 1.0,
        }
    }

    fn create_live_streaming_strategy() -> OptimizationStrategy {
        OptimizationStrategy {
            priority_threshold: Priority::HIGH,
            max_frame_size: 64 * 1024, // 64KB for video frames
            batch_size: 10,
            compression_enabled: true,
            adaptive_quality: true, // Important for varying network conditions
            description: "Optimized for live streaming with audience interaction".to_string(),
            target_latency_ms: 500.0,
            target_throughput_mbps: 20.0,
        }
    }

    // Private calculation methods

    fn calculate_efficiency_score(
        &self,
        frames: &[crate::domain::entities::Frame],
        strategy: &OptimizationStrategy,
    ) -> f64 {
        if frames.is_empty() {
            return 0.0;
        }

        // Calculate how well the frames align with the strategy
        let target_priority = strategy.priority_threshold.value();
        let mut efficiency_sum = 0.0;

        for frame in frames {
            let priority = frame.priority();
            // Score based on how close the frame priority is to target
            let priority_diff = (priority.value() as i32 - target_priority as i32).abs() as f64;
            let priority_score = 1.0 - (priority_diff / 255.0).min(1.0);

            // Score based on frame size efficiency
            let size_score = if frame.estimated_size() <= strategy.max_frame_size {
                1.0
            } else {
                strategy.max_frame_size as f64 / frame.estimated_size() as f64
            };

            efficiency_sum += (priority_score + size_score) / 2.0;
        }

        (efficiency_sum / frames.len() as f64).min(1.0)
    }

    fn estimate_latency_improvement(
        &self,
        strategy: &OptimizationStrategy,
        context: &crate::application::services::prioritization_service::PerformanceContext,
    ) -> f64 {
        // Estimate improvement based on strategy parameters
        let mut improvement = 0.0;

        // Higher priority threshold should reduce latency
        let priority_factor = strategy.priority_threshold.value() as f64 / 255.0;
        improvement += priority_factor * 0.3;

        // Smaller batch sizes reduce latency
        let batch_factor = 1.0 - (strategy.batch_size as f64 / 50.0).min(1.0);
        improvement += batch_factor * 0.2;

        // Smaller frame sizes reduce processing time
        let frame_size_factor = 1.0 - (strategy.max_frame_size as f64 / (1024.0 * 1024.0)).min(1.0);
        improvement += frame_size_factor * 0.2;

        // Consider current performance
        if context.average_latency_ms > strategy.target_latency_ms {
            improvement *= 1.5; // Higher improvement potential
        }

        improvement.min(1.0)
    }

    fn estimate_throughput_improvement(
        &self,
        strategy: &OptimizationStrategy,
        context: &crate::application::services::prioritization_service::PerformanceContext,
    ) -> f64 {
        let mut improvement = 0.0;

        // Larger batch sizes improve throughput
        let batch_factor = (strategy.batch_size as f64 / 50.0).min(1.0);
        improvement += batch_factor * 0.4;

        // Compression can improve effective throughput
        if strategy.compression_enabled {
            improvement += 0.2;
        }

        // Larger frame sizes can improve efficiency
        let frame_size_factor = (strategy.max_frame_size as f64 / (1024.0 * 1024.0)).min(1.0);
        improvement += frame_size_factor * 0.3;

        // Consider current performance
        if context.available_bandwidth_mbps < strategy.target_throughput_mbps {
            improvement *= 1.3; // Higher improvement potential
        }

        improvement.min(1.0)
    }

    fn calculate_resource_utilization(
        &self,
        strategy: &OptimizationStrategy,
        context: &crate::application::services::prioritization_service::PerformanceContext,
    ) -> f64 {
        let mut utilization = context.cpu_usage;

        // Compression increases CPU usage
        if strategy.compression_enabled {
            utilization += 0.1;
        }

        // Adaptive quality increases CPU usage
        if strategy.adaptive_quality {
            utilization += 0.05;
        }

        // Larger batch sizes reduce relative overhead
        let batch_efficiency = (strategy.batch_size as f64 / 20.0).min(1.0);
        utilization -= batch_efficiency * 0.1;

        utilization.clamp(0.0, 1.0)
    }

    fn calculate_quality_score(
        &self,
        frames: &[crate::domain::entities::Frame],
        strategy: &OptimizationStrategy,
    ) -> f64 {
        if frames.is_empty() {
            return 0.0;
        }

        let mut quality_sum = 0.0;

        for frame in frames {
            let mut frame_quality = 1.0;

            // Penalize frames that are too large
            if frame.estimated_size() > strategy.max_frame_size {
                frame_quality *= 0.8;
            }

            // Reward frames with appropriate priority
            let priority = frame.priority();
            if priority.value() >= strategy.priority_threshold.value() {
                frame_quality *= 1.1;
            }

            quality_sum += frame_quality;
        }

        (quality_sum / frames.len() as f64).min(1.0)
    }
}

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

// Supporting types

#[derive(Debug, Clone)]
pub struct StrategyAdjustmentRecommendation {
    pub adjustment_type: AdjustmentType,
    pub description: String,
    pub expected_impact: String,
    pub confidence: f64,
    pub urgency: AdjustmentUrgency,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AdjustmentType {
    PriorityIncrease,
    PriorityDecrease,
    BatchSizeIncrease,
    BatchSizeDecrease,
    FrameSizeIncrease,
    FrameSizeDecrease,
    CompressionEnable,
    CompressionDisable,
    QualityIncrease,
    QualityReduction,
}

// Use shared AdjustmentUrgency type
use crate::application::shared::AdjustmentUrgency;

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

    #[test]
    fn test_optimization_service_creation() {
        let service = OptimizationService::new();
        assert!(service.custom_strategies.is_empty());
    }

    #[test]
    fn test_strategy_selection_based_on_use_case() {
        let service = OptimizationService::new();

        // Test real-time dashboard strategy selection
        let dashboard_strategy = service
            .get_strategy_for_use_case(&StreamingUseCase::RealTimeDashboard)
            .unwrap();
        assert_eq!(dashboard_strategy.priority_threshold, Priority::HIGH);
        assert_eq!(dashboard_strategy.batch_size, 5);
        assert!(dashboard_strategy.adaptive_quality);

        // Test mobile app strategy selection
        let mobile_strategy = service
            .get_strategy_for_use_case(&StreamingUseCase::MobileApp)
            .unwrap();
        assert_eq!(mobile_strategy.priority_threshold, Priority::HIGH);
        assert!(mobile_strategy.compression_enabled);
        assert_eq!(mobile_strategy.max_frame_size, 8 * 1024);
    }

    #[test]
    fn test_custom_strategy_registration() {
        let mut service = OptimizationService::new();

        let custom_strategy = OptimizationStrategy {
            priority_threshold: Priority::CRITICAL,
            max_frame_size: 8 * 1024,
            batch_size: 3,
            compression_enabled: false,
            adaptive_quality: false,
            description: "Ultra low-latency gaming strategy".to_string(),
            target_latency_ms: 50.0,
            target_throughput_mbps: 30.0,
        };

        service
            .register_custom_strategy("ultra_gaming".to_string(), custom_strategy.clone())
            .unwrap();

        let retrieved = service
            .get_strategy_for_use_case(&StreamingUseCase::Custom("ultra_gaming".to_string()))
            .unwrap();
        assert_eq!(retrieved.priority_threshold, Priority::CRITICAL);
        assert_eq!(retrieved.max_frame_size, 8 * 1024);
        assert_eq!(retrieved.target_latency_ms, 50.0);
    }
}