mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 21:31:59 +03:00
⚡ Optimize historical rates batch fetching with API range query
Replaced individual Frankfurter API requests with a single batched range query in fetch_historical_rates_batch. Added fallback logic to handle missing weekend/holiday dates. Ran cargo fmt to resolve CI formatting failures. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
845bc3d484
commit
da0c8edb8c
2 changed files with 14 additions and 6 deletions
|
|
@ -1 +0,0 @@
|
||||||
// Wait, we can just write a quick script to benchmark it from JS or Rust
|
|
||||||
|
|
@ -662,13 +662,19 @@ async fn fetch_historical_rates_batch(
|
||||||
available_dates.sort();
|
available_dates.sort();
|
||||||
|
|
||||||
for req_date_str in missing_dates {
|
for req_date_str in missing_dates {
|
||||||
if let Ok(req_date) = chrono::NaiveDate::parse_from_str(&req_date_str, "%Y-%m-%d") {
|
if let Ok(req_date) =
|
||||||
|
chrono::NaiveDate::parse_from_str(&req_date_str, "%Y-%m-%d")
|
||||||
|
{
|
||||||
// Find the exact date or the closest preceding date
|
// Find the exact date or the closest preceding date
|
||||||
let matched_date_str = match available_dates.binary_search(&req_date) {
|
let matched_date_str = match available_dates.binary_search(&req_date) {
|
||||||
Ok(idx) => Some(available_dates[idx].format("%Y-%m-%d").to_string()),
|
Ok(idx) => {
|
||||||
|
Some(available_dates[idx].format("%Y-%m-%d").to_string())
|
||||||
|
}
|
||||||
Err(idx) => {
|
Err(idx) => {
|
||||||
if idx > 0 {
|
if idx > 0 {
|
||||||
Some(available_dates[idx - 1].format("%Y-%m-%d").to_string())
|
Some(
|
||||||
|
available_dates[idx - 1].format("%Y-%m-%d").to_string(),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
@ -676,9 +682,12 @@ async fn fetch_historical_rates_batch(
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(matched_date) = matched_date_str {
|
if let Some(matched_date) = matched_date_str {
|
||||||
if let Some(matched_rates_obj) = rates_obj.get(&matched_date).and_then(|r| r.as_object()) {
|
if let Some(matched_rates_obj) =
|
||||||
|
rates_obj.get(&matched_date).and_then(|r| r.as_object())
|
||||||
|
{
|
||||||
let daily_rates = parse_rate_map(matched_rates_obj);
|
let daily_rates = parse_rate_map(matched_rates_obj);
|
||||||
let cache_path = data_dir(&app).join(format!("rates-{}.json", req_date_str));
|
let cache_path =
|
||||||
|
data_dir(&app).join(format!("rates-{}.json", req_date_str));
|
||||||
if let Ok(serialized) = serde_json::to_string(&daily_rates) {
|
if let Ok(serialized) = serde_json::to_string(&daily_rates) {
|
||||||
write_atomic_async(cache_path, serialized).await;
|
write_atomic_async(cache_path, serialized).await;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue