rsiot/components_config/influxdb3/
config.rs

1//! Тестирование:
2//!
3//! ```bash
4//! cargo test -p rsiot-components-config --doc influxdb_v2
5//! ```
6
7use std::time::Duration;
8
9use crate::message::*;
10
11use super::LineProtocolItem;
12
13pub type FnInput<TMsg> = fn(&Message<TMsg>) -> Option<Vec<LineProtocolItem>>;
14
15/// Конфигурация cmp_influxdb
16#[derive(Clone, Debug)]
17pub struct Config<TMsg> {
18    /// # Примеры
19    ///
20    /// ```ignore
21    /// host: String::from("influxdb"),
22    /// ```
23    pub host: String,
24
25    /// Порт базы данных
26    pub port: u16,
27
28    /// База данных
29    pub database: String,
30
31    /// Периодичность отправки данных для сохранения в базе данных
32    pub send_period: Duration,
33
34    /// Функция преобразования сообщения в строки протокола InfluxDB
35    pub fn_input: FnInput<TMsg>,
36}
37
38#[cfg(test)]
39mod test {
40    use super::super::super::influxdb_v2 as cmp_influxdb;
41
42    #[test]
43    fn stub() {
44        use crate::message::example_message::*;
45        let _ = cmp_influxdb::Config::<Custom> {
46            host: String::from("influxdb"),
47            port: 8086,
48            org: String::from("org"),
49            bucket: String::from("bucket"),
50            token: String::from("token"),
51            fn_input: |_| None,
52        };
53    }
54
55    #[test]
56    fn fn_input() {
57        use crate::message::{example_message::*, *};
58        let _ = cmp_influxdb::Config::<Custom> {
59            host: String::from("influxdb"),
60            port: 8086,
61            org: String::from("org"),
62            bucket: String::from("bucket"),
63            token: String::from("token"),
64            fn_input: |msg: &Message<Custom>| {
65                let value = match &msg.data {
66                    MsgData::Custom(Custom::ValueInstantF64(data)) => {
67                        cmp_influxdb::ValueType::f64(*data)
68                    }
69                    _ => return None,
70                };
71                let line = cmp_influxdb::LineProtocolItem::new(&msg.key, value, &msg.ts);
72                Some(vec![line])
73            },
74        };
75    }
76}