first stab at calculating the score of a topic for the top tab

This commit is contained in:
Régis Hanol 2013-12-27 18:10:35 +01:00
parent 90eb6e6b8f
commit 9c8e50351d
4 changed files with 30 additions and 10 deletions

View File

@ -73,8 +73,7 @@ class ListController < ApplicationController
end
def top
sort_order = params[:sort_order] || "posts"
top = generate_top_lists_by(sort_order)
top = generate_top_lists
respond_to do |format|
format.html do
@ -179,7 +178,7 @@ class ListController < ApplicationController
public_send(method, opts.merge(next_page_params(opts)))
end
def generate_top_lists_by(sort_order)
def generate_top_lists
top = {}
topic_ids = Set.new
@ -188,7 +187,7 @@ class ListController < ApplicationController
per_page: SiteSetting.topics_per_period_in_summary,
except_topic_ids: topic_ids.to_a
}
list = TopicQuery.new(current_user, options).list_top(sort_order, period)
list = TopicQuery.new(current_user, options).list_top_for(period)
topic_ids.merge(list.topic_ids)
top[period] = list
end

View File

@ -20,12 +20,17 @@ class TopTopic < ActiveRecord::Base
FROM topics
WHERE deleted_at IS NULL
AND visible
AND NOT archived")
# update all the counter caches
AND archetype <> :private_message
AND NOT archived",
private_message: Archetype::private_message)
TopTopic.periods.each do |period|
# update all the counter caches
TopTopic.sort_orders.each do |sort|
TopTopic.send("update_#{sort}_count_for", period)
end
# compute top score
TopTopic.compute_top_score_for(period)
end
end
end
@ -61,6 +66,16 @@ class TopTopic < ActiveRecord::Base
TopTopic.update_top_topics(period, "likes", sql)
end
def self.compute_top_score_for(period)
exec_sql("UPDATE top_topics
SET #{period}_score = log(
1 +
(#{period}_posts_count) *
(#{period}_likes_count / 2.0 + 1.0) *
(#{period}_views_count / 100.0 + 1.0)
)")
end
def self.start_of(period)
case period
when :yearly then 1.year.ago

View File

@ -0,0 +1,7 @@
class AddScoresToTopTopics < ActiveRecord::Migration
def change
TopTopic.periods.each do |period|
add_column :top_topics, "#{period}_score".to_sym, :float
end
end
end

View File

@ -84,12 +84,11 @@ class TopicQuery
create_list(:posted) {|l| l.where('tu.user_id IS NOT NULL') }
end
def list_top(sort_order, period)
count = "#{period}_#{sort_order}_count"
def list_top_for(period)
score = "#{period}_score"
create_list(:top, unordered: true) do |topics|
topics.joins(:top_topic)
.where("top_topics.#{count} > 0")
.order("top_topics.#{count} DESC, topics.bumped_at DESC")
.order("top_topics.#{score} DESC, topics.bumped_at DESC")
end
end