sylvia_iot_data/models/
network_uldata.rs

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