sylvia_iot_data/models/
coremgr_opdata.rs

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