dw_models/
bucket.rs

1use chrono::DateTime;
2use chrono::Utc;
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use serde_json::map::Map;
6use serde_json::value::Value;
7use std::collections::HashMap;
8
9use crate::Event;
10use crate::TryVec;
11
12#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
13pub struct Bucket {
14    #[serde(skip)]
15    pub bid: Option<i64>,
16    #[serde(default)]
17    pub id: String,
18    #[serde(rename = "type")] /* type is a reserved Rust keyword */ pub _type: String,
19    pub client: String,
20    pub hostname: String,
21    pub created: Option<DateTime<Utc>>,
22    #[serde(default)]
23    pub data: Map<String, Value>,
24    #[serde(default, skip_deserializing)]
25    pub metadata: BucketMetadata,
26    // Events should only be "Some" during import/export
27    // It's using a TryVec to discard only the events which were failed to be serialized so only a
28    // few events are being dropped during import instead of failing the whole import
29    pub events: Option<TryVec<Event>>,
30    pub last_updated: Option<DateTime<Utc>>, // TODO: Should probably be moved into metadata field
31}
32
33#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
34pub struct BucketMetadata {
35    #[serde(default)]
36    pub start: Option<DateTime<Utc>>,
37    pub end: Option<DateTime<Utc>>,
38}
39
40impl Default for BucketMetadata {
41    fn default() -> BucketMetadata {
42        BucketMetadata {
43            start: None,
44            end: None,
45        }
46    }
47}
48
49#[derive(Serialize, Deserialize, JsonSchema, Clone)]
50pub struct BucketsExport {
51    pub buckets: HashMap<String, Bucket>,
52}
53
54#[test]
55fn test_bucket() {
56    let b = Bucket {
57        bid: None,
58        id: "id".to_string(),
59        _type: "type".to_string(),
60        client: "client".to_string(),
61        hostname: "hostname".to_string(),
62        created: None,
63        data: json_map! {},
64        metadata: BucketMetadata::default(),
65        events: None,
66        last_updated: None,
67    };
68    debug!("bucket: {:?}", b);
69}