public async Task OnGetAsync(CancellationToken cancellationToken) { using (var scope = Services.CreateScope()) { var dbContext = scope.ServiceProvider .GetRequiredService <CustomerInfoDbContext>(); CustomerList = await dbContext.CustomerInfo.ToListAsync(); //IEnumerable<StateStats> statesInfo = stateData.GetStateStats().Result; IStateData stateData = scope.ServiceProvider.GetRequiredService <IStateData>(); if (stateData != null) { IEnumerable <StateStats> statesInfo = stateData.GetStateStats().Result; foreach (var recipient in CustomerList) { Console.WriteLine("in background task"); Console.WriteLine("BEFORE email: " + recipient.Email); StateStats stateOfInterest = statesInfo.FirstOrDefault(x => x.State == recipient.State.ToUpper()); Console.WriteLine("email: " + recipient.Email); Console.WriteLine("stateOfInterest: " + stateOfInterest.State); Execute(recipient.Email, recipient.Name, recipient.State, stateOfInterest).Wait(); } } await Task.CompletedTask; } }
public async Task OnGetAsync() { // get the puzzles and teams // TODO: Filter puzzles if an author; no need to filter teams. Revisit when authors exist. var puzzles = await _context.Puzzles.Where(p => p.Event == this.Event).Select(p => new PuzzleStats() { Puzzle = p }).ToListAsync(); var teams = await _context.Teams.Where(t => t.Event == this.Event).Select(t => new TeamStats() { Team = t }).ToListAsync(); // build an ID-based lookup for puzzles and teams var puzzleLookup = new Dictionary <int, PuzzleStats>(); puzzles.ForEach(p => puzzleLookup[p.Puzzle.ID] = p); var teamLookup = new Dictionary <int, TeamStats>(); teams.ForEach(t => teamLookup[t.Team.ID] = t); // tabulate solve counts and team scores var states = await PuzzleStateHelper.GetSparseQuery(_context, this.Event, null, null).ToListAsync(); var stateList = new List <StateStats>(states.Count); foreach (var state in states) { // TODO: Is it more performant to prefilter the states if an author, or is this sufficient? if (!puzzleLookup.TryGetValue(state.PuzzleID, out PuzzleStats puzzle) || !teamLookup.TryGetValue(state.TeamID, out TeamStats team)) { continue; } stateList.Add(new StateStats() { Puzzle = puzzle, Team = team, UnlockedTime = state.UnlockedTime, SolvedTime = state.SolvedTime }); if (state.IsSolved) { puzzle.SolveCount++; team.SolveCount++; team.Score += puzzle.Puzzle.SolveValue; if (puzzle.Puzzle.IsFinalPuzzle) { team.FinalMetaSolveTime = state.SolvedTime.Value; } } } // sort puzzles by solve count, add the sort index to the lookup puzzles = puzzles.OrderByDescending(p => p.SolveCount).ThenBy(p => p.Puzzle.Name).ToList(); for (int i = 0; i < puzzles.Count; i++) { puzzles[i].SortOrder = i; } // sort teams by metameta/score, add the sort index to the lookup teams = teams.OrderBy(t => t.FinalMetaSolveTime).ThenByDescending(t => t.Score).ThenBy(t => t.Team.Name).ToList(); for (int i = 0; i < teams.Count; i++) { if (i == 0 || teams[i].FinalMetaSolveTime != teams[i - 1].FinalMetaSolveTime || teams[i].Score != teams[i - 1].Score) { teams[i].Rank = i + 1; } teams[i].SortOrder = i; } // Build the map var stateMap = new StateStats[puzzles.Count, teams.Count]; stateList.ForEach(state => stateMap[state.Puzzle.SortOrder, state.Team.SortOrder] = state); this.Puzzles = puzzles; this.Teams = teams; this.StateMap = stateMap; }
public async Task <IActionResult> OnGetAsync(int?refresh) { if (refresh != null) { Refresh = refresh; } // get the puzzles and teams List <PuzzleStats> puzzles; if (EventRole == EventRole.admin) { puzzles = await _context.Puzzles.Where(p => p.Event == Event) .Select(p => new PuzzleStats() { Puzzle = p }) .ToListAsync(); } else { puzzles = await UserEventHelper.GetPuzzlesForAuthorAndEvent(_context, Event, LoggedInUser) .Select(p => new PuzzleStats() { Puzzle = p }) .ToListAsync(); } List <TeamStats> teams = await _context.Teams.Where(t => t.Event == Event) .Select(t => new TeamStats() { Team = t }) .ToListAsync(); // build an ID-based lookup for puzzles and teams Dictionary <int, PuzzleStats> puzzleLookup = new Dictionary <int, PuzzleStats>(); puzzles.ForEach(p => puzzleLookup[p.Puzzle.ID] = p); Dictionary <int, TeamStats> teamLookup = new Dictionary <int, TeamStats>(); teams.ForEach(t => teamLookup[t.Team.ID] = t); // tabulate solve counts and team scores List <PuzzleStatePerTeam> states = await PuzzleStateHelper.GetSparseQuery( _context, Event, null, null, EventRole == EventRole.admin?null : LoggedInUser).ToListAsync(); List <StateStats> stateList = new List <StateStats>(states.Count); foreach (PuzzleStatePerTeam state in states) { // TODO: Is it more performant to prefilter the states if an author, or is this sufficient? if (!puzzleLookup.TryGetValue(state.PuzzleID, out PuzzleStats puzzle) || !teamLookup.TryGetValue(state.TeamID, out TeamStats team)) { continue; } stateList.Add(new StateStats() { Puzzle = puzzle, Team = team, UnlockedTime = state.UnlockedTime, SolvedTime = state.SolvedTime, LockedOut = state.IsEmailOnlyMode }); if (state.SolvedTime != null) { puzzle.SolveCount++; team.SolveCount++; team.Score += puzzle.Puzzle.SolveValue; if (puzzle.Puzzle.IsCheatCode) { team.CheatCodeUsed = true; team.FinalMetaSolveTime = DateTime.MaxValue; } if (puzzle.Puzzle.IsFinalPuzzle && !team.CheatCodeUsed) { team.FinalMetaSolveTime = state.SolvedTime.Value; } } } // sort puzzles by group, then solve count, add the sort index to the lookup // but put non-puzzles to the end puzzles = puzzles.OrderByDescending(p => p.Puzzle.IsPuzzle) .ThenByDescending(p => p.Puzzle.Group) .ThenBy(p => p.SolveCount) .ThenBy(p => p.Puzzle.Name) .ToList(); for (int i = 0; i < puzzles.Count; i++) { puzzles[i].SortOrder = i; } // sort teams by metameta/score, add the sort index to the lookup teams = teams.OrderBy(t => t.FinalMetaSolveTime) .ThenByDescending(t => t.Score) .ThenBy(t => t.Team.Name) .ToList(); for (int i = 0; i < teams.Count; i++) { if (i == 0 || teams[i].FinalMetaSolveTime != teams[i - 1].FinalMetaSolveTime || teams[i].Score != teams[i - 1].Score) { teams[i].Rank = i + 1; } teams[i].SortOrder = i; } // Build the map var stateMap = new StateStats[puzzles.Count, teams.Count]; stateList.ForEach(state => stateMap[state.Puzzle.SortOrder, state.Team.SortOrder] = state); Puzzles = puzzles; Teams = teams; StateMap = stateMap; return(Page()); }
static async Task Execute(string recepiantEmail, string recepiantName, string location, StateStats stateOfInterest) { var apiKey = Environment.GetEnvironmentVariable("covidApiKey"); var client = new SendGridClient(apiKey); var from = new EmailAddress("*****@*****.**", "Queen Sumaiya"); var subject = "COVID-19 Case Number Update"; var to = new EmailAddress(recepiantEmail, recepiantName); var plainTextContent = location + " : and easy to do anywhere, even with C#"; var htmlContent = "<strong>" + location + "</strong><p>confirmed:" + stateOfInterest.Positive + "</p><p> deaths: " + stateOfInterest.Death + "</p><p> recovered: " + stateOfInterest.Recovered + "</p>"; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent); var response = await client.SendEmailAsync(msg); }