eeyf 0.1.0

Eric Evans' Yahoo Finance API - A rate-limited, reliable Rust adapter for Yahoo Finance API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Batch operations for fetching multiple symbols in parallel with automatic rate limiting.
//!
//! This module provides efficient parallel processing of multiple symbols while respecting
//! rate limits and handling per-symbol errors gracefully.
//!
//! # Examples
//!
//! ```no_run
//! use eeyf::{YahooConnector, batch::BatchQuoteRequest};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let provider = YahooConnector::new()?;
//!     
//!     let symbols = vec!["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA"];
//!     let batch = BatchQuoteRequest::new(symbols)
//!         .with_concurrency(10)
//!         .with_continue_on_error(true);
//!     
//!     let results = provider.batch_get_quote(&batch).await?;
//!     
//!     for result in results {
//!         match result {
//!             Ok(quote) => println!("{}: ${:.2}", quote.symbol, quote.regular_market_price),
//!             Err(e) => eprintln!("Error: {}", e),
//!         }
//!     }
//!     
//!     Ok(())
//! }
//! ```

use crate::YahooError;
use futures_util::stream::{self, StreamExt};
use std::time::Duration;

/// Configuration for batch quote requests
#[derive(Debug, Clone)]
pub struct BatchQuoteRequest {
    /// List of symbols to fetch
    pub symbols: Vec<String>,
    /// Maximum number of concurrent requests (default: 10)
    pub concurrency: usize,
    /// Whether to continue processing on individual symbol errors (default: true)
    pub continue_on_error: bool,
    /// Timeout per symbol request in seconds (default: 30)
    pub timeout_secs: u64,
}

impl BatchQuoteRequest {
    /// Create a new batch request for the given symbols
    pub fn new<S: AsRef<str>>(symbols: Vec<S>) -> Self {
        Self {
            symbols: symbols.iter().map(|s| s.as_ref().to_string()).collect(),
            concurrency: 10,
            continue_on_error: true,
            timeout_secs: 30,
        }
    }

    /// Set the maximum number of concurrent requests
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        self.concurrency = concurrency.max(1).min(50); // Clamp between 1 and 50
        self
    }

    /// Set whether to continue on individual symbol errors
    pub fn with_continue_on_error(mut self, continue_on_error: bool) -> Self {
        self.continue_on_error = continue_on_error;
        self
    }

    /// Set the timeout per symbol request
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout_secs = timeout.as_secs().max(1).min(300); // 1 to 300 seconds
        self
    }
}

/// Result of a batch operation with progress tracking
#[derive(Debug)]
pub struct BatchResult<T> {
    /// Successful results with their symbols
    pub results: Vec<(String, T)>,
    /// Failed symbols with their errors
    pub errors: Vec<(String, YahooError)>,
    /// Total symbols processed
    pub total: usize,
    /// Number of successful fetches
    pub successful: usize,
    /// Number of failed fetches
    pub failed: usize,
}

impl<T> BatchResult<T> {
    /// Create a new empty batch result
    pub fn new(total: usize) -> Self {
        Self {
            results: Vec::with_capacity(total),
            errors: Vec::new(),
            total,
            successful: 0,
            failed: 0,
        }
    }

    /// Add a successful result
    pub fn add_success(&mut self, symbol: String, result: T) {
        self.results.push((symbol, result));
        self.successful += 1;
    }

    /// Add a failed result
    pub fn add_error(&mut self, symbol: String, error: YahooError) {
        self.errors.push((symbol, error));
        self.failed += 1;
    }

    /// Get success rate as a percentage
    pub fn success_rate(&self) -> f64 {
        if self.total == 0 {
            0.0
        } else {
            (self.successful as f64 / self.total as f64) * 100.0
        }
    }

    /// Check if all requests were successful
    pub fn is_complete_success(&self) -> bool {
        self.failed == 0 && self.successful == self.total
    }

    /// Get all successful results, consuming self
    pub fn into_results(self) -> Vec<(String, T)> {
        self.results
    }

    /// Get all errors, consuming self
    pub fn into_errors(self) -> Vec<(String, YahooError)> {
        self.errors
    }
}

