Adaptação para ler o .env como argumento.

This commit is contained in:
2026-07-15 15:18:13 -03:00
parent 1cddd14f94
commit 82267a01f7

View File

@@ -14,10 +14,141 @@ pub mod send_mail_util;
pub mod zip_directory_util; pub mod zip_directory_util;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
// Get the current day in the format YYYY-MM-DD
let current_date = chrono::Local::now();
let formatted_date = current_date.format("%Y-%m-%d").to_string();
println!("Current date: {}", formatted_date);
// Get the day before the current date
let day_before = current_date
.checked_sub_signed(chrono::Duration::days(1))
.expect("Failed to get the day before");
let formatted_day_before = day_before.format("%Y-%m-%d").to_string();
println!("Day before: {}", formatted_day_before);
let day_before_at_midnight = day_before
.with_hour(0)
.unwrap()
.with_minute(0)
.unwrap()
.with_second(0)
.unwrap();
let formatted_day_before_at_midnight =
day_before_at_midnight.format("%Y-%m-%d %H:%M").to_string();
let day_before_at_23_59_59 = day_before
.with_hour(23)
.unwrap()
.with_minute(59)
.unwrap()
.with_second(59)
.unwrap();
let formatted_day_before_at_23_59_59 =
day_before_at_23_59_59.format("%Y-%m-%d %H:%M").to_string();
println!(
"Day before at midnight: {}, Day before at 23:59:59: {}",
formatted_day_before_at_midnight, formatted_day_before_at_23_59_59
);
let formatted_day_before = day_before_at_midnight.format("%Y-%m-%d").to_string();
// Create a folder named with the day_before
if !std::fs::exists(format!("./evaluations/evaluations_noc-cli/{formatted_day_before}")).unwrap() {
std::fs::create_dir(format!("./evaluations/evaluations_noc-cli/{formatted_day_before}"))
.expect("Failed to create directory")
}
// Create the response time folder
if !std::fs::exists(format!(
"./evaluations/evaluations_noc-cli/{formatted_day_before}/response_time.csv"
))
.unwrap()
{
let mut response_time_file = std::fs::File::create_new(format!(
"./evaluations/evaluations_noc-cli/{formatted_day_before}/response_time.csv"
))
.expect("Failed to response_time.csv");
}
/*
// --- NOVO: processar argumento de linha de comando para data específica ---
use std::env;
use chrono::NaiveDate;
let args: Vec<String> = env::args().collect();
//Primeiro argumento: data.
let target_day = if args.len() > 1 {
// Se um argumento foi passado, interpretar como YYYY-MM-DD
let naive_date = NaiveDate::parse_from_str(&args[1], "%Y-%m-%d")
.expect("Formato de data inválido. Use YYYY-MM-DD");
// Converter para DateTime com hora 00:00:00 do fuso local
naive_date.and_hms_opt(0, 0, 0).unwrap()
} else {
// Comportamento padrão: dia anterior ao atual
(chrono::Local::now() - chrono::Duration::days(1)).naive_local()
};
println!("Processando o dia: {}", target_day.format("%Y-%m-%d"));
// Definir início e fim do dia (para consultas na API)
let day_start = target_day; // já 00:00
let day_end = target_day
.with_hour(23).unwrap()
.with_minute(59).unwrap()
.with_second(59).unwrap();
let formatted_day = target_day.format("%Y-%m-%d").to_string();
let formatted_day_start = day_start.format("%Y-%m-%d %H:%M").to_string();
let formatted_day_end = day_end.format("%Y-%m-%d %H:%M").to_string();
println!("Início do dia: {}", formatted_day_start);
println!("Fim do dia: {}", formatted_day_end);
// Criar pasta com o nome do dia processado
if !std::fs::exists(format!("./evaluations/evaluations_noc-cli/{formatted_day}")).unwrap() {
std::fs::create_dir(format!("./evaluations/evaluations_noc-cli/{formatted_day}"))
.expect("Failed to create directory");
}
// Criar arquivo response_time.csv se não existir
if !std::fs::exists(format!("./evaluations/evaluations_noc-cli/{formatted_day}/response_time.csv")).unwrap() {
let _ = std::fs::File::create_new(format!(
"./evaluations/evaluations_noc-cli/{formatted_day}/response_time.csv"
))
.expect("Failed to create response_time.csv");
}
// Segundo argumento: arquivo .env
if args.len() > 2 {
let env_path = &args[2];
dotenv::from_path(env_path).expect("Falha ao carregar .env");
} else {
dotenv::dotenv().ok();
}
*/
// coleta os argumentos da linha de comando.
let args: Vec<String> = env::args().collect();
//Primeiro argumento: arquivo .env
let env_file = args.get(1).map(|s| s.as_str());
match env_file {
Some(path) => {
dotenv::from_path(path).expect("Failed to load .env file");
println!("Loaded from {}", path);
}
None => {
dotenv::dotenv().ok();
println!("Loaded default .env");
}
}
/*
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, using defaults"), None => eprintln!("Failed to load .env file, using defaults"),
} }
*/
// Read environment variables // Read environment variables
let OLLAMA_URL = env::var("OLLAMA_URL").unwrap_or("localhost".to_string()); let OLLAMA_URL = env::var("OLLAMA_URL").unwrap_or("localhost".to_string());
@@ -125,115 +256,6 @@ fn main() -> anyhow::Result<()> {
} }
}; };
// Get the current day in the format YYYY-MM-DD
let current_date = chrono::Local::now();
let formatted_date = current_date.format("%Y-%m-%d").to_string();
println!("Current date: {}", formatted_date);
// Get the day before the current date
let day_before = current_date
.checked_sub_signed(chrono::Duration::days(1))
.expect("Failed to get the day before");
let formatted_day_before = day_before.format("%Y-%m-%d").to_string();
println!("Day before: {}", formatted_day_before);
let day_before_at_midnight = day_before
.with_hour(0)
.unwrap()
.with_minute(0)
.unwrap()
.with_second(0)
.unwrap();
let formatted_day_before_at_midnight =
day_before_at_midnight.format("%Y-%m-%d %H:%M").to_string();
let day_before_at_23_59_59 = day_before
.with_hour(23)
.unwrap()
.with_minute(59)
.unwrap()
.with_second(59)
.unwrap();
let formatted_day_before_at_23_59_59 =
day_before_at_23_59_59.format("%Y-%m-%d %H:%M").to_string();
println!(
"Day before at midnight: {}, Day before at 23:59:59: {}",
formatted_day_before_at_midnight, formatted_day_before_at_23_59_59
);
let formatted_day_before = day_before_at_midnight.format("%Y-%m-%d").to_string();
// Create a folder named with the day_before
if !std::fs::exists(format!("./evaluations/evaluations_noc-cli/{formatted_day_before}")).unwrap() {
std::fs::create_dir(format!("./evaluations/evaluations_noc-cli/{formatted_day_before}"))
.expect("Failed to create directory")
}
// Create the response time folder
if !std::fs::exists(format!(
"./evaluations/evaluations_noc-cli/{formatted_day_before}/response_time.csv"
))
.unwrap()
{
let mut response_time_file = std::fs::File::create_new(format!(
"./evaluations/evaluations_noc-cli/{formatted_day_before}/response_time.csv"
))
.expect("Failed to response_time.csv");
}
/*
// --- NOVO: processar argumento de linha de comando para data específica ---
use std::env;
use chrono::NaiveDate;
let args: Vec<String> = env::args().collect();
let target_day = if args.len() > 1 {
// Se um argumento foi passado, interpretar como YYYY-MM-DD
let naive_date = NaiveDate::parse_from_str(&args[1], "%Y-%m-%d")
.expect("Formato de data inválido. Use YYYY-MM-DD");
// Converter para DateTime com hora 00:00:00 do fuso local
naive_date.and_hms_opt(0, 0, 0).unwrap()
} else {
// Comportamento padrão: dia anterior ao atual
(chrono::Local::now() - chrono::Duration::days(1)).naive_local()
};
println!("Processando o dia: {}", target_day.format("%Y-%m-%d"));
// Definir início e fim do dia (para consultas na API)
let day_start = target_day; // já 00:00
let day_end = target_day
.with_hour(23).unwrap()
.with_minute(59).unwrap()
.with_second(59).unwrap();
let formatted_day = target_day.format("%Y-%m-%d").to_string();
let formatted_day_start = day_start.format("%Y-%m-%d %H:%M").to_string();
let formatted_day_end = day_end.format("%Y-%m-%d %H:%M").to_string();
println!("Início do dia: {}", formatted_day_start);
println!("Fim do dia: {}", formatted_day_end);
// Criar pasta com o nome do dia processado
if !std::fs::exists(format!("./evaluations/evaluations_noc-cli/{formatted_day}")).unwrap() {
std::fs::create_dir(format!("./evaluations/evaluations_noc-cli/{formatted_day}"))
.expect("Failed to create directory");
}
// Criar arquivo response_time.csv se não existir
if !std::fs::exists(format!("./evaluations/evaluations_noc-cli/{formatted_day}/response_time.csv")).unwrap() {
let _ = std::fs::File::create_new(format!(
"./evaluations/evaluations_noc-cli/{formatted_day}/response_time.csv"
))
.expect("Failed to create response_time.csv");
}
*/
// Read system prompt // Read system prompt
let prompt = std::fs::read_to_string("./prompts/PROMPT_NOC-CLI.txt").unwrap(); let prompt = std::fs::read_to_string("./prompts/PROMPT_NOC-CLI.txt").unwrap();
let filter_file_contents = std::fs::read_to_string("./filters/FILTER_NOC-CLI.txt").unwrap_or(String::new()); let filter_file_contents = std::fs::read_to_string("./filters/FILTER_NOC-CLI.txt").unwrap_or(String::new());