use chrono::{Duration, Utc};
use polars::error::PolarsError;
use polars::prelude::*;
use std::fs::File;
use std::sync::Arc;
use std::{error::Error, path::Path, path::PathBuf};
use crate::constants::LSTM_TRAINING_DAYS;
use rustalib::util::file_utils::read_financial_data;
pub fn read_csv_to_dataframe<P: AsRef<Path>>(
file_path: P,
_has_header: bool,
_column_names: Option<Vec<&str>>,
) -> PolarsResult<DataFrame> {
let (df, _) = read_financial_data(file_path.as_ref().to_str().unwrap())?;
Ok(df)
}
pub fn load_and_preprocess_with_days(
full_path: &PathBuf,
days: Option<i64>,
) -> Result<DataFrame, Box<dyn Error>> {
println!("Loading data from: {}", full_path.display());
if !full_path.exists() {
return Err(format!("File not found: {}", full_path.display()).into());
}
let (df, _) = read_financial_data(full_path.to_str().unwrap())?;
let training_days = days.unwrap_or(LSTM_TRAINING_DAYS);
let one_year_ago = Utc::now() - Duration::days(training_days);
let cutoff_str = one_year_ago.format("%Y-%m-%d %H:%M:%S UTC").to_string();
use polars::prelude::{col, lit};
let df = df
.lazy()
.filter(col("time").gt(lit(cutoff_str)))
.collect()?;
Ok(df)
}
pub fn prepare_lstm_data(
file_path: &PathBuf,
_sequence_length: usize,
) -> Result<DataFrame, Box<dyn Error>> {
let df = load_and_preprocess_with_days(file_path, None)?;
Ok(df)
}