gpui-rhai 0.1.8

A Rhai-authored declarative UI runtime for GPUI
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
#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]

use std::cmp::Ordering;
use std::collections::{BTreeMap, VecDeque, btree_map::Entry};
use std::sync::{Arc, RwLock};

use thiserror::Error;

use super::{
    ChartColumn, ChartColumnValues, ChartDataError, ChartDataLimits, ChartDataset, ChartNullBitmap,
    ChartTransformSpec, ChartValue,
};
use crate::UiValue;

#[derive(Clone, Copy, Debug, Default)]
pub struct ChartTransformContext {
    pub limits: ChartDataLimits,
}

/// Trusted Rust transform extension. Implementations receive only typed owned
/// chart data and durable options; no Rhai engine value crosses this boundary.
pub trait HostChartTransform: Send + Sync {
    /// Transform one immutable typed dataset.
    ///
    /// # Errors
    ///
    /// Returns an application-facing diagnostic; no partial output is kept.
    fn transform(
        &self,
        input: &ChartDataset,
        options: &UiValue,
        context: ChartTransformContext,
    ) -> Result<ChartDataset, String>;
}

#[derive(Clone, Default)]
pub struct ChartTransformRegistry {
    inner: Arc<RwLock<BTreeMap<String, Arc<dyn HostChartTransform>>>>,
}

impl std::fmt::Debug for ChartTransformRegistry {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let registered = self
            .inner
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .keys()
            .cloned()
            .collect::<Vec<_>>();
        formatter
            .debug_struct("ChartTransformRegistry")
            .field("registered", &registered)
            .finish()
    }
}

impl ChartTransformRegistry {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a trusted transform under a stable ID.
    ///
    /// # Errors
    ///
    /// Returns for an unsafe/duplicate ID or poisoned registry.
    pub fn register(
        &self,
        id: impl Into<String>,
        transform: impl HostChartTransform + 'static,
    ) -> Result<(), ChartTransformError> {
        let id = id.into();
        validate_identifier(&id)?;
        let mut registry = self
            .inner
            .write()
            .map_err(|_| ChartTransformError::Poisoned)?;
        match registry.entry(id.clone()) {
            Entry::Vacant(entry) => {
                entry.insert(Arc::new(transform));
            }
            Entry::Occupied(_) => return Err(ChartTransformError::DuplicateHost(id)),
        }
        Ok(())
    }

    fn get(&self, id: &str) -> Result<Arc<dyn HostChartTransform>, ChartTransformError> {
        self.inner
            .read()
            .map_err(|_| ChartTransformError::Poisoned)?
            .get(id)
            .cloned()
            .ok_or_else(|| ChartTransformError::UnknownHost(id.to_owned()))
    }

    #[cfg(feature = "dev-reload")]
    pub(crate) fn copy_from(&self, source: &Self) {
        let source = source
            .inner
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clone();
        *self
            .inner
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = source;
    }
}

/// Apply a declared transform pipeline in order.
///
/// Each stage produces a fresh validated dataset. The input remains immutable,
/// so a failing stage cannot expose a partially transformed candidate.
///
/// # Errors
///
/// Returns the first data, transform, or Host-extension diagnostic.
pub fn apply_chart_transforms(
    input: &ChartDataset,
    transforms: &[ChartTransformSpec],
    registry: &ChartTransformRegistry,
    context: ChartTransformContext,
) -> Result<ChartDataset, ChartTransformError> {
    let mut current = input.clone();
    for transform in transforms {
        current = match transform {
            ChartTransformSpec::Filter {
                dimension,
                operator,
                value,
            } => filter(&current, dimension, operator, value)?,
            ChartTransformSpec::Sort {
                dimension,
                descending,
            } => sort(&current, dimension, *descending)?,
            ChartTransformSpec::Aggregate {
                group_by,
                dimension,
                operation,
                output,
            } => aggregate(
                &current,
                group_by,
                dimension,
                operation,
                output,
                context.limits,
            )?,
            ChartTransformSpec::Bin {
                dimension,
                bins,
                output_start,
                output_end,
            } => bin(
                &current,
                dimension,
                *bins,
                output_start,
                output_end,
                context.limits,
            )?,
            ChartTransformSpec::Stack {
                group_by,
                dimension,
                output_start,
                output_end,
            } => stack(
                &current,
                group_by,
                dimension,
                output_start,
                output_end,
                context.limits,
            )?,
            ChartTransformSpec::Normalize {
                group_by,
                dimension,
                output,
            } => normalize(&current, group_by, dimension, output, context.limits)?,
            ChartTransformSpec::MovingWindow {
                dimension,
                window,
                operation,
                output,
            } => moving_window(
                &current,
                dimension,
                *window,
                operation,
                output,
                context.limits,
            )?,
            ChartTransformSpec::Downsample { x, y, threshold } => {
                downsample_lttb(&current, x, y, *threshold)?
            }
            ChartTransformSpec::Host { id, options } => registry
                .get(id)?
                .transform(&current, options, context)
                .map_err(|message| ChartTransformError::HostFailed {
                    id: id.clone(),
                    message,
                })?,
        };
    }
    Ok(current)
}

