From fd58b4f8096fa9006fe4420878631411c99953fb Mon Sep 17 00:00:00 2001 From: dullbananas Date: Tue, 2 Jul 2024 06:40:18 -0700 Subject: [PATCH] Exponential controversy rank (#4872) * Update utils.sql * add migration --- crates/db_schema/replaceable_schema/utils.sql | 2 +- .../down.sql | 17 +++++++++++++++++ .../up.sql | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 migrations/2024-07-01-014711_exponential_controversy/down.sql create mode 100644 migrations/2024-07-01-014711_exponential_controversy/up.sql diff --git a/crates/db_schema/replaceable_schema/utils.sql b/crates/db_schema/replaceable_schema/utils.sql index f236c5387..26447f2c2 100644 --- a/crates/db_schema/replaceable_schema/utils.sql +++ b/crates/db_schema/replaceable_schema/utils.sql @@ -8,7 +8,7 @@ CREATE FUNCTION r.controversy_rank (upvotes numeric, downvotes numeric) 0 ELSE ( - upvotes + downvotes) * CASE WHEN upvotes > downvotes THEN + upvotes + downvotes) ^ CASE WHEN upvotes > downvotes THEN downvotes::float / upvotes::float ELSE upvotes::float / downvotes::float diff --git a/migrations/2024-07-01-014711_exponential_controversy/down.sql b/migrations/2024-07-01-014711_exponential_controversy/down.sql new file mode 100644 index 000000000..15a7f296a --- /dev/null +++ b/migrations/2024-07-01-014711_exponential_controversy/down.sql @@ -0,0 +1,17 @@ +UPDATE + post_aggregates +SET + controversy_rank = CASE WHEN downvotes <= 0 + OR upvotes <= 0 THEN + 0 + ELSE + (upvotes + downvotes) * CASE WHEN upvotes > downvotes THEN + downvotes::float / upvotes::float + ELSE + upvotes::float / downvotes::float + END + END +WHERE + upvotes > 0 + AND downvotes > 0; + diff --git a/migrations/2024-07-01-014711_exponential_controversy/up.sql b/migrations/2024-07-01-014711_exponential_controversy/up.sql new file mode 100644 index 000000000..87a700de3 --- /dev/null +++ b/migrations/2024-07-01-014711_exponential_controversy/up.sql @@ -0,0 +1,17 @@ +UPDATE + post_aggregates +SET + controversy_rank = CASE WHEN downvotes <= 0 + OR upvotes <= 0 THEN + 0 + ELSE + (upvotes + downvotes) ^ CASE WHEN upvotes > downvotes THEN + downvotes::float / upvotes::float + ELSE + upvotes::float / downvotes::float + END + END +WHERE + upvotes > 0 + AND downvotes > 0; +