aquarium_control/water/refill_stats_data.rs
1use std::fmt;
2
3/// Contains the data retrieved from the SQL database.
4/// It is also used to communicate data to be updated in the SQL database.
5pub struct RefillStatsData {
6 /// Duration since the last refill operation in seconds
7 pub duration_since_last_refill: u64,
8
9 /// Total volume of refills in the last 24 hours in liters
10 pub refill_volume_last_24h: f64,
11
12 /// Count of refills in the last 24 hours
13 pub refill_count_last_24h: u64,
14
15 /// Total volume of refills in the last hour in liters
16 pub refill_volume_last_hour: f64,
17
18 /// Count of refills in the last hour
19 pub refill_count_last_hour: u64,
20}
21
22impl RefillStatsData {
23 /// Creates a new `RefillStatsData` instance, with all its fields initialized to zero.
24 ///
25 /// This constructor provides a default, empty state for storing historical refill data,
26 /// suitable for initialization before actual values are retrieved from the database.
27 ///
28 /// # Returns
29 /// A new `RefillStatsData` struct with all numeric fields set to `0` or `0.0`.
30 pub fn new() -> RefillStatsData {
31 RefillStatsData {
32 duration_since_last_refill: 0,
33 refill_volume_last_24h: 0.0,
34 refill_count_last_24h: 0,
35 refill_volume_last_hour: 0.0,
36 refill_count_last_hour: 0,
37 }
38 }
39}
40
41impl fmt::Display for RefillStatsData {
42 /// Provides a multi-line, human-readable output of the struct's values for debugging purposes.
43 ///
44 /// This implementation formats all fields of the `RefillStatsData` struct into a detailed
45 /// string representation, summarizing historical refill statistics such as duration since
46 /// the last refill, and total volume and count for both the last 24 hours and the last hour.
47 /// This is particularly useful for inspecting refill data during development and logging.
48 ///
49 /// # Arguments
50 /// * `f` - A mutable reference to the formatter, as required by the `fmt::Display` trait.
51 ///
52 /// # Returns
53 /// An `Ok(())` on successful formatting.
54 ///
55 /// # Errors
56 /// Returns an `Err` containing a `std::fmt::Error` if an I/O error occurs
57 /// while writing to the formatter.
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 write!(
60 f,
61 " {}: Duration since last refill is {} seconds.\n \
62 Refill: volume of last 24h is {} L.\n Refill: Count of last 24h is {}.\n \
63 Refill: volume of last hour is {} L.\n Refill: Count of last hour is {}.",
64 module_path!(),
65 self.duration_since_last_refill,
66 self.refill_volume_last_24h,
67 self.refill_count_last_24h,
68 self.refill_volume_last_hour,
69 self.refill_count_last_hour
70 )
71 }
72}