fn filter(
    input: &ChartDataset,
    dimension: &str,
    operator: &str,
    expected: &UiValue,
) -> Result<ChartDataset, ChartTransformError> {
    if !matches!(
        operator,
        "eq" | "ne" | "lt" | "lte" | "gt" | "gte" | "contains"
    ) {
        return Err(ChartTransformError::UnknownFilterOperator(
            operator.to_owned(),
        ));
    }
    let column = require_column(input, dimension)?;
    let expected = ui_scalar(expected)?;
    let indices = (0..input.len())
        .filter(|index| {
            column
                .value(*index)
                .is_some_and(|actual| compare_filter(&actual, operator, &expected))
        })
        .collect::<Vec<_>>();
    Ok(input.select_rows(&indices)?)
}

fn compare_filter(actual: &ChartValue, operator: &str, expected: &ChartValue) -> bool {
    match operator {
        "eq" => compare_values(actual, expected) == Some(Ordering::Equal),
        "ne" => compare_values(actual, expected) != Some(Ordering::Equal),
        "lt" => compare_values(actual, expected) == Some(Ordering::Less),
        "lte" => matches!(
            compare_values(actual, expected),
            Some(Ordering::Less | Ordering::Equal)
        ),
        "gt" => compare_values(actual, expected) == Some(Ordering::Greater),
        "gte" => matches!(
            compare_values(actual, expected),
            Some(Ordering::Greater | Ordering::Equal)
        ),
        "contains" => matches!((actual, expected),
            (ChartValue::String(actual), ChartValue::String(expected)) if actual.contains(expected)),
        _ => false,
    }
}

fn sort(
    input: &ChartDataset,
    dimension: &str,
    descending: bool,
) -> Result<ChartDataset, ChartTransformError> {
    let column = require_column(input, dimension)?;
    let mut indices = (0..input.len()).collect::<Vec<_>>();
    indices.sort_by(|left, right| {
        let order = match (column.value(*left), column.value(*right)) {
            (Some(left), Some(right)) => compare_values(&left, &right).unwrap_or(Ordering::Equal),
            _ => Ordering::Equal,
        };
        if descending { order.reverse() } else { order }
    });
    Ok(input.select_rows(&indices)?)
}

