leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly support
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
//! Data processing pipeline with Polars integration

use polars::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Data processing error types
#[derive(Debug, thiserror::Error)]
pub enum DataError {
    #[error("Polars error: {0}")]
    Polars(#[from] PolarsError),

    #[error("Data format error: {0}")]
    Format(String),

    #[error("Data validation error: {0}")]
    Validation(String),

    #[error("Data source error: {0}")]
    Source(String),

    #[error("Processing error: {0}")]
    Processing(String),
}

/// Data processing strategy selection
#[derive(Debug, Clone)]
pub enum ProcessingStrategy {
    CPU(RayonConfig),
    GPU(ComputeConfig),
    Streaming(StreamConfig),
    Hybrid(HybridConfig),
}

#[derive(Debug, Clone)]
pub struct RayonConfig {
    pub num_threads: Option<usize>,
    pub chunk_size: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct ComputeConfig {
    pub workgroup_size: u32,
    pub memory_budget: usize,
}

#[derive(Debug, Clone)]
pub struct StreamConfig {
    pub buffer_size: usize,
    pub batch_size: usize,
    pub enable_backpressure: bool,
}

#[derive(Debug, Clone)]
pub struct HybridConfig {
    pub cpu_threshold: usize,
    pub gpu_threshold: usize,
    pub cpu_config: RayonConfig,
    pub gpu_config: ComputeConfig,
}

/// Strategy selector for optimal data processing
pub struct StrategySelector {
    benchmarks: HashMap<String, f64>,
    device_capabilities: DeviceCapabilities,
}

impl StrategySelector {
    pub fn new() -> Self {
        Self {
            benchmarks: HashMap::new(),
            device_capabilities: DeviceCapabilities::detect(),
        }
    }

    pub fn select(&self, spec: &DataSpec) -> ProcessingStrategy {
        let data_size = spec.estimated_size();
        let complexity = spec.complexity();
        let is_streaming = spec.is_streaming();

        if is_streaming {
            return ProcessingStrategy::Streaming(StreamConfig {
                buffer_size: 10_000,
                batch_size: 1_000,
                enable_backpressure: true,
            });
        }

        if data_size > 1_000_000 && complexity > 0.7 && self.device_capabilities.gpu_available {
            ProcessingStrategy::GPU(ComputeConfig {
                workgroup_size: 64,
                memory_budget: 100 * 1024 * 1024, // 100MB
            })
        } else {
            ProcessingStrategy::CPU(RayonConfig {
                num_threads: None, // Use all available cores
                chunk_size: Some(10_000),
            })
        }
    }

    pub fn benchmark(&mut self, name: &str, duration: f64) {
        self.benchmarks.insert(name.to_string(), duration);
    }
}

#[derive(Debug, Clone)]
pub struct DeviceCapabilities {
    pub gpu_available: bool,
    pub cpu_cores: usize,
    pub memory_gb: f64,
    pub simd_available: bool,
}

impl DeviceCapabilities {
    pub fn detect() -> Self {
        Self {
            gpu_available: Self::detect_gpu(),
            cpu_cores: num_cpus::get(),
            memory_gb: Self::detect_memory(),
            simd_available: Self::detect_simd(),
        }
    }

    fn detect_gpu() -> bool {
        // Check for WebGPU support
        #[cfg(target_arch = "wasm32")]
        {
            // This would be implemented with web-sys
            false // Placeholder
        }
        #[cfg(not(target_arch = "wasm32"))]
        {
            // Check for native GPU support
            false // Placeholder
        }
    }

    fn detect_memory() -> f64 {
        // Get available memory in GB
        8.0 // Placeholder
    }

    fn detect_simd() -> bool {
        if cfg!(target_feature = "simd128") {
            true
        } else {
            false
        }
    }
}

/// Data specification for processing
#[derive(Debug, Clone)]
pub struct DataSpec {
    pub source: DataSource,
    pub transforms: Vec<DataTransform>,
    pub filters: Vec<Filter>,
    pub aggregations: Vec<Aggregation>,
    pub output_format: OutputFormat,
}

impl DataSpec {
    pub fn estimated_size(&self) -> usize {
        // Estimate data size based on source and transforms
        match &self.source {
            DataSource::DataFrame(df) => df.height(),
            DataSource::Url { .. } => 100_000,     // Estimate
            DataSource::Query { .. } => 1_000_000, // Estimate
            DataSource::Stream { .. } => 10_000,   // Streaming estimate
        }
    }

