Commit c2364402 by David Ormsbee

Merge pull request #2 from rll/dave/tuning

Performance tuning
parents 9b188b87 c8204217
...@@ -70,15 +70,12 @@ get "#{APIPREFIX}/search/threads/recent_active" do ...@@ -70,15 +70,12 @@ get "#{APIPREFIX}/search/threads/recent_active" do
query_params["commentable_id"] = params["commentable_id"] if params["commentable_id"] query_params["commentable_id"] = params["commentable_id"] if params["commentable_id"]
comment_threads = if follower_id comment_threads = if follower_id
User.find(follower_id).subscribed_threads.select do |thread| User.find(follower_id).subscribed_threads
thread.last_activity_at >= from_time and \
query_params.to_a.map {|query| thread[query.first] == query.last}.all?
end
else else
CommentThread.all.where(query_params.merge(:last_activity_at => {:$gte => from_time})) CommentThread.all
end end
comment_threads.to_a.sort {|x, y| y.last_activity_at <=> x.last_activity_at}[0..4].map(&:to_hash).to_json comment_threads.where(query_params.merge(:last_activity_at => {:$gte => from_time})).order_by(:last_activity_at.desc).limit(5).to_a.map(&:to_hash).to_json
end end
...@@ -86,7 +83,7 @@ get "#{APIPREFIX}/search/tags/trending" do ...@@ -86,7 +83,7 @@ get "#{APIPREFIX}/search/tags/trending" do
query_params = {} query_params = {}
query_params["course_id"] = params["course_id"] if params["course_id"] query_params["course_id"] = params["course_id"] if params["course_id"]
query_params["commentable_id"] = params["commentable_id"] if params["commentable_id"] query_params["commentable_id"] = params["commentable_id"] if params["commentable_id"]
CommentThread.all.where(query_params).to_a CommentThread.where(query_params).only(:tags_array).to_a
.map(&:tags_array).flatten.group_by{|x| x} .map(&:tags_array).flatten.group_by{|x| x}
.map{|k, v| [k, v.count]} .map{|k, v| [k, v.count]}
.sort_by {|x| - x.last}[0..4] .sort_by {|x| - x.last}[0..4]
......
...@@ -15,6 +15,8 @@ class Comment < Content ...@@ -15,6 +15,8 @@ class Comment < Content
field :anonymous, type: Boolean, default: false field :anonymous, type: Boolean, default: false
field :at_position_list, type: Array, default: [] field :at_position_list, type: Array, default: []
index({author_id: 1, course_id: 1})
belongs_to :comment_thread, index: true belongs_to :comment_thread, index: true
belongs_to :author, class_name: "User", index: true belongs_to :author, class_name: "User", index: true
......
...@@ -21,6 +21,8 @@ class CommentThread < Content ...@@ -21,6 +21,8 @@ class CommentThread < Content
field :at_position_list, type: Array, default: [] field :at_position_list, type: Array, default: []
field :last_activity_at, type: Time field :last_activity_at, type: Time
index({author_id: 1, course_id: 1})
include Tire::Model::Search include Tire::Model::Search
include Tire::Model::Callbacks include Tire::Model::Callbacks
......
...@@ -7,6 +7,8 @@ class Subscription ...@@ -7,6 +7,8 @@ class Subscription
field :source_type, type: String field :source_type, type: String
index({subscriber_id: 1, source_id: 1, source_type: 1}) index({subscriber_id: 1, source_id: 1, source_type: 1})
index({subscriber_id: 1, source_type: 1})
index({subscriber_id: 1})
def to_hash def to_hash
as_document.slice(*%w[subscriber_id source_id source_type]) as_document.slice(*%w[subscriber_id source_id source_type])
......
...@@ -31,23 +31,23 @@ class User ...@@ -31,23 +31,23 @@ class User
end end
def subscribed_thread_ids def subscribed_thread_ids
subscriptions_as_subscriber.where(source_type: "CommentThread").map(&:source_id) Subscription.where(subscriber_id: id.to_s, source_type: "CommentThread").only(:source_id).map(&:source_id)
end end
def subscribed_commentable_ids def subscribed_commentable_ids
subscriptions_as_subscriber.where(source_type: "Commentable").map(&:source_id) subscriptions_as_subscriber.where(source_type: "Commentable").only(:source_id).map(&:source_id)
end end
def subscribed_user_ids def subscribed_user_ids
subscriptions_as_subscriber.where(source_type: "User").map(&:source_id) subscriptions_as_subscriber.where(source_type: "User").only(:source_id).map(&:source_id)
end end
def subscribed_threads def subscribed_threads
subscribed_thread_ids.map {|id| CommentThread.find(id)} CommentThread.where(:id.in => subscribed_thread_ids)
end end
def subscribed_commentables def subscribed_commentables
subscribed_commentable_ids.map {|id| Commentable.find(id)} Commentable.find(*subscribed_commentable_ids).only(:id).map(&:id)
end end
def subscribed_users def subscribed_users
...@@ -60,15 +60,16 @@ class User ...@@ -60,15 +60,16 @@ class User
hash = hash.merge("subscribed_thread_ids" => subscribed_thread_ids, hash = hash.merge("subscribed_thread_ids" => subscribed_thread_ids,
"subscribed_commentable_ids" => subscribed_commentable_ids, "subscribed_commentable_ids" => subscribed_commentable_ids,
"subscribed_user_ids" => subscribed_user_ids, "subscribed_user_ids" => subscribed_user_ids,
"follower_ids" => subscriptions_as_source.map(&:subscriber_id), "follower_ids" => subscriptions_as_source.only(:subscriber_id).map(&:subscriber_id),
"id" => id, "id" => id,
"upvoted_ids" => upvoted_ids, "upvoted_ids" => upvoted_ids,
"downvoted_ids" => downvoted_ids, "downvoted_ids" => downvoted_ids,
"default_sort_key" => default_sort_key) "default_sort_key" => default_sort_key
)
end end
if params[:course_id] if params[:course_id]
hash = hash.merge("threads_count" => comment_threads.where(course_id: params[:course_id]).count, hash = hash.merge("threads_count" => CommentThread.where(user_id: id, course_id: params[:course_id]).count,
"comments_count" => comments.where(course_id: params[:course_id]).count, "comments_count" => Comment.where(user_id: id, course_id: params[:course_id]).count
) )
end end
hash hash
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment