cleaned up update.rs eco and corp
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
use super::{scraper::*, storage::*, helpers::*, types::*};
|
||||
use crate::{config::Config, scraper::webdriver::{ScrapeTask, ChromeDriverPool}, util::directories::DataPaths, util::logger};
|
||||
use chrono::{Local};
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Runs the full update for economic data using streaming to minimize memory usage
|
||||
pub async fn run_full_update(config: &Config, pool: &Arc<ChromeDriverPool>) -> anyhow::Result<()> {
|
||||
pub async fn run_full_update(config: &Config, pool: &Arc<ChromeDriverPool>, shutdown_flag: &Arc<AtomicBool>) -> anyhow::Result<()> {
|
||||
let paths = DataPaths::new(".")?;
|
||||
|
||||
logger::log_info("Economic Update: Initializing...").await;
|
||||
@@ -14,17 +14,23 @@ pub async fn run_full_update(config: &Config, pool: &Arc<ChromeDriverPool>) -> a
|
||||
let today_str = chrono::Local::now().date_naive().format("%Y-%m-%d").to_string();
|
||||
let end_date = config.target_end_date();
|
||||
|
||||
logger::log_info("=== Economic Update ===").await;
|
||||
|
||||
// Step 1: Build lightweight index instead of loading all events
|
||||
logger::log_info("Economic Update: Building event index...").await;
|
||||
logger::log_info("Step 1: Building event index...").await;
|
||||
let chunks = scan_existing_chunks(&paths).await?;
|
||||
let event_index = build_event_index(&chunks).await?;
|
||||
|
||||
logger::log_info(&format!("Economic Update: Indexed {} events from {} chunks",
|
||||
logger::log_info(&format!(" Economic Update: Indexed {} events from {} chunks",
|
||||
event_index.len(), chunks.len())).await;
|
||||
|
||||
if shutdown_flag.load(Ordering::SeqCst) {
|
||||
logger::log_warn("Shutdown detected after GLEIF download").await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Step 2: Determine start date
|
||||
let start_date = if event_index.is_empty() {
|
||||
logger::log_warn("Economic Update: No existing events found, starting from config date").await;
|
||||
logger::log_warn("Step 2: No existing events found, starting from config date").await;
|
||||
config.economic_start_date.clone()
|
||||
} else {
|
||||
// Find the latest date in the index
|
||||
@@ -35,7 +41,7 @@ pub async fn run_full_update(config: &Config, pool: &Arc<ChromeDriverPool>) -> a
|
||||
.unwrap_or(today_str.clone());
|
||||
|
||||
if max_date >= today_str {
|
||||
logger::log_info("Economic Update: Events exist for today, starting from today").await;
|
||||
logger::log_info(" Events exist for today, starting from today").await;
|
||||
today_str.clone()
|
||||
} else {
|
||||
let next = chrono::NaiveDate::parse_from_str(&max_date, "%Y-%m-%d")
|
||||
@@ -43,34 +49,46 @@ pub async fn run_full_update(config: &Config, pool: &Arc<ChromeDriverPool>) -> a
|
||||
.and_then(|d| d.succ_opt())
|
||||
.map(|d| d.format("%Y-%m-%d").to_string())
|
||||
.unwrap_or(today_str.clone());
|
||||
logger::log_info(&format!("Economic Update: Resuming from: {}", next)).await;
|
||||
logger::log_info(&format!(" Resuming from: {}", next)).await;
|
||||
next
|
||||
}
|
||||
};
|
||||
|
||||
logger::log_info(&format!("Economic Update: Scraping events from {} → {}", start_date, end_date)).await;
|
||||
if shutdown_flag.load(Ordering::SeqCst) {
|
||||
logger::log_warn("Shutdown detected after GLEIF download").await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Step 3: Scrape new events in batches
|
||||
logger::log_info(&format!("Step 3: Scraping events from {} → {}", start_date, end_date)).await;
|
||||
let new_events = scrape_all_economic_events(&start_date, &end_date, pool).await?;
|
||||
|
||||
logger::log_info(&format!("Economic Update: Scraped {} new events", new_events.len())).await;
|
||||
logger::log_info(&format!(" Scraped {} new events", new_events.len())).await;
|
||||
|
||||
if shutdown_flag.load(Ordering::SeqCst) {
|
||||
logger::log_warn("Shutdown detected after GLEIF download").await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Step 4: Process events in streaming fashion
|
||||
logger::log_info(&format!("Step 4: Detecting changes")).await;
|
||||
let (changes, updated_events) = process_events_streaming(&chunks, &new_events, &today_str).await?;
|
||||
|
||||
logger::log_info(&format!("Economic Update: Detected {} changes", changes.len())).await;
|
||||
|
||||
logger::log_info(&format!(" Detected {} changes", changes.len())).await;
|
||||
if !changes.is_empty() {
|
||||
logger::log_info(&format!("Economic Update: Saving {} changes to log", changes.len())).await;
|
||||
logger::log_info(&format!(" Saving {} changes to log", changes.len())).await;
|
||||
save_changes(&paths, &changes).await?;
|
||||
logger::log_info("Economic Update: Changes saved successfully").await;
|
||||
logger::log_info(" Changes saved successfully").await;
|
||||
}
|
||||
|
||||
if shutdown_flag.load(Ordering::SeqCst) {
|
||||
logger::log_warn("Shutdown detected after GLEIF download").await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Step 5: Save consolidated events
|
||||
logger::log_info(&format!("Economic Update: Saving {} total events to chunks", updated_events.len())).await;
|
||||
logger::log_info(&format!("Step 5: Saving {} total events to chunks", updated_events.len())).await;
|
||||
save_optimized_chunks(&paths, updated_events).await?;
|
||||
|
||||
logger::log_info(&format!("✓ Economic update complete — {} changes detected", changes.len())).await;
|
||||
logger::log_info(&format!(" ✓ Economic update complete — {} changes detected", changes.len())).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// src/forex/update_rates.rs
|
||||
// src/forex/update_forex.rs
|
||||
use crate::config::Config;
|
||||
use crate::util::directories::DataPaths;
|
||||
use crate::util::logger;
|
||||
|
||||
Reference in New Issue
Block a user