Caching SQL INSERTS
This commit is contained in:
parent
2fa2966aaa
commit
137ebde68b
4 changed files with 59 additions and 50 deletions
|
|
@ -2,7 +2,6 @@ WITH RECURSIVE rq_range AS (
|
|||
SELECT DISTINCT rq from cars
|
||||
),
|
||||
track_combinations AS (
|
||||
-- Extract unique human-readable track_id and track_type combinations
|
||||
SELECT DISTINCT track_id, track_type
|
||||
FROM car_track_times
|
||||
),
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ WITH RECURSIVE rq_range AS (
|
|||
SELECT DISTINCT rq from cars
|
||||
),
|
||||
track_combinations AS (
|
||||
-- Extract unique human-readable track_id and track_type combinations
|
||||
SELECT DISTINCT track_id, track_type
|
||||
FROM car_track_times
|
||||
),
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
UPDATE car_track_times
|
||||
SET time = 2000 / time
|
||||
WHERE track_id LIKE 'Test Bowl%';
|
||||
104
src/records.rs
104
src/records.rs
|
|
@ -1,5 +1,5 @@
|
|||
use reqwest::get;
|
||||
use rusqlite::{params, Connection};
|
||||
use rusqlite::{params, CachedStatement, Connection};
|
||||
use serde_json::Value;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
|
@ -39,7 +39,7 @@ async fn main() {
|
|||
async fn list_best_cars() {
|
||||
todo!()
|
||||
}
|
||||
async fn parse_car_times(rid: String, connection: &Connection) {
|
||||
async fn parse_car_times(rid: String, statement: &mut CachedStatement<'_>) {
|
||||
let file_name = rid.to_owned() + ".json";
|
||||
let local_file_path = "times/".to_owned() + file_name.as_str();
|
||||
let file_content = fs::read_to_string(local_file_path);
|
||||
|
|
@ -57,17 +57,26 @@ async fn parse_car_times(rid: String, connection: &Connection) {
|
|||
{
|
||||
// Extract time and configuration
|
||||
let mut time = details["t"].as_f64().unwrap_or(0.0);
|
||||
let down_votes = details["down"].as_array();
|
||||
|
||||
let no_down_votes = match down_votes {
|
||||
None => true,
|
||||
Some(votes) => votes.len() <= 0,
|
||||
};
|
||||
|
||||
if track_id.starts_with("testBowl") {
|
||||
time = 2000.0 / time;
|
||||
}
|
||||
|
||||
// Insert into the database
|
||||
if time > 0.0 {
|
||||
let _ = connection.execute(
|
||||
"INSERT INTO car_track_times (car_rid, track_id, track_type, time, configuration)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![rid, translate_track_name(cleaned_track_id), translate_track_type(track_type), time, config],
|
||||
);
|
||||
if time > 1.2 && no_down_votes {
|
||||
let _ = statement.execute([
|
||||
rid.as_str(),
|
||||
translate_track_name(cleaned_track_id),
|
||||
translate_track_type(track_type),
|
||||
time.to_string().as_str(),
|
||||
config,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -78,9 +87,13 @@ async fn parse_car_times(rid: String, connection: &Connection) {
|
|||
}
|
||||
async fn parse_all_times() {
|
||||
let connection = Connection::open("top_drives.db").expect("Issue opening DB file!");
|
||||
let mut statement = connection.prepare_cached("INSERT INTO car_track_times (car_rid, track_id, track_type, time, configuration) VALUES (?1, ?2, ?3, ?4, ?5)").expect("Could not prepare statement!");
|
||||
for car in get_cars_from_db() {
|
||||
parse_car_times(car, &connection).await;
|
||||
parse_car_times(car, &mut statement).await;
|
||||
connection.flush_prepared_statement_cache();
|
||||
}
|
||||
|
||||
let _ = connection.execute("DELETE FROM car_track_times WHERE time < 1.2;", ());
|
||||
}
|
||||
fn parse_cars() {
|
||||
let connection = Connection::open("top_drives.db").expect("Issue opening DB file!");
|
||||
|
|
@ -90,40 +103,40 @@ fn parse_cars() {
|
|||
serde_json::from_str(&fs::read_to_string(cars_file).unwrap()).unwrap();
|
||||
|
||||
for car in cars_data {
|
||||
let _ = connection.execute(
|
||||
"INSERT OR IGNORE INTO cars
|
||||
if let Err(e) = connection.execute("INSERT OR IGNORE INTO cars
|
||||
(class, rq, onlyName, brand, year, abs, tcs, clearance, country, topSpeed, acel, hand,
|
||||
drive, tyres, mra, weight, name, rid, prize, bodyTypes, fuel, seats, engine, tags, guid, brake)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20, ?21, ?22, ?23, ?24, ?25, ?26)",
|
||||
params![
|
||||
car["class"].as_str(),
|
||||
car["rq"].as_i64(),
|
||||
car["onlyName"].as_str(),
|
||||
car["brand"].as_str(),
|
||||
car["year"].as_i64(),
|
||||
car["abs"].as_bool(),
|
||||
car["tcs"].as_bool(),
|
||||
car["clearance"].as_str(),
|
||||
car["country"].as_str(),
|
||||
car["topSpeed"].as_i64(),
|
||||
car["acel"].as_f64(),
|
||||
car["hand"].as_i64(),
|
||||
car["drive"].as_str(),
|
||||
car["tyres"].as_str(),
|
||||
car["mra"].as_f64(),
|
||||
car["weight"].as_i64(),
|
||||
car["name"].as_str(),
|
||||
car["rid"].as_str(),
|
||||
car["prize"].as_bool(),
|
||||
serde_json::to_string(&car["bodyTypes"]).unwrap(),
|
||||
car["fuel"].as_str(),
|
||||
car["seats"].as_str(),
|
||||
car["engine"].as_str(),
|
||||
serde_json::to_string(&car["tags"]).unwrap(),
|
||||
car["guid"].as_str(),
|
||||
car["brake"].as_str(),
|
||||
],
|
||||
);
|
||||
drive, tyres, mra, weight, name, rid, prize, bodyTypes, fuel, seats, engine, tags, guid, brake)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20, ?21, ?22, ?23, ?24, ?25, ?26);",
|
||||
params![
|
||||
car["class"].as_str().unwrap(),
|
||||
car["rq"].as_u64().unwrap(),
|
||||
car["onlyName"].as_str().unwrap(),
|
||||
car["brand"].as_str().unwrap(),
|
||||
car["year"].as_u64().unwrap(),
|
||||
car["abs"].as_bool().unwrap(),
|
||||
car["tcs"].as_bool().unwrap(),
|
||||
car["clearance"].as_str().unwrap(),
|
||||
car["country"].as_str().unwrap(),
|
||||
car["topSpeed"].as_u64().unwrap(),
|
||||
car["acel"].as_f64().unwrap_or_default(),
|
||||
car["hand"].as_u64().unwrap(),
|
||||
car["drive"].as_str().unwrap(),
|
||||
car["tyres"].as_str().unwrap(),
|
||||
car["mra"].as_f64().unwrap_or_default(),
|
||||
car["weight"].as_u64().unwrap(),
|
||||
car["name"].as_str().unwrap(),
|
||||
car["rid"].as_str().unwrap(),
|
||||
car["prize"].as_bool().unwrap(),
|
||||
Some(serde_json::to_string(&car["bodyTypes"]).unwrap().as_str()).unwrap_or_default(),
|
||||
car["fuel"].as_str().unwrap(),
|
||||
car["seats"].as_u64().unwrap_or_default(),
|
||||
car["engine"].as_str().unwrap(),
|
||||
Some(serde_json::to_string(&car["tags"]).unwrap().as_str()).unwrap_or_default(),
|
||||
car["guid"].as_str().unwrap(),
|
||||
car["brake"].as_str().unwrap(),
|
||||
]) {
|
||||
eprintln!("Insertion error: {}", e);
|
||||
};
|
||||
}
|
||||
}
|
||||
fn split_prefix_postfix<'a>(input: &'a str, delimiter: &'a str) -> Option<(&'a str, &'a str)> {
|
||||
|
|
@ -214,10 +227,10 @@ fn init_db() {
|
|||
);
|
||||
}
|
||||
async fn get_tracks_file() {
|
||||
get_file("https://raw.githubusercontent.com/TiagoXavi/topdrives-records/refs/heads/main/src/database/tracks_repo.json","tracks_repo.json").await;
|
||||
get_file("https://github.com/TiagoXavi/topdrives-records/raw/refs/heads/main/src/database/tracks_repo.json","tracks_repo.json").await;
|
||||
}
|
||||
async fn get_cars_file() {
|
||||
get_file("https://github.com/TiagoXavi/topdrives-records/raw/refs/heads/main/src/database/cars_final_PL24_2-fixed.json", "cars_repo.json").await;
|
||||
get_file("https://github.com/TiagoXavi/topdrives-records/raw/refs/heads/main/src/database/cars_final_PL25-fixed.json", "cars_repo.json").await;
|
||||
}
|
||||
async fn get_file(file_url: &str, local_file_path: &str) {
|
||||
let _ = fs::remove_file(local_file_path);
|
||||
|
|
@ -344,10 +357,11 @@ fn translate_track_name(track_code: &str) -> &str {
|
|||
"miBeachDrag" => "Miami Beach Drag",
|
||||
"mnGforce" => "Monaco G-Force Test",
|
||||
"mnHairpin" => "Monaco Hairpin",
|
||||
"mnCityNarrow" => "Monaco Narrow Streets",
|
||||
"mnNarrowStreets" => "Monaco Narrow Streets",
|
||||
"mnCity" => "Monaco Streets",
|
||||
"mnCityLong" => "Monaco Streets Long",
|
||||
"moto" => "Motocross Track",
|
||||
"mtButte" => "mtButte",
|
||||
"mtHairpin" => "Mountain Hairpin",
|
||||
"mtHill" => "Mountain Hill Climb",
|
||||
"mtIncline" => "Mountain Incline Road",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue