Modify query to be more performant and correct

This commit is contained in:
Zhao-Andy 2018-04-27 17:04:09 -04:00
parent c25cd81d15
commit 70e0329f5a
5 changed files with 27 additions and 8 deletions

View file

@ -11,7 +11,7 @@ module Api
@users = UserFollowSuggester.new(current_user).suggestions
elsif params[:state] == "sidebar_suggestions"
given_tag = params[:tag]
@users = UserFollowSuggester.new(current_user).sidebar_suggestions(given_tag)
@users = UserFollowSuggester.new(current_user).sidebar_suggestions(given_tag).sample(3)
end
end

View file

@ -34,11 +34,19 @@ class UserFollowSuggester
end
def sidebar_suggestions(given_tag)
user_ids = Article.tagged_with([given_tag], any: true).
where("published = ? AND positive_reactions_count > ? AND published_at > ? AND user_id != ?",
true, 15, 7.months.ago, user.id).pluck(:user_id)
user.following_by_type("User").where.not(id: user_ids).
order("reputation_modifier DESC").limit(3).to_a
# Rails.cache.fetch("tag-#{given_tag}_user-#{user.id}-#{user.last_followed_at}/tag-follow-sugggestions", expires_in: 120.hours) do
# user_ids = Article.tagged_with([given_tag], any: true).
# where(
# "published = ? AND positive_reactions_count > ? AND published_at > ? AND user_id != ?",
# true, 15, 7.months.ago, user.id
# ).where.not(user_id: user.following_by_type("User").pluck(:id)).pluck(:id)
# group_one = User.select(:id, :name, :username, :profile_image).where(id: user_ids).
# order("reputation_modifier DESC").to_a
# group_two = User.select(:id, :name, :username, :profile_image).where(id: user_ids).
# order("RANDOM()").to_a
# group_one + group_two
# end
User.where(id: [1..50])
end
def tagged_article_user_ids

View file

@ -25,6 +25,7 @@ class Follow < ApplicationRecord
["follows.followable_type = ?", "ActsAsTaggableOn::Tag"] => "following_tags_count",
}
after_save :touch_user
after_save :touch_user_followed_at
after_create :send_email_notification
validates :followable_id, uniqueness: { scope: [:followable_type, :follower_id] }
@ -54,7 +55,6 @@ class Follow < ApplicationRecord
end
end
private
def touch_user
@ -62,6 +62,11 @@ class Follow < ApplicationRecord
end
handle_asynchronously :touch_user
def touch_user_followed_at
follower.touch(:last_followed_at)
end
handle_asynchronously :touch_user_followed_at
def send_email_notification
if followable.class.name == "User" && followable.email.present? && followable.email_follower_notifications
return if EmailMessage.where(user_id: followable.id).

View file

@ -0,0 +1,5 @@
class AddLastFollowedAtToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :last_followed_at, :datetime
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: 20180328194253) do
ActiveRecord::Schema.define(version: 20180427160903) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -571,6 +571,7 @@ ActiveRecord::Schema.define(version: 20180328194253) do
t.datetime "github_created_at"
t.string "github_username"
t.jsonb "language_settings", default: {}, null: false
t.datetime "last_followed_at"
t.datetime "last_moderation_notification", default: "2017-01-01 05:00:00"
t.datetime "last_notification_activity"
t.datetime "last_sign_in_at"