mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
first stab at calculating the score of a topic for the top tab
This commit is contained in:
parent
90eb6e6b8f
commit
9c8e50351d
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
7
db/migrate/20131227164338_add_scores_to_top_topics.rb
Normal file
7
db/migrate/20131227164338_add_scores_to_top_topics.rb
Normal 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
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user