aquarium_control/recorder/
data_injection.rs

1/* Copyright 2024 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9use crate::database::sql_interface_data::SqlInterfaceData;
10use crate::recorder::recorder_data_frame::RecorderDataFrame;
11use crate::utilities::logger::log_error_chain;
12
13/// Trait for the execution of data logging to SQL database
14/// This trait allows running the main control with a mock implementation for testing.
15pub trait DataInjectionTrait {
16    /// Transfers the data provided as parameter to SQL database
17    /// # Arguments
18    /// * `data_frame` - Data to be transferred to SQL database
19    /// * `sql_interface_data` - Interface to SQL database
20    fn inject_data(
21        &mut self,
22        data_frame: RecorderDataFrame,
23        sql_interface_data: &mut SqlInterfaceData,
24    );
25}
26
27/// Implements the `DataInjectionTrait` for concrete data logging to an SQL database.
28///
29/// This struct serves as the practical component for transferring `RecorderDataFrame`
30/// instances to the configured SQL database.
31pub struct DataInjection;
32
33impl DataInjectionTrait for DataInjection {
34    /// Transfers a `RecorderDataFrame` to the SQL database for logging.
35    ///
36    /// This method serves as the concrete implementation for injecting collected
37    /// data frames into the database. It attempts to write the provided `data_frame`
38    /// using the `sql_interface_data` and logs any errors that occur during the process.
39    ///
40    /// # Arguments
41    /// * `data_frame` - The `RecorderDataFrame` containing the data to be persisted in the SQL database.
42    ///   This is passed by value, meaning ownership is transferred to this function.
43    /// * `sql_interface_data` - A mutable reference to the `SqlInterfaceData` instance,
44    ///   which handles the actual database write operation.
45    fn inject_data(
46        &mut self,
47        data_frame: RecorderDataFrame,
48        sql_interface_data: &mut SqlInterfaceData,
49    ) {
50        match sql_interface_data.write_data_frame_to_database(&data_frame) {
51            Ok(()) => { /* do nothing */ }
52            Err(e) => {
53                log_error_chain(
54                    module_path!(),
55                    "Error occurred when injecting data into database.",
56                    e,
57                );
58            }
59        };
60    }
61}