public async Task <List <Quote> > GetQuotes(List <string> symbols) { var existingQuotes = await _stockData.GetQuotes(); List <Quote> quotesToReuse = new List <Quote>(); List <Quote> quotesToRefresh = new List <Quote>(); foreach (var quote in existingQuotes) { if (DateTimeOffset.UtcNow.Subtract(quote.UpdatedDate).TotalMinutes > 5) { quotesToRefresh.Add(quote); } else { if (symbols.Contains(quote.Symbol)) { symbols.Remove(quote.Symbol); } quotesToReuse.Add(quote); } } var symbolsToRetrieve = quotesToRefresh.Select(q => q.Symbol).Union(symbols).Distinct().ToList(); if (symbolsToRetrieve.Any()) { ConcurrentQueue <Quote> quotes = new ConcurrentQueue <Quote>(); await Task.WhenAll(symbolsToRetrieve.Select(symbol => Task.Run(async() => { var quote = await _iexClient.GetQuote(symbol); quotes.Enqueue(new Quote { Symbol = quote.symbol, Price = quote.latestPrice, QuoteDate = quote.latestUpdate, UpdatedDate = DateTimeOffset.UtcNow }); }))); quotesToRefresh = quotes.ToList(); await _stockData.SaveQuotes(quotesToRefresh); } return(quotesToReuse.Union(quotesToRefresh).ToList()); }
public async Task <List <Quote> > GetQuotes(List <string> symbols) { var existingQuotes = await _stockData.GetQuotes(); List <Quote> quotesToReuse = new List <Quote>(); List <Quote> quotesToRefresh = new List <Quote>(); foreach (var quote in existingQuotes) { if (DateTimeOffset.UtcNow.Subtract(quote.UpdatedDate).TotalMinutes > 5) { quotesToRefresh.Add(quote); } else { if (symbols.Contains(quote.Symbol)) { symbols.Remove(quote.Symbol); } quotesToReuse.Add(quote); } } var symbolsToRetrieve = quotesToRefresh.Select(q => q.Symbol).Union(symbols).Distinct().ToList(); if (symbolsToRetrieve.Any()) { var quotes = await _alphaClient.Execute(x => x.RequestBatchQuotesAsync(symbolsToRetrieve.ToArray())); quotesToRefresh = quotes.Select(q => new Quote { Symbol = q.Symbol, Price = q.Price, QuoteDate = q.Time, UpdatedDate = DateTimeOffset.UtcNow }).ToList(); await _stockData.SaveQuotes(quotesToRefresh); } return(quotesToReuse.Union(quotesToRefresh).ToList()); }
public async Task Test() { var quotes = await _stockDataAccess.GetQuotes(); }