fn aggregate(
    input: &ChartDataset,
    group_by: &[String],
    dimension: &str,
    operation: &str,
    output: &str,
    limits: ChartDataLimits,
) -> Result<ChartDataset, ChartTransformError> {
    validate_output(output)?;
    let value_column = require_column(input, dimension)?;
    let group_columns = group_columns(input, group_by)?;
    let mut groups = BTreeMap::<Vec<TypedGroupKey>, (Vec<ChartValue>, Vec<ChartValue>)>::new();
    for row in 0..input.len() {
        let values = group_columns
            .iter()
            .map(|column| column.value(row).unwrap_or(ChartValue::Null))
            .collect::<Vec<_>>();
        let key = values.iter().map(TypedGroupKey::from).collect::<Vec<_>>();
        let entry = groups.entry(key).or_insert_with(|| (values, Vec::new()));
        if let Some(value) = value_column.value(row)
            && !matches!(value, ChartValue::Null)
        {
            entry.1.push(value);
        }
    }
    let rows = groups
        .into_iter()
        .map(|(_, (key, values))| {
            let mut row = group_by
                .iter()
                .cloned()
                .zip(key)
                .collect::<BTreeMap<_, _>>();
            row.insert(
                output.to_owned(),
                ChartValue::Number(reduce_values(&values, operation)?),
            );
            Ok(row)
        })
        .collect::<Result<Vec<_>, ChartTransformError>>()?;
    if rows.is_empty() {
        let empty = input.select_rows(&[])?;
        let mut columns = group_by
            .iter()
            .filter_map(|name| empty.column(name).cloned())
            .collect::<Vec<_>>();
        columns.push(ChartColumn::new(
            output,
            ChartColumnValues::Number(Vec::<f64>::new().into()),
            ChartNullBitmap::from_validity(Vec::<bool>::new()),
        )?);
        return Ok(ChartDataset::new(input.name(), columns, None, limits)?);
    }
    Ok(ChartDataset::from_chart_rows(
        input.name(),
        &rows,
        None,
        limits,
    )?)
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum TypedGroupKey {
    Null,
    Number(u64),
    Integer(i64),
    Timestamp(i64),
    Bool(bool),
    String(String),
}

impl From<&ChartValue> for TypedGroupKey {
    fn from(value: &ChartValue) -> Self {
        match value {
            ChartValue::Null => Self::Null,
            ChartValue::Number(value) => Self::Number(value.to_bits()),
            ChartValue::Integer(value) => Self::Integer(*value),
            ChartValue::Timestamp(value) => Self::Timestamp(*value),
            ChartValue::Bool(value) => Self::Bool(*value),
            ChartValue::String(value) => Self::String(value.clone()),
        }
    }
}

fn reduce_values(values: &[ChartValue], operation: &str) -> Result<f64, ChartTransformError> {
    if operation == "count" {
        return Ok(usize_to_f64(values.len()));
    }
    let numbers = values
        .iter()
        .filter_map(ChartValue::as_number)
        .collect::<Vec<_>>();
    reduce(&numbers, operation)
}

fn reduce(values: &[f64], operation: &str) -> Result<f64, ChartTransformError> {
    let value = match operation {
        "sum" => values.iter().sum(),
        "mean" => {
            if values.is_empty() {
                0.0
            } else {
                values.iter().sum::<f64>() / usize_to_f64(values.len())
            }
        }
        "min" => values.iter().copied().reduce(f64::min).unwrap_or(0.0),
        "max" => values.iter().copied().reduce(f64::max).unwrap_or(0.0),
        "count" => usize_to_f64(values.len()),
        other => return Err(ChartTransformError::UnknownOperation(other.to_owned())),
    };
    Ok(value)
}

fn bin(
    input: &ChartDataset,
    dimension: &str,
    bins: u16,
    output_start: &str,
    output_end: &str,
    limits: ChartDataLimits,
) -> Result<ChartDataset, ChartTransformError> {
    validate_output(output_start)?;
    validate_output(output_end)?;
    let column = require_column(input, dimension)?;
    let values = (0..input.len())
        .filter_map(|row| column.value(row).and_then(|value| value.as_number()))
        .collect::<Vec<_>>();
    let Some(min) = values.iter().copied().reduce(f64::min) else {
        return Ok(input.clone());
    };
    let max = values.iter().copied().reduce(f64::max).unwrap_or(min);
    let width = if max > min {
        (max - min) / f64::from(bins)
    } else {
        1.0
    };
    let mut rows = input.rows();
    for row in &mut rows {
        let value = row
            .get(dimension)
            .and_then(ChartValue::as_number)
            .unwrap_or(min);
        let index = ((value - min) / width)
            .floor()
            .clamp(0.0, f64::from(bins.saturating_sub(1)));
        let start = min + index * width;
        row.insert(output_start.to_owned(), ChartValue::Number(start));
        row.insert(output_end.to_owned(), ChartValue::Number(start + width));
    }
    Ok(ChartDataset::from_chart_rows(
        input.name(),
        &rows,
        input.key_dimension().map(ToOwned::to_owned),
        limits,
    )?)
}

fn stack(
    input: &ChartDataset,
    group_by: &[String],
    dimension: &str,
    output_start: &str,
    output_end: &str,
    limits: ChartDataLimits,
) -> Result<ChartDataset, ChartTransformError> {
    validate_output(output_start)?;
    validate_output(output_end)?;
    let column = require_column(input, dimension)?;
    let group_columns = group_columns(input, group_by)?;
    let mut positive = BTreeMap::<Vec<String>, f64>::new();
    let mut negative = BTreeMap::<Vec<String>, f64>::new();
    let mut rows = input.rows();
    for (index, row) in rows.iter_mut().enumerate() {
        let key = group_columns
            .iter()
            .map(|column| {
                column
                    .value(index)
                    .unwrap_or(ChartValue::Null)
                    .display_text()
            })
            .collect::<Vec<_>>();
        let value = column
            .value(index)
            .and_then(|value| value.as_number())
            .unwrap_or(0.0);
        let running = if value >= 0.0 {
            positive.entry(key).or_default()
        } else {
            negative.entry(key).or_default()
        };
        let start = *running;
        *running += value;
        row.insert(output_start.to_owned(), ChartValue::Number(start));
        row.insert(output_end.to_owned(), ChartValue::Number(*running));
    }
    Ok(ChartDataset::from_chart_rows(
        input.name(),
        &rows,
        input.key_dimension().map(ToOwned::to_owned),
        limits,
    )?)
}

fn normalize(
    input: &ChartDataset,
    group_by: &[String],
    dimension: &str,
    output: &str,
    limits: ChartDataLimits,
) -> Result<ChartDataset, ChartTransformError> {
    validate_output(output)?;
    let column = require_column(input, dimension)?;
    let group_columns = group_columns(input, group_by)?;
    let mut totals = BTreeMap::<Vec<String>, f64>::new();
    for row in 0..input.len() {
        let key = group_columns
            .iter()
            .map(|column| column.value(row).unwrap_or(ChartValue::Null).display_text())
            .collect::<Vec<_>>();
        let value = column
            .value(row)
            .and_then(|value| value.as_number())
            .unwrap_or(0.0)
            .abs();
        *totals.entry(key).or_default() += value;
    }
    let mut rows = input.rows();
    for (index, row) in rows.iter_mut().enumerate() {
        let key = group_columns
            .iter()
            .map(|column| {
                column
                    .value(index)
                    .unwrap_or(ChartValue::Null)
                    .display_text()
            })
            .collect::<Vec<_>>();
        let total = totals.get(&key).copied().unwrap_or(0.0);
        let value = column
            .value(index)
            .and_then(|value| value.as_number())
            .unwrap_or(0.0);
        row.insert(
            output.to_owned(),
            ChartValue::Number(if total > 0.0 { value / total } else { 0.0 }),
        );
    }
    Ok(ChartDataset::from_chart_rows(
        input.name(),
        &rows,
        input.key_dimension().map(ToOwned::to_owned),
        limits,
    )?)
}

fn moving_window(
    input: &ChartDataset,
    dimension: &str,
    window: usize,
    operation: &str,
    output: &str,
    limits: ChartDataLimits,
) -> Result<ChartDataset, ChartTransformError> {
    validate_output(output)?;
    if window == 0 || window > limits.max_rows {
        return Err(ChartTransformError::InvalidWindow(window));
    }
    let column = require_column(input, dimension)?;
    let mut active = VecDeque::with_capacity(window);
    let mut rows = input.rows();
    for (index, row) in rows.iter_mut().enumerate() {
        if let Some(value) = column.value(index).and_then(|value| value.as_number()) {
            active.push_back(value);
        }
        while active.len() > window {
            active.pop_front();
        }
        row.insert(
            output.to_owned(),
            ChartValue::Number(reduce(active.make_contiguous(), operation)?),
        );
    }
    Ok(ChartDataset::from_chart_rows(
        input.name(),
        &rows,
        input.key_dimension().map(ToOwned::to_owned),
        limits,
    )?)
}

/// Largest-Triangle-Three-Buckets downsampling. Returned rows keep their
/// original key values, so hit testing can resolve back to source identity.
fn downsample_lttb(
    input: &ChartDataset,
    x: &str,
    y: &str,
    threshold: usize,
) -> Result<ChartDataset, ChartTransformError> {
    if threshold < 3 || input.len() <= threshold {
        return Ok(input.clone());
    }
    let x = require_column(input, x)?;
    let y = require_column(input, y)?;
    let categorical_x = matches!(
        x.data_type(),
        super::ChartDataType::String | super::ChartDataType::Bool
    );
    let mut segments = Vec::<Vec<(usize, f64, f64)>>::new();
    let mut current = Vec::new();
    let mut separators = Vec::<usize>::new();
    let mut pending_separator = None;
    for index in 0..input.len() {
        let x = x.value(index).and_then(|value| {
            if categorical_x {
                (value != ChartValue::Null).then(|| usize_to_f64(index))
            } else {
                value.as_number()
            }
        });
        let y = y.value(index).and_then(|value| value.as_number());
        if let (Some(x), Some(y)) = (x, y) {
            if current.is_empty()
                && !segments.is_empty()
                && let Some(separator) = pending_separator.take()
            {
                separators.push(separator);
            }
            current.push((index, x, y));
        } else if !current.is_empty() {
            segments.push(std::mem::take(&mut current));
            pending_separator = Some(index);
        } else if !segments.is_empty() && pending_separator.is_none() {
            pending_separator = Some(index);
        }
    }
    if !current.is_empty() {
        segments.push(current);
    }
    if segments.is_empty() {
        return Ok(input.select_rows(&[])?);
    }

    let max_segments = threshold.div_ceil(2).max(1);
    let kept = evenly_spaced_indexes(segments.len(), segments.len().min(max_segments));
    let separator_count = kept.len().saturating_sub(1);
    let point_budget = threshold.saturating_sub(separator_count).max(1);
    let lengths = kept
        .iter()
        .map(|index| segments[*index].len())
        .collect::<Vec<_>>();
    let allocations = allocate_segment_budget(&lengths, point_budget);
    let mut selected = Vec::with_capacity(threshold);
    for (position, (segment_index, allocation)) in kept.iter().copied().zip(allocations).enumerate()
    {
        selected.extend(sample_segment(&segments[segment_index], allocation));
        if let Some(next) = kept.get(position + 1) {
            let separator_index = segment_index.min(separators.len().saturating_sub(1));
            if segment_index < *next
                && let Some(separator) = separators.get(separator_index)
            {
                selected.push(*separator);
            }
        }
    }
    selected.sort_unstable();
    selected.dedup();
    debug_assert!(selected.len() <= threshold);
    Ok(input.select_rows(&selected)?)
}

fn evenly_spaced_indexes(len: usize, count: usize) -> Vec<usize> {
    if count >= len {
        return (0..len).collect();
    }
    if count <= 1 {
        return vec![len / 2];
    }
    (0..count)
        .map(|index| index * (len - 1) / (count - 1))
        .collect()
}

fn allocate_segment_budget(lengths: &[usize], budget: usize) -> Vec<usize> {
    let mut allocated = vec![1; lengths.len()];
    let requested = budget.saturating_sub(lengths.len());
    let capacities = lengths
        .iter()
        .map(|length| length.saturating_sub(1))
        .collect::<Vec<_>>();
    let total_capacity = capacities.iter().sum::<usize>();
    if requested == 0 || total_capacity == 0 {
        return allocated;
    }
    let remaining = requested.min(total_capacity);
    let mut remainders = Vec::with_capacity(lengths.len());
    let mut used = 0_usize;
    for (index, capacity) in capacities.iter().copied().enumerate() {
        let weighted = remaining as u128 * capacity as u128;
        let extra = usize::try_from(weighted / total_capacity as u128).unwrap_or(capacity);
        allocated[index] += extra.min(capacity);
        used += extra.min(capacity);
        remainders.push((index, weighted % total_capacity as u128));
    }
    remainders.sort_by(|left, right| right.1.cmp(&left.1).then(left.0.cmp(&right.0)));
    for (index, _) in remainders.into_iter().take(remaining.saturating_sub(used)) {
        if allocated[index] < lengths[index] {
            allocated[index] += 1;
        }
    }
    allocated
}

fn sample_segment(points: &[(usize, f64, f64)], threshold: usize) -> Vec<usize> {
    match threshold {
        0 => Vec::new(),
        1 => vec![points[points.len() / 2].0],
        2 if points.len() > 1 => vec![points[0].0, points[points.len() - 1].0],
        _ => lttb_segment(points, threshold),
    }
}

fn lttb_segment(points: &[(usize, f64, f64)], threshold: usize) -> Vec<usize> {
    if points.len() <= threshold || threshold < 3 {
        return points.iter().map(|point| point.0).collect();
    }
    let bucket_size =
        usize_to_f64(points.len().saturating_sub(2)) / usize_to_f64(threshold.saturating_sub(2));
    let mut selected = Vec::with_capacity(threshold);
    selected.push(points[0].0);
    let mut anchor = 0;
    for bucket in 0..threshold.saturating_sub(2) {
        let next_start =
            ((usize_to_f64(bucket + 1) * bucket_size).floor() as usize + 1).min(points.len() - 1);
        let next_end =
            ((usize_to_f64(bucket + 2) * bucket_size).floor() as usize + 1).min(points.len());
        let average = if next_start < next_end {
            let slice = &points[next_start..next_end];
            let count = usize_to_f64(slice.len());
            (
                slice.iter().map(|point| point.1).sum::<f64>() / count,
                slice.iter().map(|point| point.2).sum::<f64>() / count,
            )
        } else {
            (points[points.len() - 1].1, points[points.len() - 1].2)
        };
        let start =
            ((usize_to_f64(bucket) * bucket_size).floor() as usize + 1).min(points.len() - 1);
        let end = ((usize_to_f64(bucket + 1) * bucket_size).floor() as usize + 1)
            .min(points.len() - 1)
            .max(start + 1);
        let anchor_point = (points[anchor].1, points[anchor].2);
        let (candidate, _) = (start..end)
            .map(|index| {
                let point = (points[index].1, points[index].2);
                let area = ((anchor_point.0 - average.0) * (point.1 - anchor_point.1)
                    - (anchor_point.0 - point.0) * (average.1 - anchor_point.1))
                    .abs();
                (index, area)
            })
            .max_by(|left, right| left.1.total_cmp(&right.1))
            .unwrap_or((start, 0.0));
        selected.push(points[candidate].0);
        anchor = candidate;
    }
    selected.push(points[points.len() - 1].0);
    selected
}

fn require_column<'a>(
    input: &'a ChartDataset,
    dimension: &str,
) -> Result<&'a super::ChartColumn, ChartTransformError> {
    input
        .column(dimension)
        .ok_or_else(|| ChartTransformError::MissingDimension(dimension.to_owned()))
}

