public async Task <AuctionHouseSnapshot> GetSnapshotAsync(AuctionHouseSnapshotMetadata metadata, CancellationToken stoppingToken) { AuctionHouseSnapshot snapshot = null; if (metadata.Files.Count > 1) { var snapshots = new List <AuctionHouseSnapshot>(); Parallel.ForEach(metadata.Files, async file => { var snapshot = await DownloadSnapshotAsync(file.Url, stoppingToken); snapshots.Add(snapshot); }); var aggregate = new AuctionHouseSnapshot(); aggregate.Realms = snapshots.SelectMany(s => s.Realms) .Distinct() .ToList(); aggregate.Auctions = snapshots.SelectMany(s => s.Auctions).ToList(); snapshot = aggregate; } else { snapshot = await DownloadSnapshotAsync(metadata.Files.First().Url, stoppingToken); } snapshot.Timestamp = (long)(metadata.Files.First().LastModified - DateTime.UnixEpoch).TotalMilliseconds; return(snapshot); }
private async Task InsertSnapshotToDbAsync(AuctionHouseSnapshot snapshot, CancellationToken stoppingToken) { try { var snapshotEntry = new Snapshot { Timestamp = snapshot.Timestamp }; snapshotEntry = await AddAsync(_dbContext.Snapshots, snapshotEntry); await _dbContext.SaveChangesAsync(stoppingToken); await foreach (var realm in FindOrInsertRealmAsync(snapshot.Realms, stoppingToken)) { await InsertSnapshotRealmDetailAsync(snapshotEntry.Id, realm, stoppingToken); } var existingAdditionalFields = await _dbContext.NameDetails.Select(d => d.Name).ToListAsync(stoppingToken); var additionalFields = snapshot.Auctions.SelectMany(a => a.AdditionalFields) .Select(a => a.Key) .Distinct() .Where(a => !existingAdditionalFields.Contains(a)) .Select(n => new NameDetail { Name = n }) .ToList(); _dbContext.NameDetails.AddRange(additionalFields); await _dbContext.SaveChangesAsync(stoppingToken); // need to pre-load these and then update distinct entries var nameDetails = _dbContext.NameDetails.Select(d => new KeyValuePair <string, NameDetail>(d.Name, d)); dbNames = new ConcurrentDictionary <string, NameDetail>(nameDetails); var realms = _dbContext.Realms.Select(r => new KeyValuePair <string, Models.Realm>(r.Name, r)); dbRealms = new ConcurrentDictionary <string, Models.Realm>(realms); await InsertAuctionsAsync(snapshotEntry.Id, snapshot.Auctions, stoppingToken); //await InsertAuctionDetailsAsync(snapshot.Auctions, stoppingToken); } catch (Exception ex) { Console.WriteLine(ex); } }
private AuctionHouseSnapshot ParseSnapshot(JObject document) { var snapshot = new AuctionHouseSnapshot(); foreach (var token in document.Children <JProperty>()) { var items = token.Children().Children(); if (token.Name == "realms") { snapshot.Realms = ParseRealms(items).ToList(); } else if (token.Name == "auctions") { snapshot.Auctions = ParseAuctions(items).ToList(); } } return(snapshot); }