sylvia_iot_data/models/
application_uldata.rs

1//! Traits and structs for application uplink data.
2
3use std::error::Error as StdError;
4
5use async_trait::async_trait;
6use chrono::{DateTime, Utc};
7use serde_json::{Map, Value};
8
9/// The item content.
10#[derive(Debug, PartialEq)]
11pub struct ApplicationUlData {
12    pub data_id: String,
13    pub proc: DateTime<Utc>,
14    pub publish: DateTime<Utc>,
15    pub unit_code: Option<String>,
16    pub network_code: String,
17    pub network_addr: String,
18    pub unit_id: String,
19    pub device_id: String,
20    pub time: DateTime<Utc>,
21    pub profile: String,
22    pub data: String,
23    pub extension: Option<Map<String, Value>>,
24}
25
26/// The sort keys for the list operation.
27pub enum SortKey {
28    Proc,
29    Pub,
30    Time,
31    NetworkCode,
32    NetworkAddr,
33}
34
35/// The sort condition for the list operation.
36pub struct SortCond {
37    pub key: SortKey,
38    pub asc: bool,
39}
40
41/// The list operation options.
42pub struct ListOptions<'a> {
43    /// The query conditions.
44    pub cond: &'a ListQueryCond<'a>,
45    /// The data offset.
46    pub offset: Option<u64>,
47    /// The maximum number to query.
48    pub limit: Option<u64>,
49    /// The sort conditions.
50    pub sort: Option<&'a [SortCond]>,
51    /// The maximum number items one time the `list()` returns.
52    ///
53    /// Use cursors until reaching `limit` or all data.
54    pub cursor_max: Option<u64>,
55}
56
57/// The query condition to delete item(s).
58#[derive(Default)]
59pub struct QueryCond<'a> {
60    pub unit_id: Option<&'a str>,
61    pub device_id: Option<&'a str>,
62    pub proc_gte: Option<DateTime<Utc>>,
63    pub proc_lte: Option<DateTime<Utc>>,
64}
65
66/// The query condition for the list operation.
67#[derive(Default)]
68pub struct ListQueryCond<'a> {
69    /// To get the specified unit.
70    pub unit_id: Option<&'a str>,
71    /// To get the specified device.
72    pub device_id: Option<&'a str>,
73    /// To get the specified device's network code.
74    pub network_code: Option<&'a str>,
75    /// To get the specified device's network address.
76    pub network_addr: Option<&'a str>,
77    /// To get the specified device/data profile.
78    pub profile: Option<&'a str>,
79    /// To get data greater than and equal to the specified `proc` time.
80    pub proc_gte: Option<DateTime<Utc>>,
81    /// To get data less than and equal to the specified `proc` time.
82    pub proc_lte: Option<DateTime<Utc>>,
83    /// To get data greater than and equal to the specified `pub` time.
84    pub pub_gte: Option<DateTime<Utc>>,
85    /// To get data less than and equal to the specified `pub` time.
86    pub pub_lte: Option<DateTime<Utc>>,
87    /// To get data greater than and equal to the specified `time` time.
88    pub time_gte: Option<DateTime<Utc>>,
89    /// To get data less than and equal to the specified `time` time.
90    pub time_lte: Option<DateTime<Utc>>,
91}
92
93/// Model operations.
94#[async_trait]
95pub trait ApplicationUlDataModel: Sync {
96    /// To create and initialize the table/collection.
97    async fn init(&self) -> Result<(), Box<dyn StdError>>;
98
99    /// To get item count for the query condition.
100    ///
101    /// **Note**: this may take a long time.
102    async fn count(&self, cond: &ListQueryCond) -> Result<u64, Box<dyn StdError>>;
103
104    /// To get item list. The maximum number of returned items will be controlled by the
105    /// `cursor_max` of the list option.
106    ///
107    /// For the first time, `cursor` MUST use `None`. If one cursor is returned, it means that
108    /// there are more items to get. Use the returned cursor to get more data items.
109    ///
110    /// **Note**: using cursors is recommended to prevent exhausting memory.
111    async fn list(
112        &self,
113        opts: &ListOptions,
114        cursor: Option<Box<dyn Cursor>>,
115    ) -> Result<(Vec<ApplicationUlData>, Option<Box<dyn Cursor>>), Box<dyn StdError>>;
116
117    /// To add an item.
118    async fn add(&self, data: &ApplicationUlData) -> Result<(), Box<dyn StdError>>;
119
120    /// To delete one or more items.
121    async fn del(&self, cond: &QueryCond) -> Result<(), Box<dyn StdError>>;
122}
123
124/// The operations for cursors.
125///
126/// All functions are private to let programs to pass them as arguments directly without any
127/// operation.
128#[async_trait]
129pub trait Cursor: Send {
130    async fn try_next(&mut self) -> Result<Option<ApplicationUlData>, Box<dyn StdError>>;
131
132    fn offset(&self) -> u64;
133}
134
135/// The expiration time of the data in seconds.
136pub const EXPIRES: i64 = 100 * 86400;