fn group_columns<'a>(
    input: &'a ChartDataset,
    dimensions: &[String],
) -> Result<Vec<&'a super::ChartColumn>, ChartTransformError> {
    dimensions
        .iter()
        .map(|dimension| require_column(input, dimension))
        .collect()
}

fn compare_values(left: &ChartValue, right: &ChartValue) -> Option<Ordering> {
    match (left, right) {
        (ChartValue::Null, ChartValue::Null) => Some(Ordering::Equal),
        (ChartValue::Null, _) => Some(Ordering::Less),
        (_, ChartValue::Null) => Some(Ordering::Greater),
        (ChartValue::String(left), ChartValue::String(right)) => Some(left.cmp(right)),
        (ChartValue::Bool(left), ChartValue::Bool(right)) => Some(left.cmp(right)),
        _ => left
            .as_number()
            .zip(right.as_number())
            .map(|(left, right)| left.total_cmp(&right)),
    }
}

fn ui_scalar(value: &UiValue) -> Result<ChartValue, ChartTransformError> {
    Ok(match value {
        UiValue::Null => ChartValue::Null,
        UiValue::Bool(value) => ChartValue::Bool(*value),
        UiValue::Integer(value) => ChartValue::Integer(*value),
        UiValue::Float(value) if value.is_finite() => ChartValue::Number(*value),
        UiValue::String(value) => ChartValue::String(value.clone()),
        _ => return Err(ChartTransformError::InvalidFilterValue),
    })
}

fn validate_identifier(value: &str) -> Result<(), ChartTransformError> {
    if !value.is_empty()
        && value.len() <= 128
        && value
            .chars()
            .next()
            .is_some_and(|character| character.is_ascii_alphabetic())
        && value
            .chars()
            .all(|character| character.is_ascii_alphanumeric() || character == '_')
    {
        Ok(())
    } else {
        Err(ChartTransformError::InvalidHostId(value.to_owned()))
    }
}

fn validate_output(value: &str) -> Result<(), ChartTransformError> {
    validate_identifier(value).map_err(|_| ChartTransformError::InvalidOutput(value.to_owned()))
}

#[allow(clippy::cast_precision_loss)]
fn usize_to_f64(value: usize) -> f64 {
    value as f64
}

#[derive(Debug, Error, Clone, PartialEq)]
pub enum ChartTransformError {
    #[error(transparent)]
    Data(#[from] ChartDataError),
    #[error("chart transform dimension `{0}` does not exist")]
    MissingDimension(String),
    #[error("chart transform row {row} requires numeric x/y values")]
    NonNumeric { row: usize },
    #[error("chart filter value must be a durable scalar")]
    InvalidFilterValue,
    #[error("chart filter operator `{0}` is not supported")]
    UnknownFilterOperator(String),
    #[error("chart aggregate operation `{0}` is not supported")]
    UnknownOperation(String),
    #[error("chart transform output `{0}` must be a safe identifier")]
    InvalidOutput(String),
    #[error("chart moving-window size {0} is invalid")]
    InvalidWindow(usize),
    #[error("host chart transform ID `{0}` must be a safe identifier")]
    InvalidHostId(String),
    #[error("host chart transform `{0}` is already registered")]
    DuplicateHost(String),
    #[error("host chart transform `{0}` is not registered")]
    UnknownHost(String),
    #[error("host chart transform `{id}` failed: {message}")]
    HostFailed { id: String, message: String },
    #[error("chart transform registry lock was poisoned")]
    Poisoned,
}

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

