Commit 620dd38b by Chris Dodge Committed by Jonathan Piacenti

All for aggregate queries on the social stats API endpoint

parent 6d159a29
...@@ -85,51 +85,59 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id| ...@@ -85,51 +85,59 @@ get "#{APIPREFIX}/users/:user_id/social_stats" do |user_id|
# get all metadata regarding forum content, but don't bother to fetch the body # get all metadata regarding forum content, but don't bother to fetch the body
# as we don't need it and we shouldn't push all that data over the wire # as we don't need it and we shouldn't push all that data over the wire
content = Content.where(author_id: user_id, course_id: course_id).without(:body) if user_id == "*" then
content = Content.where(course_id: course_id).without(:body)
num_threads = 0 else
num_comments = 0 content = Content.where(author_id: user_id, course_id: course_id).without(:body)
num_replies = 0 end
num_upvotes = 0
num_downvotes = 0
num_flagged = 0
num_comments_generated = 0
thread_ids = [] user_stats = {}
thread_ids = {}
content.each do |item| content.each do |item|
user_id = item.author_id
if user_stats.key?(user_id) == false then
user_stats[user_id] = {
"num_threads" => 0,
"num_comments" => 0,
"num_replies" => 0,
"num_upvotes" => 0,
"num_downvotes" => 0,
"num_flagged" => 0,
"num_comments_generated" => 0
}
thread_ids[user_id] = []
end
if item._type == "CommentThread" then if item._type == "CommentThread" then
num_threads += 1 user_stats[user_id]["num_threads"] += 1
thread_ids.push(item._id) thread_ids[user_id].push(item._id)
num_comments_generated += item.comment_count user_stats[user_id]["num_comments_generated"] += item.comment_count
elsif item._type == "Comment" and item.parent_ids == [] then elsif item._type == "Comment" and item.parent_ids == [] then
num_comments += 1 user_stats[user_id]["num_comments"] += 1
else else
num_replies += 1 user_stats[user_id]["num_replies"] += 1
end end
# don't allow for self-voting # don't allow for self-voting
item.votes["up"].delete(user_id) item.votes["up"].delete(user_id)
item.votes["down"].delete(user_id) item.votes["down"].delete(user_id)
num_upvotes += item.votes["up"].count user_stats[user_id]["num_upvotes"] += item.votes["up"].count
num_downvotes += item.votes["down"].count user_stats[user_id]["num_downvotes"] += item.votes["down"].count
num_flagged += item.abuse_flaggers.count user_stats[user_id]["num_flagged"] += item.abuse_flaggers.count
end end
# with the array of objectId's for threads, get a count of number of other users who have a subscription on it # with the array of objectId's for threads, get a count of number of other users who have a subscription on it
num_thread_followers = Subscription.where(:subscriber_id.ne => user_id, :source_id.in => thread_ids).count() user_stats.keys.each do |user_id|
user_stats[user_id]["num_thread_followers"] = Subscription.where(:subscriber_id.ne => user_id, :source_id.in => thread_ids[user_id]).count()
{ end
num_threads: num_threads,
num_comments: num_comments, user_stats.to_json
num_replies: num_replies,
num_upvotes: num_upvotes,
num_downvotes: num_downvotes,
num_flagged: num_flagged,
num_thread_followers: num_thread_followers,
num_comments_generated: num_comments_generated
}.to_json
end end
end end
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