Merge pull request #4566 from tgxworld/fix_perf_redirect_to_top

Fix perf redirect to top
This commit is contained in:
Guo Xiang Tan 2016-11-25 03:39:56 +01:00 committed by GitHub
commit 63a88ee6e7
10 changed files with 54 additions and 30 deletions

View File

@ -327,13 +327,19 @@ class ListController < ApplicationController
exclude_category_ids.pluck(:id)
end
def self.best_period_for(previous_visit_at, category_id=nil)
def self.best_period_with_topics_for(previous_visit_at, category_id=nil)
best_periods_for(previous_visit_at).each do |period|
top_topics = TopTopic.where("#{period}_score > 0")
top_topics = top_topics.joins(:topic).where("topics.category_id = ?", category_id) if category_id
return period if top_topics.count >= SiteSetting.topics_per_period_in_top_page
top_topics = top_topics.limit(SiteSetting.topics_per_period_in_top_page)
return period if top_topics.count == SiteSetting.topics_per_period_in_top_page
end
# default period is yearly
false
end
def self.best_period_for(previous_visit_at, category_id=nil)
best_period_with_topics_for(previous_visit_at, category_id) ||
SiteSetting.top_page_default_timeframe.to_sym
end

View File

@ -547,7 +547,9 @@ end
#
# Indexes
#
# index_categories_on_background_url (background_url)
# index_categories_on_email_in (email_in) UNIQUE
# index_categories_on_logo_url (logo_url)
# index_categories_on_topic_count (topic_count)
# unique_index_categories_on_name (name) UNIQUE
#

View File

@ -14,4 +14,6 @@ end
# Indexes
#
# idx_unique_post_uploads (post_id,upload_id) UNIQUE
# index_post_uploads_on_post_id (post_id)
# index_post_uploads_on_upload_id (upload_id)
#

View File

@ -99,8 +99,8 @@ class SiteSetting < ActiveRecord::Base
end
def self.min_redirected_to_top_period(duration)
period = ListController.best_period_for(duration)
return period if TopTopic.topics_per_period(period) >= SiteSetting.topics_per_period_in_top_page
period = ListController.best_period_with_topics_for(duration)
return period if period
# not enough topics
nil

View File

@ -4,12 +4,6 @@ class TopTopic < ActiveRecord::Base
belongs_to :topic
def self.topics_per_period(period)
DistributedMemoizer.memoize("#{Discourse.current_hostname}_topics_per_period_#{period}", 1.day) do
TopTopic.where("#{period}_score > 0").count
end.to_i
end
# The top topics we want to refresh often
def self.refresh_daily!
transaction do
@ -52,6 +46,8 @@ class TopTopic < ActiveRecord::Base
all: 6)
end
private
def self.sort_orders
@@sort_orders ||= [:posts, :views, :likes, :op_likes].freeze
end
@ -224,10 +220,6 @@ class TopTopic < ActiveRecord::Base
AND tt.#{period}_#{sort}_count <> c.count",
from: start_of(period))
end
private_class_method :sort_orders, :update_counts_and_compute_scores_for, :remove_invisible_topics,
:add_new_visible_topics, :update_posts_count_for, :update_views_count_for, :update_likes_count_for,
:compute_top_score_for, :start_of, :update_top_topics
end
# == Schema Information
@ -265,13 +257,16 @@ end
#
# Indexes
#
# index_top_topics_on_all_score (all_score)
# index_top_topics_on_daily_likes_count (daily_likes_count)
# index_top_topics_on_daily_op_likes_count (daily_op_likes_count)
# index_top_topics_on_daily_posts_count (daily_posts_count)
# index_top_topics_on_daily_score (daily_score)
# index_top_topics_on_daily_views_count (daily_views_count)
# index_top_topics_on_monthly_likes_count (monthly_likes_count)
# index_top_topics_on_monthly_op_likes_count (monthly_op_likes_count)
# index_top_topics_on_monthly_posts_count (monthly_posts_count)
# index_top_topics_on_monthly_score (monthly_score)
# index_top_topics_on_monthly_views_count (monthly_views_count)
# index_top_topics_on_quarterly_likes_count (quarterly_likes_count)
# index_top_topics_on_quarterly_op_likes_count (quarterly_op_likes_count)
@ -281,9 +276,11 @@ end
# index_top_topics_on_weekly_likes_count (weekly_likes_count)
# index_top_topics_on_weekly_op_likes_count (weekly_op_likes_count)
# index_top_topics_on_weekly_posts_count (weekly_posts_count)
# index_top_topics_on_weekly_score (weekly_score)
# index_top_topics_on_weekly_views_count (weekly_views_count)
# index_top_topics_on_yearly_likes_count (yearly_likes_count)
# index_top_topics_on_yearly_op_likes_count (yearly_op_likes_count)
# index_top_topics_on_yearly_posts_count (yearly_posts_count)
# index_top_topics_on_yearly_score (yearly_score)
# index_top_topics_on_yearly_views_count (yearly_views_count)
#

View File

@ -1101,6 +1101,7 @@ end
# index_users_on_auth_token (auth_token)
# index_users_on_last_posted_at (last_posted_at)
# index_users_on_last_seen_at (last_seen_at)
# index_users_on_uploaded_avatar_id (uploaded_avatar_id)
# index_users_on_username (username) UNIQUE
# index_users_on_username_lower (username_lower) UNIQUE
#

View File

@ -105,5 +105,7 @@ end
#
# Indexes
#
# index_user_avatars_on_custom_upload_id (custom_upload_id)
# index_user_avatars_on_gravatar_upload_id (gravatar_upload_id)
# index_user_avatars_on_user_id (user_id)
#

View File

@ -124,4 +124,6 @@ end
# Indexes
#
# index_user_profiles_on_bio_cooked_version (bio_cooked_version)
# index_user_profiles_on_card_background (card_background)
# index_user_profiles_on_profile_background (profile_background)
#

View File

@ -0,0 +1,9 @@
class AddScoresIndexesToTopTopics < ActiveRecord::Migration
def change
add_index :top_topics, :daily_score
add_index :top_topics, :weekly_score
add_index :top_topics, :monthly_score
add_index :top_topics, :yearly_score
add_index :top_topics, :all_score
end
end

View File

@ -76,7 +76,11 @@ describe SiteSetting do
before do
SiteSetting.topics_per_period_in_top_page = 2
SiteSetting.top_page_default_timeframe = 'daily'
TopTopic.stubs(:topics_per_period).with(:daily).returns(3)
2.times do
TopTopic.create!(daily_score: 2.5)
end
TopTopic.refresh!
end
@ -91,7 +95,6 @@ describe SiteSetting do
before do
SiteSetting.topics_per_period_in_top_page = 20
SiteSetting.top_page_default_timeframe = 'daily'
TopTopic.stubs(:topics_per_period).with(:daily).returns(1)
TopTopic.refresh!
end