public void RunUniqueGamesPass(int ingestionId = 0)
        {
            using (var db = new DataContext())
            {
                var processingRun = new ProcessingRun { DateTime = DateTime.UtcNow };
                ingestionId = ingestionId == 0 ? GetLatestIngestionId(db) : ingestionId;

                var latestMatches = db.Matches.Where(x => x.Ingestion.Id == ingestionId);
                var duplicateMatches = latestMatches.Where(x => latestMatches.Any(y => y.Date == x.Date && x.Id != y.Id));

                // Avoid access to modified closure
                var matches = duplicateMatches;
                var badMatches = duplicateMatches.Where(x => matches.Where(d => d.Date == x.Date).FirstOrDefault(d => d.Decision == "WIN") == null);
                duplicateMatches = duplicateMatches.Except(badMatches);

                var populatedMatches = duplicateMatches.Include(x => x.LadderMember).Select(x => new
                    {
                        Date = x.Date,
                        LadderMember1 = x.LadderMember,
                        LadderMember2 = duplicateMatches.Where(y => x.Date == y.Date).FirstOrDefault(y => x.Id != y.Id).LadderMember,
                        Map = x.Map,
                        Speed = x.Speed,
                        Type = x.Type
                    }
                );

                var ingestion = db.Ingestions.FirstOrDefault(i => i.Id == ingestionId);
                foreach (var populatedMatch in populatedMatches)
                {
                    //Make sure that we only store matches once from this ingestion
                    var date = UnixTimeStampToDateTime(populatedMatch.Date);
                    var uniqueMatches = new List<UniqueGmMatch>();
                    if (uniqueMatches.Any(u => DateTime.Equals(date, u.DateTime)))
                    {
                        continue;
                    }

                    var hydratedMatch = new UniqueGmMatch
                    {
                        DateTime = date,
                        Ingestion = ingestion,
                        LadderMember1 = populatedMatch.LadderMember1,
                        LadderMember2 = populatedMatch.LadderMember2,
                        Map = populatedMatch.Map,
                        Speed = populatedMatch.Speed,
                        Type = populatedMatch.Type,
                        Winner = populatedMatch.LadderMember1.Matches.First(x => x.Date == populatedMatch.Date).Decision == "WIN" ? populatedMatch.LadderMember1 : populatedMatch.LadderMember2,
                        ProcessingRun = processingRun
                    };
                    uniqueMatches.Add(hydratedMatch);
                    db.UniqueGmMatches.Add(hydratedMatch);
                }

                db.SaveChanges();
            }
        }
        public void RunNewIngestion()
        {
            var ingestion = new Ingestion
            {
                Time = DateTime.UtcNow
            };

            var ladderMembers = GetGmPlayersWithMatchesForRegion(LadderRegion.NorthAmerica);
            ladderMembers.AddRange(GetGmPlayersWithMatchesForRegion(LadderRegion.Europe));
            ladderMembers.AddRange(GetGmPlayersWithMatchesForRegion(LadderRegion.Korea));

            ingestion.LadderMembers = ladderMembers;

            using (var db = new DataContext())
            {
                db.Ingestions.Add(ingestion);
                db.SaveChanges();
            }
        }
 protected void Save(string jsonResults)
 {
     using (var db = new DataContext())
     {
         var latestRun = db.ProcessingRuns.OrderByDescending(x => x.Id).First();
         var output = new PostProcessingOutput
         {
             PostProcessingJobType = JobType,
             ProcessingRun = latestRun,
             ProcessingRunId = latestRun.Id,
             JsonResults = jsonResults
         };
         db.PostProcessingOutputs.Add(output);
         db.SaveChanges();
     }
 }