From ecbf943cc28dc14daeff0a6fb3b0cf6e532a1872 Mon Sep 17 00:00:00 2001 From: Darren Reid Date: Wed, 3 Apr 2024 17:05:09 +1100 Subject: [PATCH] Calculate model performance by tag. --- MyApp.ServiceInterface/LeaderboardServices.cs | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/MyApp.ServiceInterface/LeaderboardServices.cs b/MyApp.ServiceInterface/LeaderboardServices.cs index e189f8e..3fbbb70 100644 --- a/MyApp.ServiceInterface/LeaderboardServices.cs +++ b/MyApp.ServiceInterface/LeaderboardServices.cs @@ -33,6 +33,13 @@ public async Task Any(CalculateLeaderBoard request) FavoriteCount = x.Sum(y => y.FavoriteCount) }).ToList(); + var leaderBoard = CalculateLeaderboardResponse(statTotals, statsByUser, answers); + + return leaderBoard; + } + + private CalculateLeaderboardResponse CalculateLeaderboardResponse(List statTotals, List statsByUser, List answers) + { var statQuestions = statTotals.Where(x => !x.Id.Contains('-')).ToList(); var overallWinRates = statsByUser.GroupBy(x => x.Id).Select(y => @@ -80,10 +87,9 @@ public async Task Any(CalculateLeaderBoard request) TotalScore = x.Sum(y => y.GetScore()) }).ToList() }; - return leaderBoard; } - + bool IsHuman(string id) { return id == "accepted" || id == "most-voted"; @@ -108,6 +114,36 @@ double CalculateWinRate(List statTotalsList, string name, int questi winner != null && winner.Id.Contains("-") && winner.Id.SplitOnFirst('-')[1] == name) / questionCount) * 100; } + + public async Task Any(GetLeaderboardStatsByTag request) + { + var statTotals = Db.Select(@"SELECT st.* +FROM main.StatTotals st + JOIN main.post p ON st.PostId = p.Id +WHERE (p.Tags LIKE @TagMiddle OR p.Tags LIKE @TagLeft OR p.Tags LIKE @TagRight OR p.Tags = @TagSolo)", new { TagSolo = $"[{request.Tag}]", + TagRight = $"%,{request.Tag}", + TagLeft = $"{request.Tag},%", + TagMiddle = $",{request.Tag},", + }); + // 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 GetLeaderboardStatsByTag +{ + public string Tag { get; set; } } public class CalculateLeaderboardResponse