Compare commits
10 Commits
7f9b87c377
...
aab2a634ef
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aab2a634ef | ||
|
|
7d5a94f4bb | ||
|
|
ca9132e9ee | ||
|
|
92010cc9cd | ||
|
|
10aa0b1818 | ||
|
|
635696219c | ||
|
|
af60eeb00d | ||
|
|
01749e299f | ||
|
|
44956a2657 | ||
|
|
f01ab3aa6f |
@@ -1,7 +1,9 @@
|
||||
[package]
|
||||
name = "rust_agreagator"
|
||||
version = "0.1.0"
|
||||
authors = ["Romulus21 <romain@delanoe.pro>"]
|
||||
edition = "2021"
|
||||
description = "Script computing the average of values from a sensor in MySQL database."
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4"
|
||||
|
||||
84
src/main.rs
84
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<dyn std::error::Error>> {
|
||||
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<String> = env::args().collect();
|
||||
if args.len() == 1 {
|
||||
let date = (Utc::now() - Duration::days(7)).naive_utc().date();
|
||||
@@ -74,45 +79,75 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let date = NaiveDate::parse_from_str(&args[1], "%Y-%m-%d").unwrap();
|
||||
let _ = aggregate_values(&mut conn, date);
|
||||
}
|
||||
dbg!(args);
|
||||
//dbg!(args);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn aggregate_values(conn: &mut PooledConn, date: NaiveDate) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let start_time = Utc.with_ymd_and_hms(date.year(), date.month(), date.day(), 00, 00, 00).unwrap() - Duration::minutes(15);
|
||||
let end_time = start_time + Duration::days(1);
|
||||
|
||||
fn aggregate_values(
|
||||
conn: &mut PooledConn,
|
||||
date: NaiveDate,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
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(),
|
||||
"temperature".to_string(),
|
||||
),
|
||||
Sensor::new(
|
||||
"maison".to_string(),
|
||||
"bureau".to_string(),
|
||||
"humidite".to_string(),
|
||||
),
|
||||
Sensor::new(
|
||||
"maison".to_string(),
|
||||
"bureau".to_string(),
|
||||
"pression".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 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(29) + 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<Row> = conn.query(query)?;
|
||||
|
||||
let donnees: Vec<Donnee> = selected_rows.into_iter().map(Donnee::from).collect();
|
||||
|
||||
let mut value: i32 = 0;
|
||||
let mut count = 0;
|
||||
let mut value: f32 = 0.0;
|
||||
let mut count: f32 = 0.0;
|
||||
let mut ids: Vec<String> = Vec::new();
|
||||
for donnee in donnees {
|
||||
value = value + donnee.donnee.parse::<i32>().unwrap();
|
||||
count = count + 1;
|
||||
value += donnee.donnee.parse::<f32>().unwrap();
|
||||
count += 1.0;
|
||||
ids.push(donnee.id.to_string());
|
||||
}
|
||||
|
||||
if count > 1 {
|
||||
if count > 1.0 {
|
||||
let average = value / count;
|
||||
let entry_date = current_time + Duration::minutes(15);
|
||||
let entry_date = current_time + (Duration::minutes(average_duration) / 2);
|
||||
let donnee = Donnee {
|
||||
id: 0,
|
||||
service: sensor.service.to_string(),
|
||||
@@ -124,10 +159,11 @@ fn aggregate_values(conn: &mut PooledConn, date: NaiveDate) -> Result<(), Box<dy
|
||||
|
||||
let _ = insert_value(conn, donnee);
|
||||
|
||||
let query = "DELETE FROM donnees WHERE id IN (".to_owned() + &*ids.join(", ") + ");";
|
||||
let query =
|
||||
"DELETE FROM donnees WHERE id IN (".to_owned() + &*ids.join(", ") + ");";
|
||||
let _: Vec<Row> = conn.query(query)?;
|
||||
}
|
||||
current_time = current_time + Duration::minutes(30);
|
||||
current_time += Duration::minutes(average_duration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user