Skip to main content

nodedb_types/timeseries/
sync.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Sync delta and WAL payload types.
4
5use serde::{Deserialize, Serialize};
6
7use super::series::{LiteId, SeriesId, SeriesKey};
8
9/// Wire format for Lite→Origin timeseries delta exchange.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct TimeseriesDelta {
12    /// Source Lite instance ID (CUID2).
13    pub source_id: LiteId,
14    /// Series identifier (metric + tags hash).
15    pub series_id: SeriesId,
16    /// Canonical series key for the source.
17    pub series_key: SeriesKey,
18    /// Minimum timestamp in this block.
19    pub min_ts: i64,
20    /// Maximum timestamp in this block.
21    pub max_ts: i64,
22    /// Gorilla-encoded compressed samples.
23    pub encoded_block: Vec<u8>,
24    /// Number of samples in the block.
25    pub sample_count: u64,
26}
27
28/// WAL record payload for a timeseries log batch.
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct LogWalBatch {
31    /// Collection name.
32    pub collection: String,
33    /// Batch of log entries: (series_id, timestamp_ms, data).
34    pub entries: Vec<(SeriesId, i64, Vec<u8>)>,
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn timeseries_delta_serialization() {
43        let delta = TimeseriesDelta {
44            source_id: "clxyz1234test".into(),
45            series_id: 12345,
46            series_key: SeriesKey::new("cpu", vec![("host".into(), "prod".into())]),
47            min_ts: 1000,
48            max_ts: 2000,
49            encoded_block: vec![1, 2, 3, 4],
50            sample_count: 100,
51        };
52        let json = sonic_rs::to_string(&delta).unwrap();
53        let back: TimeseriesDelta = sonic_rs::from_str(&json).unwrap();
54        assert_eq!(back.source_id, "clxyz1234test");
55        assert_eq!(back.series_id, 12345);
56        assert_eq!(back.sample_count, 100);
57    }
58}