refactor: extract filter to a single location

chore: add filter for network sector chats
This commit is contained in:
Jelson Stoelben Rodrigues
2025-09-23 11:59:07 -03:00
parent 8078c05f33
commit 0d6a9ed72e

View File

@@ -198,8 +198,8 @@ fn main() -> anyhow::Result<()> {
println!("IDS {:?}", talk_ids);
// Calculate the response time in seconds
let response_time = talk_ids
// Gather messages and apply filtering
let filtered_chats = talk_ids
.iter()
.cloned()
.map(|talk_id| {
@@ -285,12 +285,24 @@ fn main() -> anyhow::Result<()> {
if comercial_agents_unavailable.is_some() {return None;}
// Has network sector in any message
let network_sector_mentioned = talk_histories.as_array().expect("Wrong message type received from talk histories").into_iter().find(|message_object|{
let message = message_object["message"].as_str().expect("Failed to decode message as string");
let found = message.to_uppercase().find(&"setor de redes".to_uppercase());
found.is_some()
});
if network_sector_mentioned.is_some() {return None;}
// Filter Bot finished chats
if json["agent"]["user"]["name"].as_str().unwrap_or("unknown_user") == "PipeBot" {return None;}
return Some(json);
})
// .clone()
});
// Calculate the response time in seconds
let response_time = filtered_chats
.clone()
.map(|messages| {
let json = messages.unwrap();
let talk_histories = &json["talk_histories"];
@@ -355,98 +367,8 @@ fn main() -> anyhow::Result<()> {
let mut response_time_file = std::fs::OpenOptions::new().write(true).open(format!("./evaluations/{formatted_day_before}/response_time.csv")).expect("Failed to open response time file for write");
response_time_file.write_all(response_time.as_bytes()).expect("Failed to write to file!");
talk_ids
.iter()
.cloned()
.map(|talk_id| {
let talk_id_get_request = client
.get(format!("https://{}/api/talk_histories", PIPERUN_API_URL))
.bearer_auth(&access_token)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.query(&[
("talk_id", talk_id),
("type", "a".to_string()),
("only_view", "1".to_string()),
]);
let talk_id_get_result = talk_id_get_request.send();
return talk_id_get_result;
})
.filter_map_ok(|result| {
let json = result.json::<serde_json::Value>().expect("Failed to deserialize response to JSON").to_owned();
let talk_histories = &json["talk_histories"];
let data = &talk_histories["data"];
// Filter chats that have very few messages
let talk_lenght = talk_histories.as_array().expect("Wrong message type received from talk histories").len();
if talk_lenght < MINIMUM_NUMBER_OF_MESSAGES_TO_EVALUATE {return None;}
// Filter chats that have less that specified ammount of talks with support agent form the last queue transfer
let found = talk_histories.as_array().expect("Wrong message type received from talk histories").into_iter().enumerate().find(|(pos, message_object)|{
let message = message_object["message"].as_str().expect("Failed to decode message as string");
let found = message.find("Atendimento transferido para a fila [NovaNet -> Atendimento -> Suporte]");
found.is_some()
});
match found {
None => {return None;},
Some(pos) => {
let pos_found = pos.0;
if pos_found < MINIMUM_NUMBER_OF_MESSAGES_WITH_AGENT_TO_EVALUATE {return None;}
}
};
// Filter chats that pass by multiple agents
let number_of_agents_except_pipebot = talk_histories.as_array().expect("Wrong message type received from talk histories").into_iter()
.map(|message| &message["user"]["name"])
.filter(|name| name.as_str().unwrap() != "PipeBot".to_string())
.unique()
.count();
if number_of_agents_except_pipebot > 1 {return None;}
// Filter stop notification active
let found_stop_notification = talk_histories.as_array().expect("Wrong message type received from talk histories").into_iter().enumerate().find(|(pos, message_object)|{
let message = message_object["message"].as_str().expect("Failed to decode message as string");
let found = message.find("ATENÇÃO CLIENTE COM NOTIFICAÇÃO DE PARADA ATIVA");
found.is_some()
});
if found_stop_notification.is_some() {return None;}
// Filter lack of dialogue
let lack_of_dialogue = talk_histories.as_array().expect("Wrong message type received from talk histories").into_iter().find(|message_object|{
let message = message_object["message"].as_str().expect("Failed to decode message as string");
let found = message.to_uppercase().find(&"Esse atendimento está sendo encerrado devido a inexistência de diálogo".to_uppercase());
found.is_some()
});
if lack_of_dialogue.is_some() {return None;}
// Filter is portal access
let portal_link_offer = talk_histories.as_array().expect("Wrong message type received from talk histories").into_iter().find(|message_object|{
let message = message_object["message"].as_str().expect("Failed to decode message as string");
let found = message.to_uppercase().find(&"https://novanet.portal.7az.com.br/login".to_uppercase());
found.is_some()
});
if portal_link_offer.is_some() {return None;}
// Filter no comercial agent available
let comercial_agents_unavailable = talk_histories.as_array().expect("Wrong message type received from talk histories").into_iter().find(|message_object|{
let message = message_object["message"].as_str().expect("Failed to decode message as string");
let found = message.to_uppercase().find(&"Transbordo automático para a fila [NovaNet -> Atendimento -> Suporte], pois não havia agentes disponíveis na fila [NovaNet -> Atendimento -> Comercial]".to_uppercase());
found.is_some()
});
if comercial_agents_unavailable.is_some() {return None;}
// Filter Bot finished chats
if json["agent"]["user"]["name"].as_str().unwrap_or("unknown_user") == "PipeBot" {return None;}
return Some(json);
})
filtered_chats
.clone()
.skip(0)
.take(15)
.for_each(|result| {