    pub fn complexity(&self) -> f64 {
        let base_complexity = 1.0;
        let transform_complexity = self.transforms.len() as f64 * 0.5;
        let filter_complexity = self.filters.len() as f64 * 0.3;
        let aggregation_complexity = self.aggregations.len() as f64 * 1.0;

        base_complexity + transform_complexity + filter_complexity + aggregation_complexity
    }

    pub fn is_streaming(&self) -> bool {
        matches!(self.source, DataSource::Stream { .. })
    }

    pub fn hash(&self) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        self.source.hash(&mut hasher);
        self.transforms.hash(&mut hasher);
        self.filters.hash(&mut hasher);
        self.aggregations.hash(&mut hasher);
        hasher.finish()
    }
}

#[derive(Debug, Clone)]
pub enum DataSource {
    DataFrame(crate::DataFrame),
    Url { url: String, format: DataFormat },
    Query { sql: String, dataset: String },
    Stream { stream_id: String },
}

#[derive(Debug, Clone)]
pub enum DataTransform {
    Select { columns: Vec<String> },
    Rename { mappings: HashMap<String, String> },
    Cast { column: String, data_type: DataType },
    FillNull { column: String, value: FillValue },
    DropNulls { columns: Option<Vec<String>> },
}

#[derive(Debug, Clone)]
pub enum Filter {
    Expression {
        expr: String,
    },
    Range {
        column: String,
        min: Option<f64>,
        max: Option<f64>,
    },
    Values {
        column: String,
        values: Vec<serde_json::Value>,
    },
    Null {
        column: String,
        keep_nulls: bool,
    },
}

#[derive(Debug, Clone, Hash)]
pub enum Aggregation {
    GroupBy {
        columns: Vec<String>,
    },
    Aggregate {
        operations: Vec<AggOp>,
    },
    Pivot {
        index: String,
        columns: String,
        values: String,
    },
    Window {
        operations: Vec<WindowOp>,
    },
}

#[derive(Debug, Clone, Hash)]
pub enum AggOp {
    Sum {
        column: String,
        alias: Option<String>,
    },
    Mean {
        column: String,
        alias: Option<String>,
    },
    Count {
        column: String,
        alias: Option<String>,
    },
    Min {
        column: String,
        alias: Option<String>,
    },
    Max {
        column: String,
        alias: Option<String>,
    },
    Std {
        column: String,
        alias: Option<String>,
    },
    Var {
        column: String,
        alias: Option<String>,
    },
}

#[derive(Debug, Clone, Hash)]
pub enum WindowOp {
    RowNumber {
        alias: String,
    },
    Rank {
        column: String,
        alias: String,
    },
    Lag {
        column: String,
        offset: i64,
        alias: String,
    },
    Lead {
        column: String,
        offset: i64,
        alias: String,
    },
    RollingMean {
        column: String,
        window: usize,
        alias: String,
    },
    RollingSum {
        column: String,
        window: usize,
        alias: String,
    },
}

#[derive(Debug, Clone, Hash)]
pub enum OutputFormat {
    DataFrame,
    Json,
    Csv,
    Parquet,
    Arrow,
}

#[derive(Debug, Clone, Hash)]
pub enum DataFormat {
    Csv,
    Json,
    Parquet,
    Arrow,
}

#[derive(Debug, Clone, Hash)]
pub enum DataType {
    Int32,
    Int64,
    Float32,
    Float64,
    String,
    Boolean,
    Date,
    DateTime,
}

#[derive(Debug, Clone, Hash)]
pub enum FillValue {
    Zero,
    Mean,
    Median,
    Mode,
    Forward,
    Backward,
    Custom(serde_json::Value),
}

/// Main data processor
pub struct DataProcessor {
    strategy_selector: StrategySelector,
    cache: HashMap<u64, ProcessedData>,
    stream_buffers: HashMap<String, StreamBuffer>,
}

impl DataProcessor {
    pub fn new() -> Result<Self, DataError> {
        Ok(Self {
            strategy_selector: StrategySelector::new(),
            cache: HashMap::new(),
            stream_buffers: HashMap::new(),
        })
    }

