From ca9132e9ee31f9230a5dfc5a97909e9ee7204481 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Sat, 24 Aug 2024 15:36:32 +0200 Subject: [PATCH] pass clippy --- src/main.rs | 56 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index ea00f58..c6ea9a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,14 @@ extern crate serde; extern crate serde_derive; use dotenv::dotenv; -use std::env; -use mysql::*; use mysql::prelude::*; +use mysql::*; use serde::Deserialize; +use std::env; extern crate chrono; -use chrono::{Duration, Utc, NaiveDate}; use chrono::prelude::*; +use chrono::{Duration, NaiveDate, Utc}; #[derive(Debug, Deserialize)] struct Sensor { @@ -24,7 +24,7 @@ impl Sensor { Sensor { service, capteur, - type_donnee + type_donnee, } } } @@ -58,14 +58,19 @@ fn main() -> Result<(), Box> { let db_user = env::var("DB_USER").expect("DB_USER must be set"); let db_password = env::var("DB_PASSWORD").expect("DB_PASSWORD must be set"); let db_host = env::var("DB_HOST").expect("DB_HOST must be set"); - let db_port: u16 = env::var("DB_PORT").expect("DB_PORT must be set").parse().unwrap(); + let db_port: u16 = env::var("DB_PORT") + .expect("DB_PORT must be set") + .parse() + .unwrap(); let db_name = env::var("DB_NAME").expect("DB_NAME must be set"); - let url = format!("mysql://{}:{}@{}:{}/{}", db_user, db_password, db_host, db_port, db_name); + let url = format!( + "mysql://{}:{}@{}:{}/{}", + db_user, db_password, db_host, db_port, db_name + ); let pool = Pool::new(url.as_str())?; let mut conn = pool.get_conn()?; - let args: Vec = env::args().collect(); if args.len() == 1 { let date = (Utc::now() - Duration::days(7)).naive_utc().date(); @@ -79,21 +84,37 @@ fn main() -> Result<(), Box> { Ok(()) } -fn aggregate_values(conn: &mut PooledConn, date: NaiveDate) -> Result<(), Box> { +fn aggregate_values( + conn: &mut PooledConn, + date: NaiveDate, +) -> Result<(), Box> { let average_duration = 15; let sensors = [ - Sensor::new("maison".to_string(), "bureau".to_string(), "co2".to_string()), - Sensor::new("maison".to_string(), "bureau".to_string(), "tvoc".to_string()) + Sensor::new( + "maison".to_string(), + "bureau".to_string(), + "co2".to_string(), + ), + Sensor::new( + "maison".to_string(), + "bureau".to_string(), + "tvoc".to_string(), + ), ]; for sensor in &sensors { - let start_time = Utc.with_ymd_and_hms(date.year(), date.month(), date.day(), 00, 00, 00).unwrap() - (Duration::minutes(average_duration) / 2); + let start_time = Utc + .with_ymd_and_hms(date.year(), date.month(), date.day(), 00, 00, 00) + .unwrap() + - (Duration::minutes(average_duration) / 2); let end_time = start_time + Duration::days(1); let mut current_time = start_time; while current_time <= end_time { let start_time_loop = current_time.to_string(); - let end_time_loop = (current_time + Duration::minutes(average_duration - 1) + Duration::seconds(59)).to_string(); + let end_time_loop = + (current_time + Duration::minutes(average_duration - 1) + Duration::seconds(59)) + .to_string(); let query = format!("SELECT * FROM donnees WHERE service = '{}' AND capteur = '{}' AND type = '{}' AND (date_donnee BETWEEN '{}' AND '{}');", sensor.service, sensor.capteur, sensor.type_donnee, start_time_loop, end_time_loop); let selected_rows: Vec = conn.query(query)?; @@ -104,8 +125,8 @@ fn aggregate_values(conn: &mut PooledConn, date: NaiveDate) -> Result<(), Box = Vec::new(); for donnee in donnees { - value = value + donnee.donnee.parse::().unwrap(); - count = count + 1; + value += donnee.donnee.parse::().unwrap(); + count += 1; ids.push(donnee.id.to_string()); } @@ -123,10 +144,11 @@ fn aggregate_values(conn: &mut PooledConn, date: NaiveDate) -> Result<(), Box = conn.query(query)?; } - current_time = current_time + Duration::minutes(average_duration); + current_time += Duration::minutes(average_duration); } } @@ -141,4 +163,4 @@ fn insert_value(conn: &mut PooledConn, donnee: Donnee) -> Result<(), mysql::Erro conn.exec_drop(query, ())?; Ok(()) -} \ No newline at end of file +}