sylvia_iot_data/models/
application_dldata.rs1use std::error::Error as StdError;
4
5use async_trait::async_trait;
6use chrono::{DateTime, Utc};
7use serde_json::{Map, Value};
8
9#[derive(Debug, PartialEq)]
11pub struct ApplicationDlData {
12 pub data_id: String,
13 pub proc: DateTime<Utc>,
14 pub resp: Option<DateTime<Utc>>,
15 pub status: i32,
16 pub unit_id: String,
17 pub device_id: Option<String>,
18 pub network_code: Option<String>,
19 pub network_addr: Option<String>,
20 pub profile: String,
21 pub data: String,
22 pub extension: Option<Map<String, Value>>,
23}
24
25pub enum SortKey {
27 Proc,
28 Resp,
29 NetworkCode,
30 NetworkAddr,
31}
32
33pub struct SortCond {
35 pub key: SortKey,
36 pub asc: bool,
37}
38
39pub struct ListOptions<'a> {
41 pub cond: &'a ListQueryCond<'a>,
43 pub offset: Option<u64>,
45 pub limit: Option<u64>,
47 pub sort: Option<&'a [SortCond]>,
49 pub cursor_max: Option<u64>,
53}
54
55#[derive(Default)]
57pub struct QueryCond<'a> {
58 pub unit_id: Option<&'a str>,
59 pub device_id: Option<&'a str>,
60 pub network_code: Option<&'a str>,
61 pub network_addr: Option<&'a str>,
62 pub proc_gte: Option<DateTime<Utc>>,
63 pub proc_lte: Option<DateTime<Utc>>,
64}
65
66#[derive(Default)]
68pub struct ListQueryCond<'a> {
69 pub unit_id: Option<&'a str>,
71 pub device_id: Option<&'a str>,
73 pub network_code: Option<&'a str>,
75 pub network_addr: Option<&'a str>,
77 pub profile: Option<&'a str>,
79 pub proc_gte: Option<DateTime<Utc>>,
81 pub proc_lte: Option<DateTime<Utc>>,
83 pub resp_gte: Option<DateTime<Utc>>,
85 pub resp_lte: Option<DateTime<Utc>>,
87}
88
89pub struct UpdateQueryCond<'a> {
91 pub data_id: &'a str,
93}
94
95pub struct Updates {
97 pub resp: DateTime<Utc>,
98 pub status: i32,
102}
103
104#[async_trait]
106pub trait ApplicationDlDataModel: Sync {
107 async fn init(&self) -> Result<(), Box<dyn StdError>>;
109
110 async fn count(&self, cond: &ListQueryCond) -> Result<u64, Box<dyn StdError>>;
114
115 async fn list(
123 &self,
124 opts: &ListOptions,
125 cursor: Option<Box<dyn Cursor>>,
126 ) -> Result<(Vec<ApplicationDlData>, Option<Box<dyn Cursor>>), Box<dyn StdError>>;
127
128 async fn add(&self, data: &ApplicationDlData) -> Result<(), Box<dyn StdError>>;
130
131 async fn del(&self, cond: &QueryCond) -> Result<(), Box<dyn StdError>>;
133
134 async fn update(
136 &self,
137 cond: &UpdateQueryCond,
138 updates: &Updates,
139 ) -> Result<(), Box<dyn StdError>>;
140}
141
142#[async_trait]
147pub trait Cursor: Send {
148 async fn try_next(&mut self) -> Result<Option<ApplicationDlData>, Box<dyn StdError>>;
149
150 fn offset(&self) -> u64;
151}
152
153pub const EXPIRES: i64 = 100 * 86400;