    pub async fn process(&mut self, spec: &DataSpec) -> Result<ProcessedData, DataError> {
        // Check cache first
        let spec_hash = spec.hash();
        if let Some(cached) = self.cache.get(&spec_hash) {
            return Ok(cached.clone());
        }

        // Select optimal processing strategy
        let strategy = self.strategy_selector.select(spec);

        // Process data based on strategy
        let result = match strategy {
            ProcessingStrategy::CPU(config) => self.process_cpu(spec, &config).await,
            ProcessingStrategy::GPU(config) => self.process_gpu(spec, &config).await,
            ProcessingStrategy::Streaming(config) => self.process_streaming(spec, &config).await,
            ProcessingStrategy::Hybrid(config) => self.process_hybrid(spec, &config).await,
        }?;

        // Cache result
        self.cache.insert(spec_hash, result.clone());

        Ok(result)
    }

    async fn process_cpu(
        &self,
        spec: &DataSpec,
        config: &RayonConfig,
    ) -> Result<ProcessedData, DataError> {
        // Load data
        let mut df = self.load_data(&spec.source).await?;

        // Apply filters
        for filter in &spec.filters {
            df = self.apply_filter(df, filter)?;
        }

        // Apply transforms
        for transform in &spec.transforms {
            df = self.apply_transform(df, transform)?;
        }

        // Apply aggregations
        for aggregation in &spec.aggregations {
            df = self.apply_aggregation(df, aggregation)?;
        }

        Ok(ProcessedData {
            data: df,
            metadata: DataMetadata::from_dataframe(&df),
            processing_time: std::time::Duration::from_millis(0), // Placeholder
        })
    }

    async fn process_gpu(
        &self,
        spec: &DataSpec,
        config: &ComputeConfig,
    ) -> Result<ProcessedData, DataError> {
        // GPU processing would be implemented here
        // For now, fall back to CPU processing
        self.process_cpu(
            spec,
            &RayonConfig {
                num_threads: None,
                chunk_size: None,
            },
        )
        .await
    }

    async fn process_streaming(
        &mut self,
        spec: &DataSpec,
        config: &StreamConfig,
    ) -> Result<ProcessedData, DataError> {
        // Streaming processing implementation
        if let DataSource::Stream { stream_id } = &spec.source {
            let buffer = self
                .stream_buffers
                .entry(stream_id.clone())
                .or_insert_with(|| StreamBuffer::new(config.buffer_size));

            // Process streaming data
            buffer.process_batch(config.batch_size)
        } else {
            Err(DataError::Processing(
                "Streaming strategy requires stream source".to_string(),
            ))
        }
    }

    async fn process_hybrid(
        &self,
        spec: &DataSpec,
        config: &HybridConfig,
    ) -> Result<ProcessedData, DataError> {
        let data_size = spec.estimated_size();

        if data_size < config.cpu_threshold {
            self.process_cpu(spec, &config.cpu_config).await
        } else if data_size < config.gpu_threshold {
            self.process_gpu(spec, &config.gpu_config).await
        } else {
            // Split processing between CPU and GPU
            self.process_cpu(spec, &config.cpu_config).await
        }
    }

    async fn load_data(&self, source: &DataSource) -> Result<DataFrame, DataError> {
        match source {
            DataSource::DataFrame(df) => Ok(df.clone()),
            DataSource::Url { url, format } => match format {
                DataFormat::Csv => {
                    let df = LazyFrame::scan_csv(url, ScanArgsIo::default())
                        .collect()
                        .map_err(DataError::Polars)?;
                    Ok(df)
                }
                DataFormat::Json => {
                    let df = LazyFrame::scan_ndjson(url, ScanArgsNdJson::default())
                        .collect()
                        .map_err(DataError::Polars)?;
                    Ok(df)
                }
                DataFormat::Parquet => {
                    let df = LazyFrame::scan_parquet(url, ScanArgsParquet::default())
                        .collect()
                        .map_err(DataError::Polars)?;
                    Ok(df)
                }
                _ => Err(DataError::Format("Unsupported format".to_string())),
            },
            DataSource::Query { sql, dataset } => {
                // Execute SQL query - would integrate with DataFusion
                Err(DataError::Processing(
                    "SQL queries not yet implemented".to_string(),
                ))
            }
            DataSource::Stream { stream_id } => {
                // Get data from stream buffer
                Err(DataError::Processing(
                    "Stream processing not yet implemented".to_string(),
                ))
            }
        }
    }

    fn apply_filter(&self, df: DataFrame, filter: &Filter) -> Result<DataFrame, DataError> {
        match filter {
            Filter::Expression { expr } => {
                let lazy_df = df.lazy().filter(col(expr));
                lazy_df.collect().map_err(DataError::Polars)
            }
            Filter::Range { column, min, max } => {
                let mut lazy_df = df.lazy();
                if let Some(min_val) = min {
                    lazy_df = lazy_df.filter(col(column).gt(lit(*min_val)));
                }
                if let Some(max_val) = max {
                    lazy_df = lazy_df.filter(col(column).lt(lit(*max_val)));
                }
                lazy_df.collect().map_err(DataError::Polars)
            }
            Filter::Values { column, values } => {
                // Convert values to appropriate type and filter
                let lazy_df = df.lazy().filter(col(column).is_in(values));
                lazy_df.collect().map_err(DataError::Polars)
            }
            Filter::Null { column, keep_nulls } => {
                if *keep_nulls {
                    df.lazy()
                        .filter(col(column).is_null())
                        .collect()
                        .map_err(DataError::Polars)
                } else {
                    df.lazy()
                        .filter(col(column).is_not_null())
                        .collect()
                        .map_err(DataError::Polars)
                }
            }
        }
    }

    fn apply_transform(
        &self,
        df: DataFrame,
        transform: &DataTransform,
    ) -> Result<DataFrame, DataError> {
        match transform {
            DataTransform::Select { columns } => {
                let lazy_df = df.lazy().select(columns.iter().map(|c| col(c)));
                lazy_df.collect().map_err(DataError::Polars)
            }
            DataTransform::Rename { mappings } => {
                let mut lazy_df = df.lazy();
                for (old_name, new_name) in mappings {
                    lazy_df = lazy_df.rename([old_name], [new_name]);
                }
                lazy_df.collect().map_err(DataError::Polars)
            }
            DataTransform::Cast { column, data_type } => {
                let polars_type = match data_type {
                    DataType::Int32 => DataType::Int32,
                    DataType::Int64 => DataType::Int64,
                    DataType::Float32 => DataType::Float32,
                    DataType::Float64 => DataType::Float64,
                    DataType::String => DataType::String,
                    DataType::Boolean => DataType::Boolean,
                    DataType::Date => DataType::Date,
                    DataType::DateTime => DataType::Datetime(TimeUnit::Milliseconds, None),
                };
                let lazy_df = df.lazy().with_columns([col(column).cast(polars_type)]);
                lazy_df.collect().map_err(DataError::Polars)
            }
            DataTransform::FillNull { column, value } => {
                let fill_value = match value {
                    FillValue::Zero => lit(0),
                    FillValue::Mean => col(column).mean(),
                    FillValue::Median => col(column).median(),
                    FillValue::Mode => col(column).mode().first(),
                    FillValue::Forward => col(column).forward_fill(),
                    FillValue::Backward => col(column).backward_fill(),
                    FillValue::Custom(val) => lit(val),
                };
                let lazy_df = df.lazy().with_columns([col(column).fill_null(fill_value)]);
                lazy_df.collect().map_err(DataError::Polars)
            }
            DataTransform::DropNulls { columns } => {
                let lazy_df = if let Some(cols) = columns {
                    df.lazy().drop_nulls(Some(cols))
                } else {
                    df.lazy().drop_nulls(None)
                };
                lazy_df.collect().map_err(DataError::Polars)
            }
        }
    }

