Skip to content

Commit

Permalink
add pulling bike stock data from DB
Browse files Browse the repository at this point in the history
  • Loading branch information
rauner committed Feb 16, 2024
1 parent e3cd979 commit edb53ed
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ axum = "*"
reqwest = { version = "*", features = ["json", "multipart"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio-postgres = "0.7.10"
urlencoding = "2.1.3"

[dev-dependencies]
tokio-test = "0.4"
Expand Down
90 changes: 88 additions & 2 deletions src/assistant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use log::info;
use std::fs;
use std::path::Path;

use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use urlencoding::encode;
use tokio_postgres::NoTls;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Expand Down Expand Up @@ -129,8 +133,88 @@ pub struct Ressources {
instructions_file_path: String,
instructions: String,
}

#[derive(Serialize)]
struct Bike {
slug: String,
category: String,
motor: String,
frame_size_code: String,
rider_height_min: i32,
rider_height_max: i32,
price: i32,
color: String,
}
impl Ressources {
pub async fn bikes_db(&self) -> Result<(), AssistantError> {
// Retrieve database credentials from environment variables
let host = env::var("DB_HOST")
.map_err(|_| AssistantError::DatabaseError("DB_HOST environment variable not set".to_string()))?;
let port = env::var("DB_PORT")
.map_err(|_| AssistantError::DatabaseError("DB_PORT environment variable not set".to_string()))?;
let dbname = env::var("DB_NAME")
.map_err(|_| AssistantError::DatabaseError("DB_NAME environment variable not set".to_string()))?;
let user = env::var("DB_USER")
.map_err(|_| AssistantError::DatabaseError("DB_USER environment variable not set".to_string()))?;
let password = env::var("DB_PASSWORD")
.map_err(|_| AssistantError::DatabaseError("DB_PASSWORD environment variable not set".to_string()))?;
let encoded_password = encode(&password);

let database_url = format!("mysql://{}:{}@{}:{}/{}", user, password, host, port, dbname);
let (client, connection) =
tokio_postgres::connect(&database_url, NoTls).await
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
// The connection object performs the actual communication with the database,
// so spawn it off to run on its own.
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
// Define the query
let main_query = r#"
SELECT bikes.slug as slug,
bike_categories.slug as category,
motor,
bike_additional_infos.frame_size_code as frame_size_code,
bike_additional_infos.rider_height_min as rider_height_min,
bike_additional_infos.rider_height_max as rider_height_max,
price,
color
FROM bikes
JOIN bike_additional_infos ON bikes.id = bike_additional_infos.bike_id
JOIN bike_categories ON bikes.bike_category_id = bike_categories.id
WHERE bikes.status = 'active'
"#;
// Execute the query
let stmt = client.prepare(main_query).await
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
let rows = client.query(&stmt, &[]).await
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
// Convert the tokio_postgres::Row into your serializable struct.
let bikes: Vec<Bike> = rows
.iter()
.map(|row| Bike {
slug: row.get("slug"),
category: row.get("category"),
motor: row.get("motor"),
frame_size_code: row.get("frame_size_code"),
rider_height_min: row.get("rider_height_min"),
rider_height_max: row.get("rider_height_max"),
price: row.get("price"),
color: row.get("color"),
})
.collect();
// Serialize the bikes to JSON
let bikes_json_string = serde_json::to_string_pretty(&bikes)
.map_err(|e| AssistantError::OpenAIError(e.to_string()))?;
// Write the JSON data to a file in the specified folder_path
let file_path = PathBuf::from(&self.folder_path).join("bikes.json");
let mut file = File::create(file_path)
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
file.write_all(bikes_json_string.as_bytes())
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
Ok(())
}
pub async fn scrape_context(&self) -> Result<(), AssistantError> {
let client = Client::new();
let folder_path = Path::new(&self.folder_path);
Expand Down Expand Up @@ -438,6 +522,8 @@ pub async fn create_ressources(
instructions_file_path: instructions_file_path.to_string(),
instructions: String::new(),
};
// Get bikes from the database and save them to a JSON file
files.bikes_db().await?;
// Scrape the context from the provided URLs
files.scrape_context().await?;
// Upload the scraped files to OpenAI
Expand Down

0 comments on commit edb53ed

Please sign in to comment.