From 431ea2814b22dc4fd5fe2b7a331b8d8f2b61c8a6 Mon Sep 17 00:00:00 2001 From: Jan-Peter von Hunnius Date: Fri, 28 Mar 2025 16:21:07 +0100 Subject: [PATCH] Implemented search for best car for track --- src/records.rs | 71 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/src/records.rs b/src/records.rs index 2a1223b..6e17ee0 100644 --- a/src/records.rs +++ b/src/records.rs @@ -1,6 +1,6 @@ use reqwest::get; -use rusqlite::{params, CachedStatement, Connection}; use rusqlite::backup::Backup; +use rusqlite::{params, CachedStatement, Connection}; use serde_json::Value; use std::env; use std::fs; @@ -20,14 +20,17 @@ async fn main() { get_cars_file().await; get_tracks_file().await; - let mut connection = Connection::open_in_memory().expect("Issue opening in-memory DB!"); + let mut connection = + Connection::open_in_memory().expect("Issue opening in-memory DB!"); init_db(&mut connection); parse_cars(&mut connection); let _ = fs::remove_file("top_drives.db"); - let mut file = Connection::open("top_drives.db").expect("Issue opening DB file!"); - let backup = Backup::new(&connection, &mut file).expect("Could not create backup!"); + let mut file = + Connection::open("top_drives.db").expect("Issue opening DB file!"); + let backup = + Backup::new(&connection, &mut file).expect("Could not create backup!"); backup.step(-1).expect("Could not create backup!"); // -1 copies all pages at once drop(backup); connection.close().unwrap(); @@ -37,10 +40,13 @@ async fn main() { fetch_times().await; } "parse" => { - let mut file = Connection::open("top_drives.db").expect("Issue opening DB file!"); - let mut connection = Connection::open_in_memory().expect("Issue opening in-memory DB!"); + let mut file = + Connection::open("top_drives.db").expect("Issue opening DB file!"); + let mut connection = + Connection::open_in_memory().expect("Issue opening in-memory DB!"); - let backup = Backup::new(&file, &mut connection).expect("Could not create backup!"); + let backup = + Backup::new(&file, &mut connection).expect("Could not create backup!"); backup.step(-1).expect("Could not create backup!"); // -1 copies all pages at once drop(backup); file.close().unwrap(); @@ -49,7 +55,8 @@ async fn main() { let _ = fs::remove_file("top_drives.db"); file = Connection::open("top_drives.db").expect("Issue opening DB file!"); - let backup = Backup::new(&connection, &mut file).expect("Could not create backup!"); + let backup = + Backup::new(&connection, &mut file).expect("Could not create backup!"); backup.step(-1).expect("Could not create backup!"); // -1 copies all pages at once drop(backup); @@ -63,12 +70,13 @@ async fn main() { list_details().await; } "track" => { - if args.get(3).is_none() { - println!("Track name and RQ limit required."); + if args.get(4).is_none() { + println!("Track name, condition and RQ limit required."); } else { let track = args.get(2).unwrap(); - let rq = args.get(3).unwrap(); - //TODO: Get best car for track and RQ limit + let condition = args.get(3).unwrap(); + let rq = args.get(4).unwrap(); + print_best_car(track, condition, rq); } } &_ => { @@ -78,6 +86,35 @@ async fn main() { } } } + +fn print_best_car(track: &String, condition: &String, rq: &String) { + let connection = Connection::open("top_drives.db").expect("Issue opening DB file!"); + + let mut stmt = connection + .prepare( + r#" + SELECT rq, brand, onlyName, year, configuration, time + FROM car_track_times t + JOIN cars c ON t.car_rid = c.rid + WHERE track_id = ? + AND track_type = ? + AND rq <= ? + ORDER BY CAST(t.time AS DECIMAL) ASC + LIMIT 1;"# + ).expect("Could not prepare query!"); + + let mut rows = stmt.query(params!(track, condition, rq)).expect("Could not execute query!"); + while let Ok(Some(row)) = rows.next() { + let rq :u8 = row.get(0).expect("Cannot extract RQ"); + let brand: String = row.get(1).expect("Cannot extract RQ"); + let only_name: String = row.get(2).expect("Cannot extract RQ"); + let year: u16 = row.get(3).expect("Cannot extract RQ"); + let configuration: String = row.get(4).expect("Cannot extract RQ"); + let time: f32 = row.get(5).expect("Cannot extract RQ"); + println!("{} {} {} {} ({}): {}", rq, brand, only_name, year, configuration, time ); + } +} + async fn list_details() { let file = Connection::open("top_drives.db").expect("Issue opening DB file!"); let mut connection = Connection::open_in_memory().expect("Issue opening in-memory DB!"); @@ -85,7 +122,7 @@ async fn list_details() { let backup = Backup::new(&file, &mut connection).expect("Could not create backup!"); backup.step(-1).expect("Could not create backup!"); // -1 copies all pages at once drop(backup); - drop (file); + drop(file); let stmt = connection.prepare( r#" WITH RECURSIVE rq_range AS ( @@ -124,7 +161,8 @@ async fn list_details() { ); let mut result = stmt.expect("Query failed!"); - let rows = result.query_map([], |row| { + let rows = result + .query_map([], |row| { Ok(( row.get::<_, i32>(0)?, row.get::<_, String>(1)?, @@ -159,7 +197,7 @@ async fn list_best_cars() { let backup = Backup::new(&file, &mut connection).expect("Could not create backup!"); backup.step(-1).expect("Could not create backup!"); // -1 copies all pages at once drop(backup); - drop (file); + drop(file); let stmt = connection.prepare( r#" WITH RECURSIVE rq_range AS ( @@ -199,7 +237,8 @@ async fn list_best_cars() { ); let mut result = stmt.expect("Query failed!"); - let rows = result.query_map([], |row| { + let rows = result + .query_map([], |row| { Ok(( row.get::<_, i32>(0).expect("Could not assign value"), row.get::<_, String>(1).expect("Could not assign value"),