Skip to content

Commit

Permalink
Fix slow LeaderboardServices.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Layoric committed Apr 6, 2024
1 parent 15de462 commit d572b8c
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions MyApp.ServiceInterface/LeaderboardServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ private CalculateLeaderboardResponse CalculateLeaderboardResponse(List<StatTotal
var statQuestions = statTotals.Where(x => !x.Id.Contains('-')).ToList();

// There might not be answers to some questions which we want to exclude from the win rate
statQuestions = statQuestions.Where(x => answers.Any(y => y.PostId == x.PostId)).ToList();
var questionAnswerDict = answers.GroupBy(x => x.PostId).ToDictionary(x => x.Key, x => x.ToList());
questionAnswerDict = questionAnswerDict.Where(x => x.Value.Count > 0).ToDictionary(x => x.Key, x => x.Value);
// Now filtering is done, put all the answers back into a list
statQuestions = statQuestions.Where(x => questionAnswerDict.ContainsKey(x.PostId)).ToList();

var overallWinRates = statsByUser.GroupBy(x => x.Id).Select(y =>
{
Expand All @@ -58,6 +61,18 @@ private CalculateLeaderboardResponse CalculateLeaderboardResponse(List<StatTotal

var modelScale = 1 / overallWinRates.Where(x => IsHuman(x.Id) == false)
.Sum(y => y.WinRate);
var humanScale = 1 / overallWinRates.Where(x => IsHuman(x.Id))
.Sum(y => y.WinRate);
var skipScale = double.IsInfinity(modelScale);
if (skipScale)
{
modelScale = 1;
}
var skipHumanScale = double.IsInfinity(humanScale);
if (skipHumanScale)
{
humanScale = 1;
}


var leaderBoard = new CalculateLeaderboardResponse
Expand All @@ -77,12 +92,19 @@ private CalculateLeaderboardResponse CalculateLeaderboardResponse(List<StatTotal
StartingUpVotes = x.GetScore()
})
.ToList(),

AnswererWinRate = overallWinRates,
HumanWinRate = overallWinRates.Where(x => IsHuman(x.Id))
.Select(x => new LeaderBoardWinRate
{
Id = x.Id,
WinRate = x.WinRate * (skipHumanScale ? 1 : humanScale) * 100
}).ToList(),
ModelWinRate = overallWinRates.Where(x => IsHuman(x.Id) == false)
.Select(x => new ModelWinRate
{
Id = x.Id,
WinRate = x.WinRate * modelScale * 100
WinRate = x.WinRate * (skipScale ? 1 : modelScale) * 100
}).ToList(),
ModelTotalScore = statsByUser.GroupBy(x => x.Id)
.Select(x => new ModelTotalScore
Expand Down Expand Up @@ -143,6 +165,32 @@ FROM main.StatTotals st

return CalculateLeaderboardResponse(statTotals,statsByUser,answers);
}

public async Task<object> Any(GetLeaderboardStatsHuman request)
{
var statTotals = Db.Select<StatTotals>(@"
select * from main.StatTotals where PostId in (select PostId from StatTotals
where PostId in (select StatTotals.PostId from StatTotals
where Id like '%-accepted')
group by PostId) and (Id like '%-accepted' or Id like '%-most-voted' or Id not like '%-%')");
// filter to answers only
var answers = statTotals.Where(x => x.Id.Contains('-')).ToList();
// Sum up votes by model, first group by UserName
var statsByUser = answers.GroupBy(x => x.Id.SplitOnFirst('-')[1]).Select(x => new StatTotals
{
Id = x.Key,
UpVotes = x.Sum(y => y.UpVotes),
DownVotes = x.Sum(y => y.DownVotes),
StartingUpVotes = x.Sum(y => y.StartingUpVotes),
FavoriteCount = x.Sum(y => y.FavoriteCount)
}).ToList();

return CalculateLeaderboardResponse(statTotals,statsByUser,answers);
}
}

public class GetLeaderboardStatsHuman
{
}

public class GetLeaderboardStatsByTag
Expand All @@ -161,6 +209,7 @@ public class CalculateLeaderboardResponse
public List<ModelTotalScore> ModelTotalScore { get; set; }
public List<ModelTotalScoreByTag> ModelTotalScoreByTag { get; set; }
public List<ModelWinRate> ModelWinRate { get; set; }
public List<LeaderBoardWinRate> HumanWinRate { get; set; }
}

public class ModelTotalScoreByTag
Expand Down

0 comments on commit d572b8c

Please sign in to comment.