From e4bb8d78234f9aae3768389eb4b40041153de4be Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Thu, 19 Jul 2018 11:55:59 -0400 Subject: [PATCH] Modify timeout and make some queries more efficient (#593) * Modify timeout and make some queries more efficient * Only query podcasts if user signed in * Remove unnecessary columns selected in boosted query etc. --- app/controllers/stories_controller.rb | 17 +++++++++++++---- app/models/article.rb | 5 +++-- app/models/comment.rb | 2 ++ app/services/suggester/articles/classic.rb | 4 +++- app/services/suggester/articles/high_quality.rb | 13 +++++++------ config/initializers/timeout.rb | 2 +- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index c5696851d..7484d6907 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -114,10 +114,7 @@ class StoriesController < ApplicationController @featured_story = @stories.where.not(main_image: nil).first&.decorate || Article.new end @stories = @stories.decorate - @podcast_episodes = PodcastEpisode. - includes(:podcast). - order("published_at desc"). - select(:slug, :title, :podcast_id).limit(5) + assign_podcasts @article_index = true @sidebar_ad = DisplayAd.where(approved: true, published: true, placement_area: "sidebar").first set_surrogate_key_header "articles", @stories.map(&:record_key) @@ -265,6 +262,15 @@ class StoriesController < ApplicationController end end + def assign_podcasts + if user_signed_in? + @podcast_episodes = PodcastEpisode. + includes(:podcast). + order("published_at desc"). + select(:slug, :title, :podcast_id).limit(5) + end + end + def article_finder(num_articles) Article.where(published: true). includes(:user). @@ -287,6 +293,7 @@ class StoriesController < ApplicationController where("positive_reactions_count > ? OR comments_count > ?", reaction_count_num, comment_count_num). where(published: true). where.not(id: @article.id, user_id: @article.user_id). + limited_column_select. where("featured_number > ?", 5.days.ago.to_i). order("RANDOM()"). limit(8) @@ -294,6 +301,7 @@ class StoriesController < ApplicationController more_articles = Article.tagged_with((["career","productivity","discuss","explainlikeimfive"]), any: true). includes(:user). where("comments_count > ?", comment_count_num). + limited_column_select. where(published: true). where.not(id: @article.id, user_id: @article.user_id). where("featured_number > ?", 5.days.ago.to_i). @@ -303,6 +311,7 @@ class StoriesController < ApplicationController @user_stickies = (@organization || @user).articles. where(published: true). + limited_column_select. tagged_with(article_tags, any: true). where.not(id: @article.id).order("published_at DESC"). limit(2) diff --git a/app/models/article.rb b/app/models/article.rb index 21d7b0b02..e7e076f3d 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -66,9 +66,10 @@ class Article < ApplicationRecord scope :limited_column_select, -> { select(:path, :title, :id, :comments_count, :positive_reactions_count, :cached_tag_list, - :main_image, :main_image_background_hex_color, :updated_at, + :main_image, :main_image_background_hex_color, :updated_at, :slug, :video, :user_id, :organization_id, :video_source_url, :video_code, - :video_thumbnail_url, :video_closed_caption_track_url, :published_at, :crossposted_at) + :video_thumbnail_url, :video_closed_caption_track_url, + :published_at, :crossposted_at, :boost_states, :description) } scope :limited_columns_internal_select, -> { diff --git a/app/models/comment.rb b/app/models/comment.rb index 3894f3a27..8c9b08dbd 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -113,6 +113,8 @@ class Comment < ApplicationRecord def self.rooted_on(commentable_id, commentable_type) includes(:user, :commentable). + select(:id, :user_id, :commentable_type, :commentable_id, + :deleted, :created_at, :processed_html, :ancestry). where(commentable_id: commentable_id, ancestry: nil, commentable_type: commentable_type) diff --git a/app/services/suggester/articles/classic.rb b/app/services/suggester/articles/classic.rb index fe3dcf48e..77b6c9b80 100644 --- a/app/services/suggester/articles/classic.rb +++ b/app/services/suggester/articles/classic.rb @@ -20,9 +20,11 @@ module Suggester def qualifying_articles(tag_names) tag_name = tag_names.sample - Rails.cache.fetch("classic-article-for-tag-#{tag_name}}", expires_in: 45.minutes) do + Rails.cache. + fetch("classic-article-for-tag-#{tag_name}}", expires_in: 90.minutes) do Article.tagged_with(tag_name). includes(:user). + limited_column_select. where(published: true, featured: true). where("positive_reactions_count > ?", MIN_REACTION_COUNT). where("published_at > ?", 10.months.ago). diff --git a/app/services/suggester/articles/high_quality.rb b/app/services/suggester/articles/high_quality.rb index ac2a4f9af..1482cc4be 100644 --- a/app/services/suggester/articles/high_quality.rb +++ b/app/services/suggester/articles/high_quality.rb @@ -8,12 +8,13 @@ module Suggester end def suggest - Article.where(published: true, featured: true). - includes(:user). - where("positive_reactions_count > ?", MIN_HQ_REACTION_COUNT). - order("RANDOM()"). - where.not(id: @not_ids). - first + Article.where(published: true, featured: true). + includes(:user). + where("positive_reactions_count > ?", MIN_HQ_REACTION_COUNT). + order("RANDOM()"). + limited_column_select. + where.not(id: @not_ids). + first end end end diff --git a/config/initializers/timeout.rb b/config/initializers/timeout.rb index 531e72b16..38c348050 100644 --- a/config/initializers/timeout.rb +++ b/config/initializers/timeout.rb @@ -1,4 +1,4 @@ -Rails.application.config.middleware.insert_before Rack::Runtime, Rack::Timeout, wait_timeout: 5, service_timeout: ENV["SERVICE_TIMEOUT"].to_i # seconds +# Rails.application.config.middleware.insert_before Rack::Runtime, Rack::Timeout, wait_timeout: 5, service_timeout: ENV["SERVICE_TIMEOUT"].to_i # seconds Rack::Timeout.unregister_state_change_observer(:logger) if Rails.env.development? Rack::Timeout::Logger.disable