rsiot/components_config/influxdb_v2/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Тестирование:
//!
//! ```bash
//! cargo test -p rsiot-components-config --doc influxdb_v2
//! ```

use crate::message::*;

use super::LineProtocolItem;

/// Конфигурация cmp_influxdb
#[derive(Clone, Debug)]
pub struct Config<TMsg> {
    /// # Примеры
    ///
    /// ```ignore
    /// host: String::from("influxdb"),
    /// ```
    pub host: String,

    /// Порт базы данных
    pub port: u16,

    /// Огранизация
    pub org: String,

    /// Bucket
    pub bucket: String,

    /// Токен
    pub token: String,

    /// Функция преобразования сообщения в строки протокола InfluxDB
    ///
    /// # Примеры
    ///
    /// ## Заглушка
    ///
    /// ```rust
    /// # use rsiot_components_config::influxdb_v2 as cmp_influxdb;
    /// # // insert from tests::stub
    /// # use rsiot_messages_core::example_message::*;
    /// # cmp_influxdb::Config::<Custom> {
    /// #     host: String::from("influxdb"),
    /// #     port: 8086,
    /// #     org: String::from("org"),
    /// #     bucket: String::from("bucket"),
    /// #     token: String::from("token"),
    /// fn_input: |_| None,
    /// # };
    /// ```
    ///
    /// ## Сохранение Custom
    ///
    /// ```rust
    /// # use rsiot_components_config::influxdb_v2 as cmp_influxdb;
    /// # // start tests::fn_input
    /// # use rsiot_messages_core::{example_message::*, *};
    /// # cmp_influxdb::Config::<Custom> {
    /// #     host: String::from("influxdb"),
    /// #     port: 8086,
    /// #     org: String::from("org"),
    /// #     bucket: String::from("bucket"),
    /// #     token: String::from("token"),
    /// fn_input: |msg: &Message<Custom>| {
    ///     let value = match &msg.data {
    ///         MsgData::Custom(data) => match data {
    ///             Custom::ValueInstantF64(data) => {
    ///                 cmp_influxdb::ValueType::f64(*data)
    ///             }
    ///             _ => return None,
    ///         },
    ///         _ => return None,
    ///     };
    ///     let line = cmp_influxdb::LineProtocolItem::new(&msg.key, value, &msg.ts);
    ///     Some(vec![line])
    /// },
    /// # };
    /// # // end
    /// ```
    pub fn_input: fn(&Message<TMsg>) -> Option<Vec<LineProtocolItem>>,
}

#[cfg(test)]
mod test {
    use super::super::super::influxdb_v2 as cmp_influxdb;

    #[test]
    fn stub() {
        use crate::message::example_message::*;
        let _ = cmp_influxdb::Config::<Custom> {
            host: String::from("influxdb"),
            port: 8086,
            org: String::from("org"),
            bucket: String::from("bucket"),
            token: String::from("token"),
            fn_input: |_| None,
        };
    }

    #[test]
    fn fn_input() {
        use crate::message::{example_message::*, *};
        let _ = cmp_influxdb::Config::<Custom> {
            host: String::from("influxdb"),
            port: 8086,
            org: String::from("org"),
            bucket: String::from("bucket"),
            token: String::from("token"),
            fn_input: |msg: &Message<Custom>| {
                let value = match &msg.data {
                    MsgData::Custom(Custom::ValueInstantF64(data)) => {
                        cmp_influxdb::ValueType::f64(*data)
                    }
                    _ => return None,
                };
                let line = cmp_influxdb::LineProtocolItem::new(&msg.key, value, &msg.ts);
                Some(vec![line])
            },
        };
    }
}