mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +03:00
fetch market data symbols concurrently instead of sequentially
fetch_market_data hit Yahoo's v8 chart endpoint once per symbol in a sequential loop — with 9 symbols that's 9 round trips end to end. join_all fires them together.
This commit is contained in:
parent
a674c66d63
commit
f634b1858e
3 changed files with 43 additions and 15 deletions
18
src-tauri/Cargo.lock
generated
18
src-tauri/Cargo.lock
generated
|
|
@ -1094,6 +1094,21 @@ dependencies = [
|
|||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-executor",
|
||||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.32"
|
||||
|
|
@ -1101,6 +1116,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1168,6 +1184,7 @@ version = "0.3.32"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-macro",
|
||||
|
|
@ -3710,6 +3727,7 @@ name = "summarum"
|
|||
version = "1.2.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"futures",
|
||||
"reqwest 0.12.28",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ serde = { version = "1", features = ["derive"] }
|
|||
serde_json = "1"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||
chrono = "0.4"
|
||||
futures = "0.3.32"
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
|
|
|
|||
|
|
@ -295,23 +295,32 @@ async fn fetch_market_data(
|
|||
// v7/finance/quote now requires a cookie+crumb pair Yahoo stopped issuing
|
||||
// to plain API clients (returns 401); v8 chart still answers anonymously,
|
||||
// one symbol per request.
|
||||
let mut prices = std::collections::HashMap::new();
|
||||
for sym in &symbols {
|
||||
let url = format!(
|
||||
"https://query1.finance.yahoo.com/v8/finance/chart/{}?range=1d&interval=1d",
|
||||
sym
|
||||
);
|
||||
let Ok(resp) = client.get(&url).send().await else {
|
||||
continue;
|
||||
};
|
||||
let Ok(body) = resp.json::<serde_json::Value>().await else {
|
||||
continue;
|
||||
};
|
||||
if let Some(price) = body["chart"]["result"][0]["meta"]["regularMarketPrice"].as_f64() {
|
||||
if price > 0.0 {
|
||||
prices.insert(sym.clone(), price);
|
||||
let futures_reqs = symbols.iter().map(|sym| {
|
||||
let client = client.clone();
|
||||
async move {
|
||||
let url = format!(
|
||||
"https://query1.finance.yahoo.com/v8/finance/chart/{}?range=1d&interval=1d",
|
||||
sym
|
||||
);
|
||||
if let Ok(resp) = client.get(&url).send().await {
|
||||
if let Ok(body) = resp.json::<serde_json::Value>().await {
|
||||
if let Some(price) =
|
||||
body["chart"]["result"][0]["meta"]["regularMarketPrice"].as_f64()
|
||||
{
|
||||
if price > 0.0 {
|
||||
return Some((sym.clone(), price));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
let mut prices = std::collections::HashMap::new();
|
||||
let results = futures::future::join_all(futures_reqs).await;
|
||||
for (sym, price) in results.into_iter().flatten() {
|
||||
prices.insert(sym, price);
|
||||
}
|
||||
|
||||
if !prices.is_empty() {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue