public async Task <IActionResult> StarResult(StarSearch search) { int threads = 4; if (ModelState.IsValid) { search.SearchDays = Math.Min(search.SearchDays, 14); search.Destination = search.Destination.MyTrim(); search.Origin = search.Origin.MyTrim(); if (search.Origin.MyLength() == 3 && search.Destination.MyLength() == 3) { var dates = System.Linq.Enumerable.Range(0, search.SearchDays).Select(i => search.OutDate.AddDays(i)).ToList(); dates.Shuffle(); var res = new System.Collections.Concurrent.ConcurrentDictionary <DateTime, FlysasLib.SearchResult>(); await Dasync.Collections.ParallelForEachExtensions.ParallelForEachAsync <DateTime>(dates, async date => { if (!res.ContainsKey(date)) //this looks smart, but doesn't realy save a lot of calls... { var q = new SASQuery { OutDate = date, From = search.Origin, To = search.Destination, Adults = search.Pax, Mode = SASQuery.SearhMode.STAR }; var c = new FlysasLib.SASRestClient(); FlysasLib.SearchResult searchResult = await c.SearchAsync(q); if (searchResult.tabsInfo != null && searchResult.tabsInfo.outboundInfo != null) { foreach (var dayWithNoSeats in searchResult.tabsInfo.outboundInfo.Where(tab => tab.points == 0)) { res.TryAdd(dayWithNoSeats.date, null); } } res.TryAdd(date, searchResult); } }, threads, false ); search.Results = res.Where(r => r.Value?.outboundFlights != null).SelectMany(r => r.Value.outboundFlights).ToList(); if (search.MaxLegs > 0) { search.Results = search.Results.Where(r => r.segments.Count() <= search.MaxLegs).ToList(); } } else { search.Results = new List <FlysasLib.FlightBaseClass>(); } } return(View(nameof(Star), search)); }
public async System.Threading.Tasks.Task Run(string input) { var parser = new Parser(); if (!Command(input)) { foreach (string query in input.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { SASQuery req = null; try { req = parser.Parse(query.Trim()); req.Mode = options.Mode; } catch (ParserException ex) { txtOut.Write("Syntax error:" + ex.Message); } catch { txtOut.Write("Syntax error:"); } if (req != null) { SearchResult result = null; try { result = await client.SearchAsync(req); } catch { txtOut.WriteLine("Error"); } if (result != null) { if (result.errors != null && result.errors.Any()) { txtOut.WriteLine("flysas.com says: " + result.errors.First().errorMessage); } else { var printer = new TablePrinter(txtOut); txtOut.WriteLine("*********Outbound*******"); printer.PrintFlights(result.outboundFlights, options, req.From, req.To); if (req.InDate.HasValue) { txtOut.WriteLine("*********Inbound*******"); printer.PrintFlights(result.inboundFlights, options, req.To, req.From); } } } txtOut.Write(Environment.NewLine + Environment.NewLine); } } } }
private void benchMark() { var count = 40; int threads = 6; var watch = System.Diagnostics.Stopwatch.StartNew(); System.Threading.Tasks.Parallel.For(0, count, new System.Threading.Tasks.ParallelOptions { MaxDegreeOfParallelism = threads }, x => { SASQuery q = new SASQuery { From = "KLR", To = "ARN", OutDate = DateTime.Now.AddDays(1 + x).Date }; var w2 = System.Diagnostics.Stopwatch.StartNew(); var res = client.Search(q); //txtOut.WriteLine("Got " + res.outboundFlights?.Count + " in " + w2.Elapsed.TotalSeconds); }); txtOut.WriteLine(watch.Elapsed.TotalSeconds); }
public SASQuery Parse(string input) { var splitChars = new[] { ' ', ',', '-' }; var stack = new CommandStack(input, splitChars); SASQuery request = null; if (stack.Count > 2) { request = new SASQuery(); request.From = stack.Pop().ToUpper(); if (airportExp.IsMatch(stack.Peek())) { request.To = stack.Pop().ToUpper(); if (airportExp.IsMatch(stack.Peek())) { request.ReturnFrom = stack.Pop().ToUpper(); if (airportExp.IsMatch(stack.Peek())) { //req.ReturnTo = stack.Pop().ToUpper(); var tmp = stack.Pop().ToUpper(); if (tmp != request.From) { throw new ParserException("Must return to origin"); } } } } request.OutDate = parseDate(stack.Pop(), DateTime.Now.Date); if (stack.Any()) { request.InDate = parseDate(stack.Pop(), request.OutDate.Value); } } else { throw new ParserException("Too few arguments"); } return(request); }
public async System.Threading.Tasks.Task InputLoop() { string input = null; var parser = new Parser(); while (!nameof(Commands.Quit).Equals(input, StringComparison.OrdinalIgnoreCase)) { txtOut.WriteLine("Syntax: Origin-Destination outDate [inDate]"); txtOut.Write(">>"); input = txtIn.ReadLine(); if (!Command(input)) { foreach (string query in input.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { SASQuery req = null; try { req = parser.Parse(query); req.Mode = options.Mode; } catch (ParserException ex) { txtOut.Write("Syntax error:" + ex.Message); } catch { txtOut.Write("Syntax error:"); } if (req != null) { SearchResult result = null; try { result = await client.SearchAsync(req); } catch { txtOut.WriteLine("Error"); } if (result != null) { if (result.errors != null && result.errors.Any()) { txtOut.WriteLine("flysas.com says: " + result.errors.First().errorMessage); } else { var printer = new TablePrinter(txtOut); txtOut.WriteLine("*********Outbound*******"); printer.PrintFlights(result.outboundFlights, options); if (req.InDate.HasValue) { txtOut.WriteLine("*********Inbound*******"); printer.PrintFlights(result.inboundFlights, options); } } } txtOut.Write(Environment.NewLine + Environment.NewLine); } } } } }
private async Task <Result> Search(string to, int offsetDays) { SASQuery query = new SASQuery { Mode = "STAR", OutDate = OutStart.AddDays(offsetDays), From = From, To = to, Adt = Pax }; bool hasReturn = InStart.HasValue; if (hasReturn) { query.InDate = InStart.Value.AddDays(offsetDays); } SearchResult result = null; try { result = await client.SearchAsync(query); } catch { Console.Error.WriteLine("Unexpected error in query."); return(new Result { OutBound = new List <FlightBaseClass>(), InBound = new List <FlightBaseClass>() }); } if (result != null) { if (result.errors != null && result.errors.Any() && !HideErrors) { Console.Error.WriteLine("flysas.com says: " + result.errors.First().errorMessage); } { IEnumerable <FlightBaseClass> validOutbound = new List <FlightBaseClass>(); IEnumerable <FlightBaseClass> validInbound = new List <FlightBaseClass>(); if (result.outboundFlights != null) { validOutbound = result.outboundFlights.Where(this.FilterFlight); } if (result.inboundFlights != null) { validInbound = result.inboundFlights.Where(this.FilterFlight); } return(new Result { OutBound = validOutbound, InBound = validInbound }); } } return(new Result { OutBound = new List <FlightBaseClass>(), InBound = new List <FlightBaseClass>() }); }