Commit 23d1a1f9 by Joel Barciauskas Committed by GitHub

Merge pull request #242 from edx/jbarciauskas/remove-newrelic-dep

Wrap newrelic calls in a rescue block
parents 1acbf22f 346c65ee
......@@ -20,42 +20,40 @@ def get_thread_ids(context, group_ids, local_params, search_text)
)
end
self.class.trace_execution_scoped(['Custom/get_search_threads/es_search']) do
body = {
size: CommentService.config['max_deep_search_comment_count'].to_i,
sort: [
{updated_at: :desc}
],
query: {
multi_match: {
query: search_text,
fields: [:title, :body],
operator: :AND
},
filtered: {
filter: {
and: filters
}
}
}
}
body = {
size: CommentService.config['max_deep_search_comment_count'].to_i,
sort: [
{updated_at: :desc}
],
query: {
multi_match: {
query: search_text,
fields: [:title, :body],
operator: :AND
},
filtered: {
filter: {
and: filters
}
}
}
}
response = Elasticsearch::Model.client.search(index: Content::ES_INDEX_NAME, body: body)
response = Elasticsearch::Model.client.search(index: Content::ES_INDEX_NAME, body: body)
thread_ids = Set.new
response['hits']['hits'].each do |hit|
case hit['_type']
when CommentThread.document_type
thread_ids.add(hit['_id'])
when Comment.document_type
thread_ids.add(hit['_source']['comment_thread_id'])
else
# There shouldn't be any other document types. Nevertheless, ignore them, if they are present.
next
end
thread_ids = Set.new
response['hits']['hits'].each do |hit|
case hit['_type']
when CommentThread.document_type
thread_ids.add(hit['_id'])
when Comment.document_type
thread_ids.add(hit['_source']['comment_thread_id'])
else
# There shouldn't be any other document types. Nevertheless, ignore them, if they are present.
next
end
thread_ids
end
thread_ids
end
def get_suggested_text(search_text)
......
......@@ -54,13 +54,11 @@ get "#{APIPREFIX}/users/:user_id/active_threads" do |user_id|
collection = presenter.to_hash
json_output = nil
self.class.trace_execution_scoped(['Custom/get_user_active_threads/json_serialize']) do
json_output = {
collection: collection,
num_pages: num_pages,
page: page,
}.to_json
end
json_output = {
collection: collection,
num_pages: num_pages,
page: page,
}.to_json
json_output
end
......
require 'new_relic/agent/method_tracer'
helpers do
def commentable
......@@ -151,27 +150,23 @@ helpers do
end
if filter_flagged
self.class.trace_execution_scoped(['Custom/handle_threads_query/find_flagged']) do
# TODO replace with aggregate query?
comment_ids = Comment.where(:course_id => course_id).
where(:abuse_flaggers.ne => [], :abuse_flaggers.exists => true).
collect{|c| c.comment_thread_id}.uniq
# TODO replace with aggregate query?
comment_ids = Comment.where(:course_id => course_id).
where(:abuse_flaggers.ne => [], :abuse_flaggers.exists => true).
collect{|c| c.comment_thread_id}.uniq
thread_ids = comment_threads.where(:abuse_flaggers.ne => [], :abuse_flaggers.exists => true).
collect{|c| c.id}
thread_ids = comment_threads.where(:abuse_flaggers.ne => [], :abuse_flaggers.exists => true).
collect{|c| c.id}
comment_threads = comment_threads.in({"_id" => (comment_ids + thread_ids).uniq})
end
comment_threads = comment_threads.in({"_id" => (comment_ids + thread_ids).uniq})
end
if filter_unanswered
self.class.trace_execution_scoped(['Custom/handle_threads_query/find_unanswered']) do
endorsed_thread_ids = Comment.where(:course_id => course_id).
where(:parent_id.exists => false, :endorsed => true).
collect{|c| c.comment_thread_id}.uniq
endorsed_thread_ids = Comment.where(:course_id => course_id).
where(:parent_id.exists => false, :endorsed => true).
collect{|c| c.comment_thread_id}.uniq
comment_threads = comment_threads.where({"thread_type" => :question}).nin({"_id" => endorsed_thread_ids})
end
comment_threads = comment_threads.where({"thread_type" => :question}).nin({"_id" => endorsed_thread_ids})
end
sort_criteria = get_sort_criteria(sort_key)
......@@ -375,11 +370,16 @@ helpers do
end
end
include ::NewRelic::Agent::MethodTracer
add_method_tracer :user
add_method_tracer :thread
add_method_tracer :comment
add_method_tracer :flag_as_abuse
add_method_tracer :un_flag_as_abuse
add_method_tracer :handle_threads_query
begin
require 'new_relic/agent/method_tracer'
include ::NewRelic::Agent::MethodTracer
add_method_tracer :user
add_method_tracer :thread
add_method_tracer :comment
add_method_tracer :flag_as_abuse
add_method_tracer :un_flag_as_abuse
add_method_tracer :handle_threads_query
rescue LoadError
logger.warn "NewRelic agent library not installed"
end
end
require 'new_relic/agent/method_tracer'
require_relative 'concerns/searchable'
require_relative 'content'
require_relative 'constants'
......@@ -166,6 +165,11 @@ class Comment < Content
end
end
include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash
begin
require 'new_relic/agent/method_tracer'
include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash
rescue LoadError
logger.warn "NewRelic agent library not installed"
end
end
require 'new_relic/agent/method_tracer'
require_relative 'concerns/searchable'
require_relative 'content'
require_relative 'constants'
......@@ -158,6 +157,11 @@ class CommentThread < Content
subscriptions.delete_all
end
include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash
begin
require 'new_relic/agent/method_tracer'
include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash
rescue LoadError
logger.warn "NewRelic agent library not installed"
end
end
require 'new_relic/agent/method_tracer'
require_relative 'constants'
class User
......@@ -51,61 +50,59 @@ class User
end
if params[:course_id]
self.class.trace_execution_scoped(['Custom/User.to_hash/count_comments_and_threads']) do
if not params[:group_ids].empty?
# Get threads in either the specified group(s) or posted to all groups (nil).
specified_groups_or_global = params[:group_ids] << nil
threads_count = CommentThread.course_context.where(
author_id: id,
course_id: params[:course_id],
group_id: {"$in" => specified_groups_or_global},
anonymous: false,
anonymous_to_peers: false
).count
# Note that the comments may have been responses to a thread not started by author_id.
# comment.standalone_context? gets the context from the parent comment_thread
# we need to eager load the comment_thread to prevent an N+1 when we iterate through the results
comment_thread_ids = Comment.includes(:comment_thread).where(
author_id: id,
course_id: params[:course_id],
anonymous: false,
anonymous_to_peers: false
).
reject{ |comment| comment.standalone_context? }.
collect{ |comment| comment.comment_thread_id }
# Filter to the unique thread ids visible to the specified group(s).
group_comment_thread_ids = CommentThread.where(
id: {"$in" => comment_thread_ids.uniq},
group_id: {"$in" => specified_groups_or_global},
).collect{|d| d.id}
# Now filter comment_thread_ids so it only includes things in group_comment_thread_ids
# (keeping duplicates so the count will be correct).
comments_count = comment_thread_ids.count{
|comment_thread_id| group_comment_thread_ids.include?(comment_thread_id)
}
else
threads_count = CommentThread.course_context.where(
author_id: id,
course_id: params[:course_id],
anonymous: false,
anonymous_to_peers: false
).count
# comment.standalone_context? gets the context from the parent comment_thread
# we need to eager load the comment_thread to prevent an N+1 when we iterate through the results
comments_count = Comment.includes(:comment_thread).where(
author_id: id,
course_id: params[:course_id],
anonymous: false,
anonymous_to_peers: false
).reject{ |comment| comment.standalone_context? }.count
end
hash = hash.merge!("threads_count" => threads_count, "comments_count" => comments_count)
if not params[:group_ids].empty?
# Get threads in either the specified group(s) or posted to all groups (nil).
specified_groups_or_global = params[:group_ids] << nil
threads_count = CommentThread.course_context.where(
author_id: id,
course_id: params[:course_id],
group_id: {"$in" => specified_groups_or_global},
anonymous: false,
anonymous_to_peers: false
).count
# Note that the comments may have been responses to a thread not started by author_id.
# comment.standalone_context? gets the context from the parent comment_thread
# we need to eager load the comment_thread to prevent an N+1 when we iterate through the results
comment_thread_ids = Comment.includes(:comment_thread).where(
author_id: id,
course_id: params[:course_id],
anonymous: false,
anonymous_to_peers: false
).
reject{ |comment| comment.standalone_context? }.
collect{ |comment| comment.comment_thread_id }
# Filter to the unique thread ids visible to the specified group(s).
group_comment_thread_ids = CommentThread.where(
id: {"$in" => comment_thread_ids.uniq},
group_id: {"$in" => specified_groups_or_global},
).collect{|d| d.id}
# Now filter comment_thread_ids so it only includes things in group_comment_thread_ids
# (keeping duplicates so the count will be correct).
comments_count = comment_thread_ids.count{
|comment_thread_id| group_comment_thread_ids.include?(comment_thread_id)
}
else
threads_count = CommentThread.course_context.where(
author_id: id,
course_id: params[:course_id],
anonymous: false,
anonymous_to_peers: false
).count
# comment.standalone_context? gets the context from the parent comment_thread
# we need to eager load the comment_thread to prevent an N+1 when we iterate through the results
comments_count = Comment.includes(:comment_thread).where(
author_id: id,
course_id: params[:course_id],
anonymous: false,
anonymous_to_peers: false
).reject{ |comment| comment.standalone_context? }.count
end
hash = hash.merge!("threads_count" => threads_count, "comments_count" => comments_count)
end
hash
end
......@@ -142,13 +139,18 @@ class User
read_state.save
end
include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash
add_method_tracer :subscribed_thread_ids
add_method_tracer :upvoted_ids
add_method_tracer :downvoted_ids
add_method_tracer :subscribe
add_method_tracer :mark_as_read
begin
require 'new_relic/agent/method_tracer'
include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash
add_method_tracer :subscribed_thread_ids
add_method_tracer :upvoted_ids
add_method_tracer :downvoted_ids
add_method_tracer :subscribe
add_method_tracer :mark_as_read
rescue LoadError
logger.warn "NewRelic agent library not installed"
end
end
......
require_relative 'thread_utils'
require 'new_relic/agent/method_tracer'
class ThreadPresenter
......@@ -120,8 +119,13 @@ class ThreadPresenter
top_level
end
include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash
add_method_tracer :merge_response_content
begin
require 'new_relic/agent/method_tracer'
include ::NewRelic::Agent::MethodTracer
add_method_tracer :to_hash
add_method_tracer :merge_response_content
rescue LoadError
logger.warn "NewRelic agent library not installed"
end
end
require 'new_relic/agent/method_tracer'
module ThreadUtils
def self.get_endorsed(threads)
......@@ -42,9 +40,14 @@ module ThreadUtils
end
class << self
include ::NewRelic::Agent::MethodTracer
add_method_tracer :get_read_states
add_method_tracer :get_endorsed
begin
require 'new_relic/agent/method_tracer'
include ::NewRelic::Agent::MethodTracer
add_method_tracer :get_read_states
add_method_tracer :get_endorsed
rescue LoadError
logger.warn "NewRelic agent library not installed"
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