Reduce queries in typical stories#show response + small fixes (#903)

* Reduce queries in typical stories#show response + small fixes

* Change const to git in non-es6 code
This commit is contained in:
Ben Halpern 2018-10-13 19:38:38 -04:00 committed by GitHub
parent 588ffeef09
commit 221c363af0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 71 additions and 63 deletions

View file

@ -1,4 +1,4 @@
const ENTER_KEY_CODE = 13;
function initializeCommentsPage() {
if (document.getElementById('comments-container')) {
@ -274,6 +274,7 @@ function handleKeyUp(event) {
function handleKeyDown(event) {
// If Ctrl+Enter OR Command+Enter is pressed
var ENTER_KEY_CODE = 13;
if((event.ctrlKey || event.metaKey) && event.keyCode === ENTER_KEY_CODE) {
// Get user details and extract code of conduct & comment count
var user = userData();

View file

@ -40,7 +40,6 @@ class AsyncInfoController < ApplicationController
followed_user_ids: @user.cached_following_users_ids,
reading_list_ids: ReadingList.new(@user).cached_ids_of_articles,
saw_onboarding: @user.saw_onboarding,
onboarding_checklist: UserStates.new(@user).cached_onboarding_checklist,
checked_code_of_conduct: @user.checked_code_of_conduct,
number_of_comments: @user.comments.count,
display_sponsors: @user.display_sponsors,

View file

@ -20,8 +20,7 @@ class StoriesController < ApplicationController
def show
@story_show = true
@podcast = Podcast.find_by_slug(params[:username])
@episode = PodcastEpisode.find_by_slug(params[:slug])
if @podcast && @episode
if @podcast && @episode = PodcastEpisode.find_by_slug(params[:slug])
handle_podcast_show
else
handle_article_show
@ -210,7 +209,6 @@ class StoriesController < ApplicationController
@article_show = true
@comment = Comment.new
assign_article_and_user_and_organization
assign_sticky_nav
handle_possible_redirect
return if performed?
not_found unless @article
@ -305,41 +303,4 @@ class StoriesController < ApplicationController
per(num_articles).
filter_excluded_tags(params[:tag])
end
def assign_sticky_nav
return unless @article
reaction_count_num = Rails.env.production? ? 15 : -1
comment_count_num = Rails.env.production? ? 7 : -2
more_articles = []
article_tags = @article.cached_tag_list_array
article_tags.delete("discuss")
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)
if tag_articles.size < 6
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).
order("RANDOM()").
limit(10 - tag_articles.size)
end
@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)
@sticky_articles = (tag_articles + more_articles).sample(8)
end
end

View file

@ -2,7 +2,7 @@ class UserDecorator < ApplicationDecorator
delegate_all
def cached_followed_tags
Rails.cache.fetch("user-#{id}-#{updated_at}/followed_tags", expires_in: 100.hours) do
Rails.cache.fetch("user-#{id}-#{updated_at}/followed_tags", expires_in: 20.hours) do
Tag.where(id: Follow.where(follower_id: id, followable_type: "ActsAsTaggableOn::Tag").pluck(:followable_id)).order("hotness_score DESC")
end
end

View file

@ -84,14 +84,6 @@ describe('Chat utilities', () => {
],
reading_list_ids: [48, 49, 34, 51, 64, 56],
saw_onboarding: true,
onboarding_checklist: {
write_your_first_article: false,
follow_your_first_tag: false,
fill_out_your_profile: false,
leave_your_first_reaction: true,
follow_your_first_dev: true,
leave_your_first_comment: false,
},
checked_code_of_conduct: false,
number_of_comments: 0,
display_sponsors: true,

View file

@ -8,7 +8,7 @@ class FollowChecker
end
def cached_follow_check
Rails.cache.fetch("user-#{follower.id}-#{follower.updated_at}/is_following_#{followable_type}_#{followable_id}", expires_in: 100.hours) do
Rails.cache.fetch("user-#{follower.id}-#{follower.updated_at}/is_following_#{followable_type}_#{followable_id}", expires_in: 20.hours) do
followable = if followable_type == "Tag"
Tag.find(followable_id)
elsif followable_type == "Organization"

View file

@ -0,0 +1,56 @@
class StickyArticleCollection
attr_accessor :article, :author, :tag_articles, :more_articles, :reaction_count_num, :comment_count_num
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
@tag_articles = tag_articles
@more_articles = more_articles
end
def user_stickies
author.articles.
where(published: true).
limited_column_select.
tagged_with(article_tags, any: true).
where.not(id: article.id).order("published_at DESC").
limit(2)
end
def suggested_stickies
(tag_articles + more_articles).sample(8)
end
def 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)
end
def more_articles
return [] if tag_articles.size < 6
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).
order("RANDOM()").
limit(10 - tag_articles.size)
end
def article_tags
tags = article.cached_tag_list_array
tags.delete("discuss")
tags
end
end

View file

@ -228,7 +228,7 @@ class User < ApplicationRecord
end
def cached_preferred_langs
Rails.cache.fetch("user-#{id}-#{updated_at}/cached_preferred_langs", expires_in: 80.hours) do
Rails.cache.fetch("user-#{id}-#{updated_at}/cached_preferred_langs", expires_in: 24.hours) do
langs = []
langs << "en" if prefer_language_en
langs << "ja" if prefer_language_ja
@ -251,7 +251,7 @@ class User < ApplicationRecord
def cached_followed_tag_names
cache_name = "user-#{id}-#{updated_at}/followed_tag_names"
Rails.cache.fetch(cache_name, expires_in: 100.hours) do
Rails.cache.fetch(cache_name, expires_in: 24.hours) do
Tag.where(
id: Follow.where(
follower_id: id,

View file

@ -1,3 +1,6 @@
<% @sticky_collection = StickyArticleCollection.new(@article, @organization || @user) %>
<% @sticky_articles = @sticky_collection.suggested_stickies %>
<% @user_stickies = @sticky_collection.user_stickies %>
<% @actor = @article.organization || @article.user %>
<style>
@ -76,11 +79,11 @@
</a>
<% end %>
<% end %>
<% if sticky_articles %>
<% if @sticky_articles %>
<div class="primary-sticky-nav-title">
<img src="<%= asset_path "emoji/apple-fire.png"%>"/> Trending on <a href="https://dev.to">dev.to</a>
</div>
<% sticky_articles.each do |article| %>
<% @sticky_articles.each do |article| %>
<a class="primary-sticky-nav-element" href="<%= article.path %>">
<img src="<%= ProfileImage.new(article.user).get(90) %>" class="primary-sticky-nav-profile-image" />
<%= article.title %>
@ -91,11 +94,5 @@
</div>
</a>
<% end %>
<% if 1 == 2 %>
<a class="primary-sticky-nav-element sticky-join-cta" href="/enter">
<img src="<%= asset_path "emoji/apple-fire.png"%>" /> Join the DEV Community
</a>
<% end %>
<% end %>
</div>

View file

@ -216,7 +216,9 @@
</div>
</div>
<%= render "articles/sticky_nav", sticky_articles: @sticky_articles %>
<% cache("sticky-sidebar-#{@article.id}-#{@article.edited_at}", :expires_in => 8.hours) do %>
<%= render "articles/sticky_nav" %>
<% end %>
<% if has_vid?(@article) %>
<%= render 'articles/fitvids' %>