chore: refactor getting chats and extract into function
This commit is contained in:
204
src/main.rs
204
src/main.rs
@@ -12,10 +12,10 @@ use reqwest;
|
|||||||
use serde_json::{self, json};
|
use serde_json::{self, json};
|
||||||
use zip;
|
use zip;
|
||||||
|
|
||||||
fn main() {
|
fn main() -> anyhow::Result<()> {
|
||||||
match dotenv::dotenv().ok() {
|
match dotenv::dotenv().ok() {
|
||||||
Some(_) => println!("Environment variables loaded from .env file"),
|
Some(_) => println!("Environment variables loaded from .env file"),
|
||||||
None => eprintln!("Failed to load .env file"),
|
None => eprintln!("Failed to load .env file, using defaults"),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read environment variables
|
// Read environment variables
|
||||||
@@ -96,7 +96,7 @@ fn main() {
|
|||||||
let access_token = match response {
|
let access_token = match response {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
if resp.status().is_success() {
|
if resp.status().is_success() {
|
||||||
let json: serde_json::Value = resp.json().unwrap();
|
let json: serde_json::Value = resp.json()?;
|
||||||
// println!("Authentication successful: {:?}", json);
|
// println!("Authentication successful: {:?}", json);
|
||||||
|
|
||||||
// Extract the access token
|
// Extract the access token
|
||||||
@@ -112,7 +112,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Authentication failed: {}", resp.status());
|
eprintln!("Authentication failed: {}", resp.status());
|
||||||
let json: serde_json::Value = resp.json().unwrap();
|
let json: serde_json::Value = resp.json()?;
|
||||||
eprintln!("Response body: {:?}", json);
|
eprintln!("Response body: {:?}", json);
|
||||||
|
|
||||||
panic!("Failed to authenticate with Piperun API");
|
panic!("Failed to authenticate with Piperun API");
|
||||||
@@ -170,97 +170,7 @@ fn main() {
|
|||||||
std::fs::create_dir(format!("./evaluations/{formatted_day_before}")).expect("Failed to create directory")
|
std::fs::create_dir(format!("./evaluations/{formatted_day_before}")).expect("Failed to create directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
let start_of_talk_code: String = "talk_start".to_string();
|
let talks_array = get_piperun_chats_on_date(&PIPERUN_API_URL, &client, &access_token, formatted_day_before_at_midnight, formatted_day_before_at_23_59_59);
|
||||||
let support_queue_id: String = "13".to_string();
|
|
||||||
|
|
||||||
// API V2
|
|
||||||
let report_type = "consolidated".to_string();
|
|
||||||
let page = "1".to_string();
|
|
||||||
let per_page = "30".to_string();
|
|
||||||
let talks_request = client
|
|
||||||
.get(format!("https://{}/api/v2/reports/talks", PIPERUN_API_URL))
|
|
||||||
.bearer_auth(&access_token)
|
|
||||||
.header("Content-Type", "application/json")
|
|
||||||
.header("Accept", "application/json")
|
|
||||||
.query(&[
|
|
||||||
("page", page.clone()),
|
|
||||||
("perPage", per_page.clone()),
|
|
||||||
("report_type", report_type.clone()),
|
|
||||||
("start_date", formatted_day_before_at_midnight.clone()),
|
|
||||||
("end_date", formatted_day_before_at_23_59_59.clone()),
|
|
||||||
("date_range_type", start_of_talk_code.clone()),
|
|
||||||
("queue_id[]", support_queue_id.clone()),
|
|
||||||
]);
|
|
||||||
|
|
||||||
println!("Sending request for consolidated talks... {talks_request:?}");
|
|
||||||
let talks_response = talks_request.send();
|
|
||||||
let talks = match talks_response {
|
|
||||||
Ok(resp) => {
|
|
||||||
if resp.status().is_success() {
|
|
||||||
let json: serde_json::Value = resp.json().unwrap();
|
|
||||||
|
|
||||||
json
|
|
||||||
} else {
|
|
||||||
eprintln!("Failed to get consolidated talks: {}", resp.status());
|
|
||||||
let json: serde_json::Value = resp.json().unwrap();
|
|
||||||
eprintln!("Response body: {:?}", json);
|
|
||||||
panic!("Failed to retrieve consolidated talks from Piperun API");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error sending request for consolidated talks: {}", e);
|
|
||||||
panic!("Failed to send request for consolidated talks to Piperun API");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Read paginated output until the end
|
|
||||||
let talks_array = talks["data"]
|
|
||||||
.as_array()
|
|
||||||
.expect("Failed to parse talks response as array");
|
|
||||||
|
|
||||||
// let next_page_url = talks["next_page_url"].as_str().unwrap();
|
|
||||||
// let second_page_talks_array = if talks["current_page"].as_i64().unwrap() != talks["last_page"].as_i64().unwrap() {
|
|
||||||
// let talks_request = client
|
|
||||||
// .get(format!("{}", next_page_url))
|
|
||||||
// .bearer_auth(&access_token)
|
|
||||||
// .header("Content-Type", "application/json")
|
|
||||||
// .header("Accept", "application/json")
|
|
||||||
// .query(&[
|
|
||||||
// ("report_type", report_type.clone()),
|
|
||||||
// ("start_date", formatted_day_before_at_midnight.clone()),
|
|
||||||
// ("end_date", formatted_day_before_at_23_59_59.clone()),
|
|
||||||
// ("date_range_type", start_of_talk_code.clone()),
|
|
||||||
// ("queue_id[]", support_queue_id.clone()),
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
// println!("Sending request for consolidated talks... {talks_request:?}");
|
|
||||||
// let talks_response = talks_request.send();
|
|
||||||
// let talks = match talks_response {
|
|
||||||
// Ok(resp) => {
|
|
||||||
// if resp.status().is_success() {
|
|
||||||
// let json: serde_json::Value = resp.json().unwrap();
|
|
||||||
// // println!("Consolidated talks response: {:?}", json);
|
|
||||||
|
|
||||||
// json
|
|
||||||
// } else {
|
|
||||||
// eprintln!("Failed to get consolidated talks: {}", resp.status());
|
|
||||||
// let json: serde_json::Value = resp.json().unwrap();
|
|
||||||
// eprintln!("Response body: {:?}", json);
|
|
||||||
// panic!("Failed to retrieve consolidated talks from Piperun API");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// Err(e) => {
|
|
||||||
// eprintln!("Error sending request for consolidated talks: {}", e);
|
|
||||||
// panic!("Failed to send request for consolidated talks to Piperun API");
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// talks["data"]
|
|
||||||
// .as_array()
|
|
||||||
// .expect("Failed to parse talks response as array").clone()
|
|
||||||
// }
|
|
||||||
// else {Vec::new()};
|
|
||||||
|
|
||||||
|
|
||||||
println!("Number of consolidated talks: {}", talks_array.len());
|
println!("Number of consolidated talks: {}", talks_array.len());
|
||||||
|
|
||||||
@@ -297,7 +207,7 @@ fn main() {
|
|||||||
return talk_id_get_result;
|
return talk_id_get_result;
|
||||||
})
|
})
|
||||||
.skip(0)
|
.skip(0)
|
||||||
.take(30)
|
.take(15)
|
||||||
.for_each(|result| {
|
.for_each(|result| {
|
||||||
let json = result.unwrap().json::<serde_json::Value>().expect("Failed to deserialize response to JSON");
|
let json = result.unwrap().json::<serde_json::Value>().expect("Failed to deserialize response to JSON");
|
||||||
let talk_histories = &json["talk_histories"];
|
let talk_histories = &json["talk_histories"];
|
||||||
@@ -448,6 +358,108 @@ A seguir estão as mensagens do atendimento, em JSON, avalie e retorne apenas um
|
|||||||
"Wilson da Conceição Oliveira <wilson.oliveira@nova.net.br>",
|
"Wilson da Conceição Oliveira <wilson.oliveira@nova.net.br>",
|
||||||
&output_zip_file_str,
|
&output_zip_file_str,
|
||||||
);
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_piperun_chats_on_date(PIPERUN_API_URL: &String, client: &reqwest::blocking::Client, access_token: &String, formatted_day_before_at_midnight: String, formatted_day_before_at_23_59_59: String) -> Vec<serde_json::Value> {
|
||||||
|
let start_of_talk_code: String = "talk_start".to_string();
|
||||||
|
let support_queue_id: String = "13".to_string();
|
||||||
|
|
||||||
|
// API V2
|
||||||
|
let report_type = "consolidated".to_string();
|
||||||
|
let page = "1".to_string();
|
||||||
|
let per_page = "15".to_string();
|
||||||
|
|
||||||
|
let talks_request = client
|
||||||
|
.get(format!("https://{}/api/v2/reports/talks", PIPERUN_API_URL))
|
||||||
|
.bearer_auth(access_token)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.query(&[
|
||||||
|
("page", page.clone()),
|
||||||
|
("perPage", per_page.clone()),
|
||||||
|
("report_type", report_type.clone()),
|
||||||
|
("start_date", formatted_day_before_at_midnight.clone()),
|
||||||
|
("end_date", formatted_day_before_at_23_59_59.clone()),
|
||||||
|
("date_range_type", start_of_talk_code.clone()),
|
||||||
|
("queue_id[]", support_queue_id.clone()),
|
||||||
|
]);
|
||||||
|
|
||||||
|
println!("Sending request for consolidated talks... {talks_request:?}");
|
||||||
|
let talks_response = talks_request.send();
|
||||||
|
|
||||||
|
let json_response = match talks_response {
|
||||||
|
Ok(resp) => {
|
||||||
|
if resp.status().is_success() {
|
||||||
|
let json: serde_json::Value = resp.json().unwrap();
|
||||||
|
json
|
||||||
|
} else {
|
||||||
|
eprintln!("Failed to get consolidated talks: {}", resp.status());
|
||||||
|
let json: serde_json::Value = resp.json().unwrap();
|
||||||
|
eprintln!("Response body: {:?}", json);
|
||||||
|
panic!("Failed to retrieve consolidated talks from Piperun API");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error: {e}");
|
||||||
|
panic!("Failed to send the request for talks to PipeRUN API");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut aggregated_talks = json_response["data"].as_array().expect("Failed to parse messages as array").to_owned();
|
||||||
|
|
||||||
|
let current_page = json_response["current_page"].as_i64().expect("Failed to obtain current page number");
|
||||||
|
let last_page = json_response["last_page"].as_i64().expect("Failed to obtain current page number");
|
||||||
|
|
||||||
|
let mut all_other_messages = (current_page..last_page).into_iter()
|
||||||
|
.map(|page| {
|
||||||
|
let page_to_request = page + 1;
|
||||||
|
let talks_request = client
|
||||||
|
.get(format!("https://{}/api/v2/reports/talks", PIPERUN_API_URL))
|
||||||
|
.bearer_auth(access_token)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.query(&[
|
||||||
|
("page", page_to_request.to_string()),
|
||||||
|
("perPage", per_page.clone()),
|
||||||
|
("report_type", report_type.clone()),
|
||||||
|
("start_date", formatted_day_before_at_midnight.clone()),
|
||||||
|
("end_date", formatted_day_before_at_23_59_59.clone()),
|
||||||
|
("date_range_type", start_of_talk_code.clone()),
|
||||||
|
("queue_id[]", support_queue_id.clone()),
|
||||||
|
]);
|
||||||
|
|
||||||
|
println!("Sending request for consolidated talks... {talks_request:?}");
|
||||||
|
let talks_response = talks_request.send();
|
||||||
|
|
||||||
|
let json_response = match talks_response {
|
||||||
|
Ok(resp) => {
|
||||||
|
if resp.status().is_success() {
|
||||||
|
let json: serde_json::Value = resp.json().unwrap();
|
||||||
|
json
|
||||||
|
} else {
|
||||||
|
eprintln!("Failed to get consolidated talks: {}", resp.status());
|
||||||
|
let json: serde_json::Value = resp.json().unwrap();
|
||||||
|
eprintln!("Response body: {:?}", json);
|
||||||
|
panic!("Failed to retrieve consolidated talks from Piperun API");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error: {e}");
|
||||||
|
panic!("Failed to send the request for talks to PipeRUN API");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let aggregated_talks = json_response["data"].as_array().expect("Failed to parse messages as array").to_owned();
|
||||||
|
|
||||||
|
return aggregated_talks;
|
||||||
|
})
|
||||||
|
.reduce(|mut this, mut acc| {acc.append(&mut this); acc})
|
||||||
|
.expect("Failed to concatenate all vectors of messages");
|
||||||
|
|
||||||
|
|
||||||
|
aggregated_talks.append(&mut all_other_messages);
|
||||||
|
aggregated_talks
|
||||||
}
|
}
|
||||||
|
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user