Skip to content

Commit

Permalink
Performance fixes for slow pages (I hope)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenny-young committed Apr 11, 2019
1 parent df30f13 commit 4ee3b0e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
13 changes: 11 additions & 2 deletions ServerCore/Pages/Events/FastestSolves.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ public FastestSolvesModel(PuzzleServerContext serverContext, UserManager<Identit

public async Task OnGetAsync()
{
Dictionary<int, string> teamNameLookup = new Dictionary<int, string>();

// build an ID-to-name mapping to improve perf
var names = await _context.Teams.Where(t => t.Event == Event)
.Select(t => new { t.ID, t.Name })
.ToListAsync();

names.ForEach(t => teamNameLookup[t.ID] = t.Name);

// get the page data: puzzle, solve count, top three fastest
var puzzlesData = await PuzzleStateHelper.GetSparseQuery(_context, this.Event, null, null)
.Where(s => s.SolvedTime != null && s.Puzzle.IsPuzzle)
.GroupBy(state => state.Puzzle)
.Select(g => new {
Puzzle = g.Key,
SolveCount = g.Count(),
Fastest = g.OrderBy(s => s.SolvedTime - s.UnlockedTime).Take(3).Select(s => new { s.Team.ID, s.Team.Name, Time = s.SolvedTime - s.UnlockedTime})
Fastest = g.OrderBy(s => s.SolvedTime - s.UnlockedTime).Take(3).Select(s => new { s.Team.ID, Time = s.SolvedTime - s.UnlockedTime})
})
.OrderByDescending(p => p.SolveCount).ThenBy(p => p.Puzzle.Name)
.ToListAsync();
Expand All @@ -40,7 +49,7 @@ public async Task OnGetAsync()
Puzzle = data.Puzzle,
SolveCount = data.SolveCount,
SortOrder = i,
Fastest = data.Fastest.Select(f => new FastRecord() { ID = f.ID, Name = f.Name, Time = f.Time }).ToArray()
Fastest = data.Fastest.Select(f => new FastRecord() { ID = f.ID, Name = teamNameLookup[f.ID], Time = f.Time }).ToArray()
};

puzzles.Add(stats);
Expand Down
6 changes: 3 additions & 3 deletions ServerCore/Pages/Teams/Answers.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@
@item.TimeSubmitted
</td>
<td>
@item.Puzzle.Group
@item.Group
</td>
<td>
@item.Puzzle.Name
@item.Name
</td>
<td>
@item.SubmissionText
</td>
<td>
@item.Response.ResponseText
@item.ResponseText
</td>
</tr>
}
Expand Down
27 changes: 21 additions & 6 deletions ServerCore/Pages/Teams/Answers.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public AnswersModel(PuzzleServerContext serverContext, UserManager<IdentityUser>
{
}


public IList<Submission> CorrectSubmissions { get; set; }
public IList<SubmissionView> CorrectSubmissions { get; set; }

public int TeamID { get; set; }

Expand All @@ -37,9 +36,7 @@ public async Task OnGetAsync(SortOrder? sort, int teamId)
TeamID = teamId;

IQueryable<Submission> submissions = _context.Submissions
.Include((s)=>s.Response)
.Where((s) => s.TeamID == teamId && s.Response.IsSolution)
.Include((s)=>s.Puzzle);
.Where((s) => s.TeamID == teamId && s.Response.IsSolution);

this.Sort = sort;
switch (sort ?? DefaultSort) {
Expand All @@ -65,7 +62,16 @@ public async Task OnGetAsync(SortOrder? sort, int teamId)
throw new Exception("Sort order is not mapped");
}

CorrectSubmissions = await submissions.ToListAsync();
CorrectSubmissions = await submissions
.Select(s => new SubmissionView()
{
TimeSubmitted = s.TimeSubmitted,
Group = s.Puzzle.Group,
Name = s.Puzzle.Name,
SubmissionText = s.SubmissionText,
ResponseText = s.Response.ResponseText
})
.ToListAsync();
}

public SortOrder? SortForColumnLink(SortOrder ascending, SortOrder descending)
Expand All @@ -90,5 +96,14 @@ public enum SortOrder
GroupAscending,
GroupDescending
}

public class SubmissionView
{
public DateTime TimeSubmitted { get; set; }
public string Group { get; set; }
public string Name { get; set; }
public string SubmissionText { get; set; }
public string ResponseText { get; set; }
}
}
}

0 comments on commit 4ee3b0e

Please sign in to comment.