BUGFIX: improved top ranking formula (+ filter topics based on their creation date)

This commit is contained in:
Régis Hanol 2014-05-05 23:05:35 +02:00
parent fd153623bb
commit f20fba041c

View File

@ -97,19 +97,24 @@ class TopTopic < ActiveRecord::Base
end end
def self.compute_top_score_for(period) def self.compute_top_score_for(period)
# log(views) + (posts * likes) sql = <<-SQL
exec_sql("UPDATE top_topics WITH top AS (
SET #{period}_score = CASE SELECT CASE
WHEN #{period}_views_count = 0 THEN 0 WHEN topics.created_at < :from THEN 0
ELSE log(#{period}_views_count) + (#{period}_posts_count * #{period}_likes_count) ELSE log(greatest(#{period}_views_count, 1)) + #{period}_likes_count + #{period}_posts_count * 2
END END AS score,
WHERE topic_id
#{period}_score <> CASE FROM top_topics
WHEN #{period}_views_count = 0 THEN 0 LEFT JOIN topics ON topics.id = top_topics.topic_id
ELSE log(#{period}_views_count) + (#{period}_posts_count * #{period}_likes_count) )
END 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 end
def self.start_of(period) def self.start_of(period)