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
//! Time Series and Event capabilities

use_prelude!();

use chrono::{DateTime, Utc};

use crate::error::{DittoError, ErrorKind};

/// Timeseries, used by the http SDK.
pub struct TimeSeries {
    pub(super) ditto: Arc<ffi_sdk::BoxedDitto>,
    pub(super) ts_name: char_p::Box,
}

impl TimeSeries {
    /// Insert content at given time in the Timeseries.
    pub fn insert<T: ::serde::Serialize>(
        &self,
        datetime: &DateTime<Utc>,
        content: T,
    ) -> Result<(), DittoError> {
        let cbor = ::serde_cbor::to_vec(content.borrow())?;
        let ditto_time = DittoTimestamp::from(datetime);
        let status = {
            ffi_sdk::ditto_insert_timeseries_event(
                &*self.ditto,
                ditto_time.epoch_bytes(),
                ditto_time.nanoseconds(),
                self.ts_name.as_ref(),
                cbor.as_slice().into(),
                None,
            )
        };
        if status != 0 {
            Err(DittoError::from_ffi(ErrorKind::InvalidInput))
        } else {
            Ok(())
        }
    }
}

#[derive(Debug)]
/// Timestamp key for an Event in a Timeseries.
pub struct DittoTimestamp {
    /// Time reference for the timestamp.
    pub epoch_sec: u64,
    /// Offset of the timestamp in nanoseconds.
    pub nanos: u32,
}

impl DittoTimestamp {
    /// Construct a Timestamp key for an Event in a Timeseries from
    /// epoch seconds and nanosecond offset.
    pub(crate) fn new(epoch_sec: u64, nanos: u32) -> Self {
        DittoTimestamp { epoch_sec, nanos }
    }

    /// Utility to make a timestamp from the current UTC time.
    pub fn utc_now() -> Self {
        let datetime = Utc::now();
        let epoch_sec: u64 = datetime.timestamp() as u64;
        let nanos: u32 = datetime.timestamp_subsec_nanos();
        Self::new(epoch_sec, nanos)
    }

    /// Return the epoch bytes in big endian.
    pub fn epoch_bytes(&self) -> [u8; 8] {
        self.epoch_sec.to_be_bytes()
    }

    /// Return the timestamp offset in nanoseconds.
    pub fn nanoseconds(&self) -> u32 {
        self.nanos
    }
}

impl From<&::chrono::DateTime<Utc>> for DittoTimestamp {
    fn from(datetime: &::chrono::DateTime<Utc>) -> DittoTimestamp {
        let epoch_sec: u64 = datetime.timestamp() as u64;
        let nanos: u32 = datetime.timestamp_subsec_nanos();
        DittoTimestamp::new(epoch_sec, nanos)
    }
}