From 93a512b7a16bba3b399cd78003995fab694139a9 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 20 Apr 2019 16:47:23 -0700 Subject: [PATCH] Adding negative score fix - Fixes #84. - Fixing unit tests. --- docs/ranking.md | 4 ++-- server/migrations/2019-03-30-212058_create_post_view/up.sql | 2 +- server/src/actions/comment_view.rs | 1 + server/src/actions/moderator.rs | 1 + server/src/actions/post_view.rs | 4 ++-- ui/src/utils.ts | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/ranking.md b/docs/ranking.md index f55a12842..34348c30f 100644 --- a/docs/ranking.md +++ b/docs/ranking.md @@ -12,14 +12,14 @@ The [Hacker New's ranking algorithm](https://medium.com/hacking-and-gonzo/how-ha ## My Algorithm ``` -Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity +Rank = ScaleFactor * sign(3 + Score) * log(abs(3 + Score)) / (Time + 2)^Gravity Score = Upvotes - Downvotes Time = time since submission (in hours) Gravity = Decay gravity, 1.8 is default ``` -- Add 1 to the score, so that the standard new comment score of +1 will be affected by time decay. Otherwise all new comments would stay at zero, near the bottom. +- Add 3 to the score, so that even minimally downvoted comments will be affected by time decay. Otherwise all new comments would stay at zero, near the bottom. - The sign and abs of the score are necessary for dealing with the log of negative scores. - A scale factor of 10k gets the rank in integer form. diff --git a/server/migrations/2019-03-30-212058_create_post_view/up.sql b/server/migrations/2019-03-30-212058_create_post_view/up.sql index 2f71b6fb9..3a509e29d 100644 --- a/server/migrations/2019-03-30-212058_create_post_view/up.sql +++ b/server/migrations/2019-03-30-212058_create_post_view/up.sql @@ -5,7 +5,7 @@ create or replace function hot_rank( returns integer as $$ begin -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600 - return floor(10000*sign(score)*log(1 + abs(score)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer; + return floor(10000*sign(3+score)*log(abs(3+score)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer; end; $$ LANGUAGE plpgsql; diff --git a/server/src/actions/comment_view.rs b/server/src/actions/comment_view.rs index e8b96e3ae..2e3ae0587 100644 --- a/server/src/actions/comment_view.rs +++ b/server/src/actions/comment_view.rs @@ -294,6 +294,7 @@ mod tests { post_id: inserted_post.id, parent_id: None, removed: None, + read: None, updated: None }; diff --git a/server/src/actions/moderator.rs b/server/src/actions/moderator.rs index a97b21202..a0d7db6c8 100644 --- a/server/src/actions/moderator.rs +++ b/server/src/actions/moderator.rs @@ -465,6 +465,7 @@ mod tests { creator_id: inserted_user.id, post_id: inserted_post.id, removed: None, + read: None, parent_id: None, updated: None }; diff --git a/server/src/actions/post_view.rs b/server/src/actions/post_view.rs index 28e5fb984..ba42fe277 100644 --- a/server/src/actions/post_view.rs +++ b/server/src/actions/post_view.rs @@ -259,7 +259,7 @@ mod tests { score: 1, upvotes: 1, downvotes: 0, - hot_rank: 864, + hot_rank: 1728, published: inserted_post.published, updated: None, subscribed: None, @@ -284,7 +284,7 @@ mod tests { score: 1, upvotes: 1, downvotes: 0, - hot_rank: 864, + hot_rank: 1728, published: inserted_post.published, updated: None, subscribed: None, diff --git a/ui/src/utils.ts b/ui/src/utils.ts index 61744e90a..70b6e8461 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -21,7 +21,7 @@ export function hotRank(comment: Comment): number { let now: Date = new Date(); let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5; - let rank = (10000 * Math.sign(comment.score) * Math.log10(1 + Math.abs(comment.score))) / Math.pow(hoursElapsed + 2, 1.8); + let rank = (10000 * Math.sign(3+comment.score) * Math.log10(Math.abs(3+comment.score))) / Math.pow(hoursElapsed + 2, 1.8); // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);