Skip to content

Commit

Permalink
Update Generate Script Function
Browse files Browse the repository at this point in the history
  • Loading branch information
arturofigueroabim committed Feb 29, 2024
1 parent bd3fa66 commit 494a3c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
76 changes: 40 additions & 36 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,48 +231,52 @@ impl Db {

pub async fn generate_user_script(&mut self, query: TokenParams) -> Result<String, String> {
let tables_result = fetch_project_tables_names_request(query.clone()).await;
let mut script_lines = Vec::new();

if let Ok(tables) = tables_result {
info!("Result from status_project_from_beam: {:?}", tables);

let records = tokens
.filter(project_id.eq(&query.project_id))
.filter(user_id.eq(&query.user_id))
.select(TokenManager::as_select())
.load::<TokenManager>(&mut self.0);

match records {
Ok(records) => {
if !records.is_empty() {
for record in &records {
for table in &tables {
script_lines.push(format!(
"builder$append(server='{}', url='https://{}/opal/', token='{}', table='{}', driver='OpalDriver')",
record.bk, record.bk, record.token, table
));

match tables_result {
Ok(tables) => {
let mut script_lines = Vec::new();

for bridgehead in &query.bridgehead_ids {

let records_result = tokens
.filter(project_id.eq(&query.project_id))
.filter(user_id.eq(&query.user_id))
.filter(bk.eq(bridgehead))
.order(id.desc())
.select(TokenManager::as_select())
.first::<TokenManager>(&mut self.0);

match records_result {
Ok(record) => {
for table in &tables {
let site_name = record.bk.split('.').nth(1).expect("Valid app id");
script_lines.push(format!(
"builder$append(server='{}', url='https://{}/opal/', token='{}', table='{}', driver='OpalDriver')",
site_name, record.bk, record.token, table
));
}
}
}
let script = generate_r_script(script_lines);
Ok(script)
} else {
info!("No records were found");
Ok("No records found for the given project and user.".into())
Err(_) => {
info!("No records were found");
return Ok("No records found for the given project and user.".into());
},
}
}
Err(err) => {
error!("Error loading records: {}", err);
Err(format!("Error loading records: {}", err))

if !script_lines.is_empty() {
let script = generate_r_script(script_lines); // Assuming this function exists and works as intended
Ok(script)
} else {
Ok("No records found for the given project and user.".into())
}
}
} else {
if let Err(e) = tables_result {
info!("Error in status_project_from_beam: {:?}", e);
}
Err("Error obtaining table names.".into())
},
Err(e) => {
error!("Error in fetch_project_tables_names_request: {:?}", e);
Err("Error obtaining table names.".into())
},
}
}
}
}

fn generate_r_script(script_lines: Vec<String>) -> String {
let mut builder_script = String::from(
Expand Down
4 changes: 2 additions & 2 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ async fn save_tokens_from_beam(mut db: Db, task: TaskRequest<OpalRequest>, token

async fn update_tokens_from_beam(mut db: Db, task: TaskRequest<OpalRequest>, token_params: TokenParams, token_name: String) -> Result<()> {
let today = Local::now();
let formatted_date = today.format("%d-%m-%Y").to_string();
let formatted_date = today.format("%d-%m-%Y %H:%M:%S").to_string();

let res = BEAM_CLIENT
.raw_beam_request(Method::GET, &format!("/v1/tasks/{}/results?wait_count={}", task.id, task.to.len()))
Expand Down Expand Up @@ -345,7 +345,7 @@ async fn update_tokens_from_beam(mut db: Db, task: TaskRequest<OpalRequest>, tok
last_error = Some(format!("Error: {error}"));
},
OpalResponse::Ok { token } => {
let site_name = result.from.as_ref().split('.').nth(1).expect("Valid app id");
let site_name = result.from.as_ref(); //.split('.').nth(1).expect("Valid app id");
let new_token = NewToken {
token_name: &token_name,
token: &token,
Expand Down

0 comments on commit 494a3c3

Please sign in to comment.