connect-stream-types 0.1.1

Type definitions for streaming types used internally by Nominal Connect
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
use std::collections::HashMap;
use std::collections::HashSet;
use std::ops::Deref;
use std::ops::DerefMut;

use serde::Deserialize;
use serde::Serialize;

use crate::Value;
use crate::ValueRef;
use crate::ValueSeries;

type Result<T, E = SampleTableError> = std::result::Result<T, E>;

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
pub struct ChannelStreamDescriptor {
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub unit: Option<String>,
}

impl ChannelStreamDescriptor {
    pub fn new(name: String, unit: Option<String>) -> Self {
        Self { name, unit }
    }
}

impl FromIterator<ChannelStreamDescriptor> for HashMap<String, String> {
    fn from_iter<T: IntoIterator<Item = ChannelStreamDescriptor>>(iter: T) -> Self {
        iter.into_iter()
            .filter_map(|channel| channel.unit.map(|unit| (channel.name, unit)))
            .collect::<HashMap<_, _>>()
    }
}

#[derive(thiserror::Error, Debug)]
pub enum SampleTableError {
    #[error("found {0} values, expected {1}")]
    RowCountMismatch(usize, usize),
    #[error("number of channels ({0}) must be equal to the number of value series ({1})")]
    ColumnCountMismatch(usize, usize),
    #[error("channel name collision: {0}")]
    NameCollision(String),
    #[error("channel not found: {0}")]
    ChannelNotFound(String),
    #[error("error applying function to value: {0}")]
    ApplyError(String),
}

/// Raw intermediate representation used only for deserialization.
/// Serde deserializes into this, then [`TryFrom`] validates dimensions
/// before producing a [`SampleTable`].
#[derive(Deserialize)]
struct RawSampleTable {
    timestamps: Vec<u64>,
    channels: Vec<ChannelStreamDescriptor>,
    values: Vec<ValueSeries>,
}

impl TryFrom<RawSampleTable> for SampleTable {
    type Error = SampleTableError;

    fn try_from(raw: RawSampleTable) -> Result<Self> {
        SampleTable::new(raw.timestamps, raw.channels, raw.values)
    }
}

/// A table of sample values, with timestamps indexing rows and channels indexing columns.
/// Values are stored in column-major order.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(try_from = "RawSampleTable")]
pub struct SampleTable {
    timestamps: Vec<u64>,
    channels: Vec<ChannelStreamDescriptor>,
    values: Vec<ValueSeries>,
}

/// Channel information and data referenced from a [`SampleTable`] for a given timestamp.
#[derive(Clone, Debug, PartialEq)]
pub struct RowRef<'values>(
    /// The timestamp of the row.
    pub u64,
    /// The channel and value for each column in the row.
    pub Box<[(&'values ChannelStreamDescriptor, ValueRef<'values>)]>,
);

impl SampleTable {
    fn verify_dimensions(&self) -> Result<()> {
        if self.channels.len() != self.values.len() {
            return Err(SampleTableError::ColumnCountMismatch(
                self.channels.len(),
                self.values.len(),
            ));
        }

        for channel in &self.values {
            if channel.len() != self.timestamps.len() {
                return Err(SampleTableError::RowCountMismatch(
                    channel.len(),
                    self.timestamps.len(),
                ));
            }
        }

        Ok(())
    }

    fn check_channel_names_unique(channels: &[ChannelStreamDescriptor]) -> Result<()> {
        let mut seen_names = HashSet::new();

        for channel in channels {
            if !seen_names.insert(&channel.name) {
                return Err(SampleTableError::NameCollision(channel.name.clone()));
            }
        }

        Ok(())
    }

    /// Create a sample table from a list of timestamps, channels, and value series.
    ///
    /// Each [`ValueSeries`] corresponds to the index-matched channel in `channels`,
    /// and must have the same length as `timestamps`.
    pub fn new(
        timestamps: Vec<u64>,
        channels: Vec<ChannelStreamDescriptor>,
        values: Vec<ValueSeries>,
    ) -> Result<Self> {
        Self::check_channel_names_unique(&channels)?;

        if values.len() != channels.len() {
            return Err(SampleTableError::ColumnCountMismatch(
                channels.len(),
                values.len(),
            ));
        }

        for series in &values {
            if series.len() != timestamps.len() {
                return Err(SampleTableError::RowCountMismatch(
                    series.len(),
                    timestamps.len(),
                ));
            }
        }

        Ok(Self {
            timestamps,
            channels,
            values,
        })
    }

    /// Create a sample table with a single timestamp and single values for a single channel.
    pub fn from_single_channel(
        timestamp: u64,
        channel: ChannelStreamDescriptor,
        value: Value,
    ) -> Self {
        Self {
            timestamps: vec![timestamp],
            channels: vec![channel],
            values: vec![value.into()],
        }
    }

    /// Create a sample table with a single timestamp and single values for multiple channels.
    pub fn from_multiple_channels(
        timestamp: u64,
        channels: Vec<ChannelStreamDescriptor>,
        values: Vec<Value>,
    ) -> Result<Self> {
        Self::check_channel_names_unique(&channels)?;

        if values.len() != channels.len() {
            return Err(SampleTableError::ColumnCountMismatch(
                values.len(),
                channels.len(),
            ));
        }

        Ok(Self {
            timestamps: vec![timestamp],
            values: values.into_iter().map(Value::into_series).collect(),
            channels,
        })
    }

    /// Lazily-evaluated iterator over the rows of the sample table.
    pub fn iter_rows<'values>(&'values self) -> Result<impl Iterator<Item = RowRef<'values>>> {
        self.verify_dimensions()?;

        Ok(self.timestamps.iter().enumerate().map(|(row, timestamp)| {
            RowRef(
                *timestamp,
                self.channels
                    .iter()
                    .enumerate()
                    .map(|(col, channel)| {
                        #[expect(clippy::expect_used, reason = "row and col are checked above")]
                        self.values
                            .get(col)
                            .and_then(|v| v.get(row))
                            .map(|v| (channel, v))
                            .expect("value series length check failed after invariants checked")
                    })
                    .collect::<Vec<_>>()
                    .into_boxed_slice(),
            )
        }))
    }

    /// The channels in the sample table.
    pub fn channels(&self) -> &[ChannelStreamDescriptor] {
        &self.channels
    }

    /// The timestamps in the sample table.
    pub fn timestamps(&self) -> &[u64] {
        &self.timestamps
    }

    /// The columns in the sample table.
    pub fn columns(&self) -> &[ValueSeries] {
        &self.values
    }

    /// The values for a given channel.
    pub fn get_channel_values(&self, channel: &ChannelStreamDescriptor) -> Option<&ValueSeries> {
        let offset = self.channels.iter().position(|c| c.name == channel.name)?;
        self.values.get(offset)
    }

    /// The values for a given channel and timestamp.
    pub fn get_timestamped_channel_values(
        &self,
        channel: &ChannelStreamDescriptor,
    ) -> Option<impl Iterator<Item = (u64, ValueRef<'_>)>> {
        let values = self.get_channel_values(channel)?;
        Some(self.timestamps.iter().cloned().zip(values))
    }

    /// The number of points in the stream.
    pub fn num_points(&self) -> usize {
        self.timestamps()
            .len()
            .saturating_mul(self.channels().len())
    }

    /// Apply a scaling function to a channel's values in place.
    ///
    /// An error is returned if the channel specified is not present.
    ///
    /// If any individual scaling operation fails, no further values are computed and the error is returned.
    pub fn scale_channel(
        &mut self,
        channel: &ChannelStreamDescriptor,
        f: impl FnMut(ValueRef<'_>) -> Result<f64, String>,
    ) -> Result<()> {
        let offset = self
            .channels
            .iter()
            .position(|c| c.name == channel.name)
            .ok_or(SampleTableError::ChannelNotFound(channel.name.clone()))?;

        let Some(channel_values) = self.values.get_mut(offset) else {
            return Err(SampleTableError::ChannelNotFound(channel.name.clone()));
        };

        let apply_result = channel_values
            .iter()
            .map(f)
            .collect::<Result<Vec<_>, String>>()
            .map_err(|e| SampleTableError::ApplyError(e.to_string()))?;

        *channel_values = apply_result.into();

        Ok(())
    }
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct StreamData {
    /// Unique identifier for this stream.
    pub stream_id: String,

    /// Origin of this stream data, used for attribution in metrics.
    ///
    /// This remains optional for wire compatibility. When absent, ingress
    /// rollups are still tracked under an explicit `"unknown"` source.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,

    /// The actual data being streamed.
    pub samples: SampleTable,
}

impl Deref for StreamData {
    type Target = SampleTable;
    fn deref(&self) -> &Self::Target {
        &self.samples
    }
}

impl DerefMut for StreamData {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.samples
    }
}

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

    fn channel(name: &str, unit: Option<&str>) -> ChannelStreamDescriptor {
        ChannelStreamDescriptor {
            name: name.to_string(),
            unit: unit.map(ToString::to_string),
        }
    }

    #[test]
    fn sample_table_creation_rejects_dimension_mismatches() {
        let mismatched_rows = SampleTable::new(
            vec![1, 2, 3],
            vec![channel("ch0", None), channel("ch1", None)],
            vec![vec![1.0, 2.0].into(), vec![3.0, 4.0].into()],
        );
        assert!(
            matches!(
                mismatched_rows.as_ref().unwrap_err(),
                SampleTableError::RowCountMismatch(..)
            ),
            "expected RowCountMismatch error, got: {mismatched_rows:?}"
        );

        let mismatched_columns = SampleTable::new(
            vec![1, 2, 3],
            vec![channel("ch0", None), channel("ch1", None)],
            vec![vec![1.0, 2.0].into()],
        );
        assert!(matches!(
            mismatched_columns.unwrap_err(),
            SampleTableError::ColumnCountMismatch(2, 1)
        ));
    }

    #[test]
    fn sample_table_creation_fails_with_duplicate_channel_names() {
        let result = SampleTable::new(
            vec![1, 2, 3],
            vec![channel("ch0", None), channel("ch0", None)],
            vec![vec![1.0, 2.0].into(), vec![3.0, 4.0].into()],
        );
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            SampleTableError::NameCollision(..)
        ));
    }

    #[test]
    fn sample_table_column_lookup_by_name() {
        let table = SampleTable::new(
            vec![1, 2, 3],
            vec![channel("ch0", None)],
            vec![vec![1.0, 2.0, 3.0].into()],
        )
        .expect("creating sample table should succeed");

        assert!(table.get_channel_values(&channel("ch0", None)).is_some());
        assert!(
            table
                .get_channel_values(&channel("missing", None))
                .is_none()
        );
    }

    #[test]
    fn missing_stream_data_source_defaults_to_unknown() {
        let data: StreamData = serde_json::from_str(
            r#"{
                "stream_id": "stream-1",
                "samples": {
                    "timestamps": [42],
                    "channels": [{
                        "name": "temp"
                    }],
                    "values": [[1.0]]
                }
            }"#,
        )
        .expect("deserializing stream data should succeed");

        assert!(data.source.is_none());
    }

    #[test]
    fn single_frame_round_trips() {
        let data = StreamData {
            stream_id: "s1".to_string(),
            source: Some("test".to_string()),
            samples: SampleTable::from_single_channel(
                100,
                channel("ch0", Some("V")),
                Value::Double(1.5),
            ),
        };

        let json = serde_json::to_string(&data).expect("serialize");
        let parsed: StreamData = serde_json::from_str(&json).expect("deserialize");

        assert_eq!(parsed.stream_id, "s1");
        assert_eq!(parsed.source.unwrap(), "test");

        let SampleTable {
            timestamps,
            channels,
            values,
        } = parsed.samples;

        assert_eq!(timestamps.len(), 1, "expected 1 timestamp");
        assert_eq!(channels.len(), 1, "expected 1 channel");
        assert_eq!(values.len(), 1, "expected 1 value");

        assert_eq!(
            *timestamps.first().expect("timestamp should be present"),
            100
        );
        assert_eq!(
            channels.first().expect("channel should be present").name,
            "ch0"
        );
        assert_eq!(
            channels
                .first()
                .expect("channel should be present")
                .unit
                .as_deref(),
            Some("V")
        );
        assert_eq!(values.first().expect("value should be present").len(), 1);
        assert_eq!(
            values
                .first()
                .expect("value should be present")
                .get(0)
                .expect("value should be present"),
            Value::Double(1.5)
        );
    }

    #[test]
    fn tabular_frame_round_trips() {
        let data = StreamData {
            stream_id: "daq".to_string(),
            source: Some("labjack_t7".to_string()),
            samples: SampleTable::new(
                vec![100, 200, 300],
                vec![channel("ch0", Some("V")), channel("ch1", None)],
                vec![
                    vec![1.0f64, 1.1f64, 1.2f64].into(),
                    vec![2.0f64, 2.1f64, 2.2f64].into(),
                ],
            )
            .expect("creating sample table should succeed"),
        };

        let json = serde_json::to_string(&data).expect("serialize");
        let parsed: StreamData = serde_json::from_str(&json).expect("deserialize");

        assert_eq!(parsed.stream_id, "daq");
        assert_eq!(parsed.source.unwrap(), "labjack_t7");

        let SampleTable {
            timestamps,
            channels,
            values,
        } = parsed.samples;

        assert_eq!(timestamps, &[100, 200, 300]);
        assert_eq!(values.iter().map(|v| v.len()).sum::<usize>(), 3 * 2);
        assert!(values.iter().map(|v| v.len()).all(|len| len == 3));
        assert_eq!(channels.len(), 2);

        assert_eq!(
            channels.first().expect("column should be present").name,
            "ch0"
        );

        assert_eq!(
            channels
                .first()
                .expect("column should be present")
                .unit
                .as_deref(),
            Some("V")
        );
    }

    #[test]
    fn serialized_json_uses_expected_field_names() {
        let data = StreamData {
            stream_id: "s".to_string(),
            source: Some("t".to_string()),
            samples: SampleTable::new(vec![1], vec![channel("c", None)], vec![vec![0.0f64].into()])
                .expect("creating sample table should succeed"),
        };

        let json = serde_json::to_string(&data).expect("serialize");

        assert!(
            json.contains(r#""samples""#),
            "missing 'samples' key: {json}"
        );
        assert!(
            json.contains(r#""timestamps""#),
            "missing 'timestamps' key: {json}"
        );
        assert!(
            json.contains(r#""channels""#),
            "missing 'channels' key: {json}"
        );
    }

    #[test]
    fn deserialization_rejects_dimension_mismatches() {
        let bad_payloads = [
            (
                "mismatched row count",
                r#"{
                    "stream_id": "stream-1",
                    "samples": {
                        "timestamps": [1, 2],
                        "channels": [{"name": "ch"}],
                        "values": [[1.0]]
                    }
                }"#,
            ),
            (
                "mismatched column count",
                r#"{
                    "stream_id": "stream-1",
                    "samples": {
                        "timestamps": [1],
                        "channels": [{"name": "a"}, {"name": "b"}],
                        "values": [[1.0]]
                    }
                }"#,
            ),
        ];

        for (name, payload) in bad_payloads {
            let result = serde_json::from_str::<StreamData>(payload);
            assert!(
                result.is_err(),
                "deserializing sample table with {name} should fail"
            );
        }
    }

    #[test]
    fn scale_channel_doubles() {
        let channel = channel("voltage", Some("V"));

        let mut table = SampleTable::new(
            vec![1, 2, 3],
            vec![channel.clone()],
            vec![vec![1.0, 2.0, 3.0].into()],
        )
        .expect("creating sample table should succeed");

        table
            .scale_channel(&channel, |v| match v {
                ValueRef::Double(v) => Ok(*v * 2.0),
                _ => Err("unexpected type".to_string()),
            })
            .expect("scale_channel should succeed");

        let SampleTable { values, .. } = table;
        let series = values.first().expect("should have one series");
        assert_eq!(series.get(0), Some(ValueRef::Double(&2.0)));
        assert_eq!(series.get(1), Some(ValueRef::Double(&4.0)));
        assert_eq!(series.get(2), Some(ValueRef::Double(&6.0)));
    }

    #[test]
    fn scale_channel_integers_become_doubles() {
        let channel = channel("count", None);

        let mut table = SampleTable::new(
            vec![1, 2],
            vec![channel.clone()],
            vec![vec![10i64, 20i64].into()],
        )
        .expect("creating sample table should succeed");

        table
            .scale_channel(&channel, |v| match v {
                ValueRef::Integer(v) => Ok(*v as f64 * 0.5),
                _ => Err("unexpected type".to_string()),
            })
            .expect("scale_channel should succeed");

        let SampleTable { values, .. } = table;
        let series = values.first().expect("should have one series");
        // Integer channel should now be Double after scaling
        assert_eq!(series.get(0), Some(ValueRef::Double(&5.0)));
        assert_eq!(series.get(1), Some(ValueRef::Double(&10.0)));
    }

    #[test]
    fn scale_channel_not_found() {
        let mut table = SampleTable::new(
            vec![1],
            vec![channel("exists", None)],
            vec![vec![1.0].into()],
        )
        .expect("creating sample table should succeed");

        let missing = channel("missing", None);

        let err = table
            .scale_channel(&missing, |v| match v {
                ValueRef::Double(v) => Ok(*v),
                _ => Err("unexpected type".to_string()),
            })
            .expect_err("scale_channel should fail for missing channel");

        assert!(
            matches!(err, SampleTableError::ChannelNotFound(ref name) if name == "missing"),
            "expected ChannelNotFound error, got: {err}"
        );
    }

    #[test]
    fn scale_channel_closure_error_propagates() {
        let channel = channel("ch", None);

        let mut table = SampleTable::new(
            vec![1, 2, 3],
            vec![channel.clone()],
            vec![vec![1.0, 2.0, 3.0].into()],
        )
        .expect("creating sample table should succeed");

        const ERR_MSG: &str = "value too large";

        let err = table
            .scale_channel(&channel, |v| match v {
                ValueRef::Double(v) if *v > 1.5 => Err(ERR_MSG.to_string()),
                ValueRef::Double(v) => Ok(*v),
                _ => Err("unexpected type".to_string()),
            })
            .expect_err("scale_channel should propagate closure error");

        assert!(
            matches!(err, SampleTableError::ApplyError(ref msg) if msg.contains(ERR_MSG)),
            "expected ApplyError, got: {err}"
        );
    }
}