/// Progress callback for batch operations
pub type ProgressCallback = Box<dyn Fn(usize, usize) + Send + Sync>;

/// Batch operations implementation
pub struct BatchOperations<'a, T> {
    fetch_fn: Box<dyn Fn(String) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T, YahooError>> + Send + 'a>> + Send + Sync + 'a>,
    progress_callback: Option<ProgressCallback>,
}

impl<'a, T: Send + 'a> BatchOperations<'a, T> {
    /// Create a new batch operations handler
    pub fn new<F, Fut>(fetch_fn: F) -> Self
    where
        F: Fn(String) -> Fut + Send + Sync + 'a,
        Fut: std::future::Future<Output = Result<T, YahooError>> + Send + 'a,
    {
        Self {
            fetch_fn: Box::new(move |symbol| Box::pin(fetch_fn(symbol))),
            progress_callback: None,
        }
    }

    /// Set a progress callback that will be called after each symbol is processed
    pub fn with_progress<F>(mut self, callback: F) -> Self
    where
        F: Fn(usize, usize) + Send + Sync + 'static,
    {
        self.progress_callback = Some(Box::new(callback));
        self
    }

    /// Execute the batch operation
    pub async fn execute(self, request: BatchQuoteRequest) -> BatchResult<T> {
        let total = request.symbols.len();
        let mut result = BatchResult::new(total);
        let mut completed = 0usize;

        // Create a stream of futures
        let futures = stream::iter(request.symbols.into_iter().map(|symbol| {
            let fetch_fn = &self.fetch_fn;
            async move {
                let symbol_clone = symbol.clone();
                match tokio::time::timeout(
                    Duration::from_secs(request.timeout_secs),
                    (fetch_fn)(symbol.clone()),
                )
                .await
                {
                    Ok(Ok(data)) => Ok((symbol_clone, data)),
                    Ok(Err(e)) => Err((symbol_clone, e)),
                    Err(_) => Err((
                        symbol_clone,
                        YahooError::ConnectionFailed(format!(
                            "Request timeout after {} seconds",
                            request.timeout_secs
                        )),
                    )),
                }
            }
        }));

        // Process with concurrency limit
        let mut stream = futures.buffer_unordered(request.concurrency);

        while let Some(outcome) = stream.next().await {
            completed += 1;

            match outcome {
                Ok((symbol, data)) => {
                    result.add_success(symbol, data);
                }
                Err((symbol, error)) => {
                    result.add_error(symbol, error);
                    if !request.continue_on_error {
                        // Drain remaining futures without processing
                        while let Some(_) = stream.next().await {
                            completed += 1;
                        }
                        break;
                    }
                }
            }

            // Call progress callback if set
            if let Some(ref callback) = self.progress_callback {
                callback(completed, total);
            }
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_batch_request_creation() {
        let symbols = vec!["AAPL", "GOOGL", "MSFT"];
        let request = BatchQuoteRequest::new(symbols);

        assert_eq!(request.symbols.len(), 3);
        assert_eq!(request.concurrency, 10);
        assert!(request.continue_on_error);
        assert_eq!(request.timeout_secs, 30);
    }

    #[test]
    fn test_batch_request_builder() {
        let request = BatchQuoteRequest::new(vec!["AAPL"])
            .with_concurrency(5)
            .with_continue_on_error(false)
            .with_timeout(Duration::from_secs(60));

        assert_eq!(request.concurrency, 5);
        assert!(!request.continue_on_error);
        assert_eq!(request.timeout_secs, 60);
    }

    #[test]
    fn test_concurrency_clamping() {
        let request = BatchQuoteRequest::new(vec!["AAPL"]).with_concurrency(100);
        assert_eq!(request.concurrency, 50); // Should be clamped to max 50

        let request = BatchQuoteRequest::new(vec!["AAPL"]).with_concurrency(0);
        assert_eq!(request.concurrency, 1); // Should be clamped to min 1
    }

    #[test]
    fn test_timeout_clamping() {
        let request = BatchQuoteRequest::new(vec!["AAPL"]).with_timeout(Duration::from_secs(500));
        assert_eq!(request.timeout_secs, 300); // Should be clamped to max 300

        let request = BatchQuoteRequest::new(vec!["AAPL"]).with_timeout(Duration::from_secs(0));
        assert_eq!(request.timeout_secs, 1); // Should be clamped to min 1
    }

    #[test]
    fn test_batch_result_tracking() {
        let mut result = BatchResult::<String>::new(5);

        result.add_success("AAPL".to_string(), "data1".to_string());
        result.add_success("GOOGL".to_string(), "data2".to_string());
        result.add_error(
            "MSFT".to_string(),
            YahooError::FetchFailed("error".to_string()),
        );

        assert_eq!(result.successful, 2);
        assert_eq!(result.failed, 1);
        assert_eq!(result.total, 5);
        assert!(!result.is_complete_success());
    }

    #[test]
    fn test_success_rate_calculation() {
        let mut result = BatchResult::<String>::new(10);

        for i in 0..7 {
            result.add_success(format!("SYM{}", i), "data".to_string());
        }
        for i in 7..10 {
            result.add_error(
                format!("SYM{}", i),
                YahooError::FetchFailed("error".to_string()),
            );
        }

        assert_eq!(result.success_rate(), 70.0);
    }

    #[tokio::test]
    async fn test_batch_operations_success() {
        let fetch_fn = |symbol: String| async move {
            Ok::<_, YahooError>(format!("data_{}", symbol))
        };

        let batch_ops = BatchOperations::new(fetch_fn);
        let request = BatchQuoteRequest::new(vec!["AAPL", "GOOGL", "MSFT"]);

        let result = batch_ops.execute(request).await;

        assert_eq!(result.successful, 3);
        assert_eq!(result.failed, 0);
        assert!(result.is_complete_success());
        assert_eq!(result.success_rate(), 100.0);
    }

    #[tokio::test]
    async fn test_batch_operations_with_errors() {
        let fetch_fn = |symbol: String| async move {
            if symbol == "FAIL" {
                Err(YahooError::FetchFailed("error".to_string()))
            } else {
                Ok::<_, YahooError>(format!("data_{}", symbol))
            }
        };

        let batch_ops = BatchOperations::new(fetch_fn);
        let request = BatchQuoteRequest::new(vec!["AAPL", "FAIL", "GOOGL"]);

        let result = batch_ops.execute(request).await;

        assert_eq!(result.successful, 2);
        assert_eq!(result.failed, 1);
        assert!(!result.is_complete_success());
    }

    #[tokio::test]
    async fn test_batch_operations_stop_on_error() {
        let fetch_fn = |symbol: String| async move {
            if symbol == "FAIL" {
                Err(YahooError::FetchFailed("error".to_string()))
            } else {
                Ok::<_, YahooError>(format!("data_{}", symbol))
            }
        };

        let batch_ops = BatchOperations::new(fetch_fn);
        let request = BatchQuoteRequest::new(vec!["AAPL", "FAIL", "GOOGL", "MSFT"])
            .with_continue_on_error(false);

        let result = batch_ops.execute(request).await;

        // Should stop after encountering error
        assert!(result.failed > 0);
        assert!(result.successful + result.failed <= 4);
    }

    #[tokio::test]
    async fn test_batch_operations_with_progress() {
        use std::sync::Arc;
        use std::sync::Mutex;

        let progress_calls = Arc::new(Mutex::new(Vec::new()));
        let progress_calls_clone = progress_calls.clone();

        let fetch_fn = |symbol: String| async move {
            tokio::time::sleep(Duration::from_millis(10)).await;
            Ok::<_, YahooError>(format!("data_{}", symbol))
        };

        let batch_ops = BatchOperations::new(fetch_fn).with_progress(move |completed, total| {
            progress_calls_clone
                .lock()
                .unwrap()
                .push((completed, total));
        });

        let request = BatchQuoteRequest::new(vec!["AAPL", "GOOGL", "MSFT"]);
        let result = batch_ops.execute(request).await;

        assert_eq!(result.successful, 3);

        let calls = progress_calls.lock().unwrap();
        assert_eq!(calls.len(), 3); // Should have 3 progress calls
        assert_eq!(calls.last(), Some(&(3, 3))); // Last call should be (3, 3)
    }
}