From f20fba041c601f775270f24c4901cf00d62a2c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 5 May 2014 23:05:35 +0200 Subject: [PATCH] BUGFIX: improved top ranking formula (+ filter topics based on their creation date) --- app/models/top_topic.rb | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/app/models/top_topic.rb b/app/models/top_topic.rb index 68882ea5de9..352677a48b1 100644 --- a/app/models/top_topic.rb +++ b/app/models/top_topic.rb @@ -97,19 +97,24 @@ class TopTopic < ActiveRecord::Base end def self.compute_top_score_for(period) - # log(views) + (posts * likes) - exec_sql("UPDATE top_topics - SET #{period}_score = CASE - WHEN #{period}_views_count = 0 THEN 0 - ELSE log(#{period}_views_count) + (#{period}_posts_count * #{period}_likes_count) - END - WHERE - #{period}_score <> CASE - WHEN #{period}_views_count = 0 THEN 0 - ELSE log(#{period}_views_count) + (#{period}_posts_count * #{period}_likes_count) - END + sql = <<-SQL + WITH top AS ( + SELECT CASE + WHEN topics.created_at < :from THEN 0 + ELSE log(greatest(#{period}_views_count, 1)) + #{period}_likes_count + #{period}_posts_count * 2 + END AS score, + topic_id + FROM top_topics + LEFT JOIN topics ON topics.id = top_topics.topic_id + ) + UPDATE top_topics + SET #{period}_score = top.score + FROM top + WHERE top_topics.topic_id = top.topic_id + AND #{period}_score <> top.score + SQL - ") + exec_sql(sql, from: start_of(period)) end def self.start_of(period)