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

use_prelude!();

use chrono::{DateTime, Utc};

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

pub struct TimeSeries {
    pub(super) ditto: Arc<ffi_sdk::BoxedDitto>,
    pub(super) ts_name: char_p::Box,
}

impl 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 = unsafe {
            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)]
pub struct DittoTimestamp {
    pub epoch_sec: u64,
    pub nanos: u32,
}

impl DittoTimestamp {
    /// Construct a Timestamp key for an Event in a Timeseries from
    /// epoch seconds and nanosecond offset
    pub 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)
    }

    pub fn epoch_bytes(&self) -> [u8; 8] {
        self.epoch_sec.to_be_bytes()
    }

    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)
    }
}