    fn dataset(rows: usize) -> ChartDataset {
        let rows = (0..rows)
            .map(|index| {
                BTreeMap::from([
                    ("id".to_owned(), ChartValue::String(format!("p{index}"))),
                    ("x".to_owned(), ChartValue::Number(usize_to_f64(index))),
                    (
                        "y".to_owned(),
                        ChartValue::Number((usize_to_f64(index) / 5.0).sin()),
                    ),
                    (
                        "group".to_owned(),
                        ChartValue::String(if index % 2 == 0 { "a" } else { "b" }.to_owned()),
                    ),
                ])
            })
            .collect::<Vec<_>>();
        ChartDataset::from_chart_rows(
            "main",
            &rows,
            Some("id".to_owned()),
            ChartDataLimits::default(),
        )
        .unwrap()
    }

    #[test]
    fn lttb_preserves_first_last_and_original_datum_keys() {
        let source = dataset(1_000);
        let sampled = apply_chart_transforms(
            &source,
            &[ChartTransformSpec::Downsample {
                x: "x".to_owned(),
                y: "y".to_owned(),
                threshold: 100,
            }],
            &ChartTransformRegistry::new(),
            ChartTransformContext::default(),
        )
        .unwrap();

        assert_eq!(sampled.len(), 100);
        assert_eq!(sampled.key(0).as_deref(), Some("p0"));
        assert_eq!(sampled.key(99).as_deref(), Some("p999"));
        let keys = (0..sampled.len())
            .filter_map(|index| sampled.key(index))
            .collect::<BTreeSet<_>>();
        assert_eq!(keys.len(), sampled.len());
    }