    fn apply_aggregation(
        &self,
        df: DataFrame,
        aggregation: &Aggregation,
    ) -> Result<DataFrame, DataError> {
        match aggregation {
            Aggregation::GroupBy { columns } => {
                let lazy_df = df.lazy().group_by(columns.iter().map(|c| col(c)));
                lazy_df.collect().map_err(DataError::Polars)
            }
            Aggregation::Aggregate { operations } => {
                let mut lazy_df = df.lazy();
                let agg_exprs: Vec<Expr> = operations
                    .iter()
                    .map(|op| {
                        match op {
                            AggOp::Sum { column, alias } => {
                                let expr = col(column).sum();
                                if let Some(alias) = alias {
                                    expr.alias(alias)
                                } else {
                                    expr
                                }
                            }
                            AggOp::Mean { column, alias } => {
                                let expr = col(column).mean();
                                if let Some(alias) = alias {
                                    expr.alias(alias)
                                } else {
                                    expr
                                }
                            }
                            AggOp::Count { column, alias } => {
                                let expr = col(column).count();
                                if let Some(alias) = alias {
                                    expr.alias(alias)
                                } else {
                                    expr
                                }
                            }
                            _ => col(column).sum(), // Placeholder
                        }
                    })
                    .collect();

                lazy_df.agg(agg_exprs).collect().map_err(DataError::Polars)
            }
            _ => Ok(df), // Placeholder for other aggregation types
        }
    }
}

/// Processed data result
#[derive(Debug, Clone)]
pub struct ProcessedData {
    pub data: DataFrame,
    pub metadata: DataMetadata,
    pub processing_time: std::time::Duration,
}

/// Data metadata
#[derive(Debug, Clone)]
pub struct DataMetadata {
    pub row_count: usize,
    pub column_count: usize,
    pub column_types: HashMap<String, String>,
    pub memory_usage: usize,
    pub processing_stats: ProcessingStats,
}

impl DataMetadata {
    pub fn from_dataframe(df: &DataFrame) -> Self {
        let column_types: HashMap<String, String> = df
            .get_column_names()
            .iter()
            .map(|name| {
                let dtype = df.column(name).unwrap().dtype();
                (name.clone(), format!("{:?}", dtype))
            })
            .collect();

        Self {
            row_count: df.height(),
            column_count: df.width(),
            column_types,
            memory_usage: df.estimated_size(),
            processing_stats: ProcessingStats::default(),
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct ProcessingStats {
    pub cpu_time: std::time::Duration,
    pub gpu_time: std::time::Duration,
    pub memory_peak: usize,
    pub cache_hits: u64,
    pub cache_misses: u64,
}

/// Streaming data buffer
pub struct StreamBuffer {
    buffer: Vec<DataFrame>,
    max_size: usize,
    dropped_count: u64,
}

impl StreamBuffer {
    pub fn new(max_size: usize) -> Self {
        Self {
            buffer: Vec::new(),
            max_size,
            dropped_count: 0,
        }
    }

    pub fn push(&mut self, data: DataFrame) {
        if self.buffer.len() >= self.max_size {
            self.buffer.remove(0);
            self.dropped_count += 1;
        }
        self.buffer.push(data);
    }

    pub fn process_batch(&mut self, batch_size: usize) -> Result<ProcessedData, DataError> {
        if self.buffer.is_empty() {
            return Ok(ProcessedData {
                data: DataFrame::empty(),
                metadata: DataMetadata::from_dataframe(&DataFrame::empty()),
                processing_time: std::time::Duration::from_millis(0),
            });
        }

        let combined_df = self.buffer.iter().fold(DataFrame::empty(), |acc, df| {
            if acc.is_empty() {
                df.clone()
            } else {
                concat(&[acc, df.clone()], Default::default()).unwrap_or(df.clone())
            }
        });

        Ok(ProcessedData {
            data: combined_df,
            metadata: DataMetadata::from_dataframe(&combined_df),
            processing_time: std::time::Duration::from_millis(0),
        })
    }

    pub fn health_metrics(&self) -> StreamHealth {
        StreamHealth {
            buffer_utilization: self.buffer.len() as f32 / self.max_size as f32,
            dropped_messages: self.dropped_count,
            current_size: self.buffer.len(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct StreamHealth {
    pub buffer_utilization: f32,
    pub dropped_messages: u64,
    pub current_size: usize,
}