FEATURE: improve the TOP page scoring formula a bit

This commit is contained in:
Régis Hanol 2014-01-02 22:38:35 +01:00
parent a897c2b091
commit edc154251d

View file

@ -36,7 +36,7 @@ class TopTopic < ActiveRecord::Base
end end
def self.update_posts_count_for(period) def self.update_posts_count_for(period)
sql = "SELECT topic_id, COUNT(*) AS count sql = "SELECT topic_id, GREATEST(COUNT(*), 1) AS count
FROM posts p FROM posts p
WHERE p.created_at >= :from WHERE p.created_at >= :from
AND p.deleted_at IS NULL AND p.deleted_at IS NULL
@ -47,7 +47,7 @@ class TopTopic < ActiveRecord::Base
end end
def self.update_views_count_for(period) def self.update_views_count_for(period)
sql = "SELECT parent_id as topic_id, COUNT(*) AS count sql = "SELECT parent_id as topic_id, GREATEST(COUNT(*), 1) AS count
FROM views v FROM views v
WHERE v.viewed_at >= :from WHERE v.viewed_at >= :from
GROUP BY topic_id" GROUP BY topic_id"
@ -68,7 +68,11 @@ class TopTopic < ActiveRecord::Base
def self.compute_top_score_for(period) def self.compute_top_score_for(period)
# log(views) + (posts * likes) # log(views) + (posts * likes)
exec_sql("UPDATE top_topics SET #{period}_score = log(#{period}_views_count + 1) + (#{period}_posts_count * #{period}_likes_count)") exec_sql("UPDATE top_topics
SET #{period}_score = CASE #{period}_views_count
WHEN 0 THEN 0
ELSE log(#{period}_views_count) + (#{period}_posts_count * #{period}_likes_count)
END")
end end
def self.start_of(period) def self.start_of(period)