Mix awarded score into ranks

This commit is contained in:
2025-09-08 23:45:57 +08:00
parent c2b49e6642
commit ab2bdcc7ca
2 changed files with 12 additions and 5 deletions

View File

@@ -19,12 +19,12 @@ public class ActivityService(
{ {
private static double CalculateHotRank(Post.Post post, Instant now) private static double CalculateHotRank(Post.Post post, Instant now)
{ {
var performanceScore = post.Upvotes - post.Downvotes + post.RepliesCount; var performanceScore = post.Upvotes - post.Downvotes + post.RepliesCount + (int)post.AwardedScore / 10;
var postTime = post.PublishedAt ?? post.CreatedAt; var postTime = post.PublishedAt ?? post.CreatedAt;
var timeScore = (now - postTime).TotalMinutes; var timeScore = (now - postTime).TotalMinutes;
// Add 1 to score to prevent negative results for posts with more downvotes than upvotes // Add 1 to score to prevent negative results for posts with more downvotes than upvotes
// Time dominates ranking, performance adjusts within similar timeframes. // Time dominates ranking, performance adjusts within similar timeframes.
var performanceWeight = Math.Log(performanceScore + 5); // smooth adjustment, median ~4-5 var performanceWeight = performanceScore + 5;
// Normalize time influence since average post interval ~60 minutes // Normalize time influence since average post interval ~60 minutes
var normalizedTime = timeScore / 60.0; var normalizedTime = timeScore / 60.0;
return performanceWeight / Math.Pow(normalizedTime + 1.0, 1.2); return performanceWeight / Math.Pow(normalizedTime + 1.0, 1.2);

View File

@@ -848,12 +848,19 @@ public partial class PostService(
g => g.Count() g => g.Count()
); );
// Load awardsScores for postsInPeriod
var awardsScores = await db.Posts
.Where(p => postsInPeriod.Contains(p.Id))
.ToDictionaryAsync(p => p.Id, p => p.AwardedScore);
var reactSocialPoints = postsInPeriod var reactSocialPoints = postsInPeriod
.Select(postId => new .Select(postId => new
{ {
PostId = postId, PostId = postId,
Count = (reactionScores.TryGetValue(postId, out var rScore) ? rScore : 0) + Count =
(repliesCounts.TryGetValue(postId, out var repCount) ? repCount : 0) (reactionScores.TryGetValue(postId, out var rScore) ? rScore : 0)
+ (repliesCounts.TryGetValue(postId, out var repCount) ? repCount : 0)
+ (awardsScores.TryGetValue(postId, out var awardScore) ? (int)(awardScore / 10) : 0)
}) })
.OrderByDescending(e => e.Count) .OrderByDescending(e => e.Count)
.Take(5) .Take(5)
@@ -877,7 +884,7 @@ public partial class PostService(
SocialCredits = e.Value SocialCredits = e.Value
}).ToList(); }).ToList();
if (records.Any()) if (records.Count != 0)
{ {
db.PostFeaturedRecords.AddRange(records); db.PostFeaturedRecords.AddRange(records);
await db.SaveChangesAsync(); await db.SaveChangesAsync();