    #[test]
    fn transform_failure_keeps_the_input_immutable() {
        let source = dataset(8);
        let result = apply_chart_transforms(
            &source,
            &[ChartTransformSpec::MovingWindow {
                dimension: "missing".to_owned(),
                window: 3,
                operation: "mean".to_owned(),
                output: "average".to_owned(),
            }],
            &ChartTransformRegistry::new(),
            ChartTransformContext::default(),
        );
        assert!(matches!(
            result,
            Err(ChartTransformError::MissingDimension(_))
        ));
        assert_eq!(source.len(), 8);
        assert!(source.column("average").is_none());
    }

    #[test]
    fn lttb_collapses_null_runs_and_respects_the_total_budget() {
        let rows = (0..1_000)
            .map(|index| {
                BTreeMap::from([
                    ("id".to_owned(), ChartValue::String(format!("p{index}"))),
                    ("x".to_owned(), ChartValue::Number(usize_to_f64(index))),
                    (
                        "y".to_owned(),
                        if index % 3 == 1 {
                            ChartValue::Null
                        } else {
                            ChartValue::Number(usize_to_f64(index))
                        },
                    ),
                ])
            })
            .collect::<Vec<_>>();
        let source = ChartDataset::from_chart_rows(
            "main",
            &rows,
            Some("id".to_owned()),
            ChartDataLimits::default(),
        )
        .unwrap();
        let sampled = apply_chart_transforms(
            &source,
            &[ChartTransformSpec::Downsample {
                x: "x".to_owned(),
                y: "y".to_owned(),
                threshold: 100,
            }],
            &ChartTransformRegistry::new(),
            ChartTransformContext::default(),
        )
        .unwrap();
        assert!(sampled.len() <= 100);
        assert!(
            (0..sampled.len()).any(|index| sampled
                .column("y")
                .unwrap()
                .value(index)
                .is_some_and(|value| value == ChartValue::Null)),
            "sampling removed every gap separator"
        );
    }
}