Optimize stories (#1988)

* Add db index to articles path

* Slight stories optimizations

* Select only needed fields for the flare tag

* Revert the last commit (to check builds)

* Return selecting only needed fields for tag
This commit is contained in:
Anna Buianova 2019-03-13 02:38:22 +03:00 committed by Ben Halpern
parent 4e120b8a8e
commit 343f109c0f
6 changed files with 13 additions and 12 deletions

View file

@ -139,7 +139,7 @@ module ApplicationHelper
def tag_colors(tag)
Rails.cache.fetch("view-helper-#{tag}/tag_colors", expires_in: 5.hours) do
if found_tag = Tag.find_by_name(tag)
if found_tag = Tag.select(%i[bg_color_hex text_color_hex]).find_by_name(tag)
{ background: found_tag.bg_color_hex, color: found_tag.text_color_hex }
else
{ background: "#d6d9e0", color: "#606570" }

View file

@ -19,7 +19,7 @@ class FlareTag
def tag
@tag ||= Rails.cache.fetch("article_flare_tag-#{article.id}-#{article.updated_at}", expires_in: 12.hours) do
flare = FLARES.detect { |f| article.cached_tag_list_array.include?(f) }
flare && flare != except_tag ? Tag.find_by_name(flare) : nil
flare && flare != except_tag ? Tag.select(%i[name bg_color_hex text_color_hex]).find_by_name(flare) : nil
end
end

View file

@ -3,7 +3,6 @@ class StickyArticleCollection
def initialize(article, author)
@article = article
@author = author
@article_tags = article_tags
@reaction_count_num = Rails.env.production? ? 15 : -1
@comment_count_num = Rails.env.production? ? 7 : -2
end
@ -18,16 +17,15 @@ class StickyArticleCollection
end
def suggested_stickies
(tag_articles + more_articles).sample(8)
(tag_articles.load + more_articles).sample(8)
end
def tag_articles
Article.tagged_with(article_tags, any: true).
@tag_articles ||= Article.tagged_with(article_tags, any: true).
includes(:user).
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)
@ -39,7 +37,6 @@ class StickyArticleCollection
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).
@ -48,8 +45,6 @@ class StickyArticleCollection
end
def article_tags
tags = article.cached_tag_list_array
tags.delete("discuss")
tags
@article_tags ||= article.cached_tag_list_array - ["discuss"]
end
end

View file

@ -86,7 +86,7 @@
<% @sticky_collection = StickyArticleCollection.new(@article, @organization || @user) %>
<% @sticky_articles = @sticky_collection.suggested_stickies %>
<% @user_stickies = @sticky_collection.user_stickies %>
<% if @user_stickies && @user_stickies.any? %>
<% if @user_stickies.any? %>
<% @user_stickies.each_with_index do |article, index| %>
<div class="primary-sticky-nav-element-wrapper">
<% if index == 0 %>

View file

@ -0,0 +1,5 @@
class AddIndexToArticlesPath < ActiveRecord::Migration[5.1]
def change
add_index :articles, :path
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190305221008) do
ActiveRecord::Schema.define(version: 20190306082543) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -128,6 +128,7 @@ ActiveRecord::Schema.define(version: 20190305221008) do
t.index ["boost_states"], name: "index_articles_on_boost_states", using: :gin
t.index ["featured_number"], name: "index_articles_on_featured_number"
t.index ["hotness_score"], name: "index_articles_on_hotness_score"
t.index ["path"], name: "index_articles_on_path"
t.index ["published_at"], name: "index_articles_on_published_at"
t.index ["slug"], name: "index_articles_on_slug"
t.index ["user_id"], name: "index_articles_on_user_id"