Skip to content

Commit

Permalink
Log and notify Sentry when relevant articles are not retrieved. (#2186)
Browse files Browse the repository at this point in the history
* Log and notify Sentry when relevant articles are not retrieved.

This PR implements two things:

* Case 1: Info-level logging when no relevant articles are returned.
* Case 2: Notify Sentry when there is an error retrieving relevant articles. In that case:
  * Catch the error and return an empty list, so it doesn't crash the frontend, which can then retrieve to recent articles.
  * Log a warning.
  * Notify Sentry. The Sentry message is static and implements a custom exception class, so similar notifications can be grouped together automatically, but includes all information we need in order to debug.

Reference: CV2-5932.
  • Loading branch information
caiosba authored Jan 21, 2025
1 parent c529963 commit 29488b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/models/team.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Team < ApplicationRecord
class RelevantArticlesError < StandardError; end

# These two callbacks must be in the top
after_create :create_team_partition
before_destroy :delete_created_bots, :remove_is_default_project_flag
Expand Down Expand Up @@ -563,10 +565,14 @@ def filter_by_keywords(query, filters, type = 'FactCheck')
query.where(Arel.sql("#{tsvector} @@ #{tsquery}"))
end

def similar_articles_search_limit(pm = nil)
pm.nil? ? CheckConfig.get('most_relevant_team_limit', 3, :integer) : CheckConfig.get('most_relevant_item_limit', 10, :integer)
end

def search_for_similar_articles(query, pm = nil)
# query: expected to be text
# pm: to request a most relevant to specific item and also include both FactCheck & Explainer
limit = pm.nil? ? CheckConfig.get('most_relevant_team_limit', 3, :integer) : CheckConfig.get('most_relevant_item_limit', 10, :integer)
limit = self.similar_articles_search_limit(pm)
threads = []
fc_items = []
ex_items = []
Expand All @@ -593,7 +599,12 @@ def search_for_similar_articles(query, pm = nil)
items = fc_items
# Get Explainers if no fact-check returned or get similar_articles for a ProjectMedia
items += ex_items if items.blank? || !pm.nil?
Rails.logger.info("Relevant articles found for team slug #{self.slug}, project media with ID #{pm&.id} and query #{query}: #{items.map(&:graphql_id)}")
items
rescue StandardError => e
Rails.logger.warn("Error when trying to retrieve relevant articles for team slug #{self.slug}, project media with ID #{pm&.id} and query #{query}.")
CheckSentry.notify(RelevantArticlesError.new('Error when trying to retrieve relevant articles'), team_slug: self.slug, project_media_id: pm&.id, query: query, exception_message: e.message, exception: e)
[]
end

def get_shorten_outgoing_urls
Expand Down
7 changes: 7 additions & 0 deletions test/models/team_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1583,4 +1583,11 @@ def setup
end
Bot::Smooch.unstub(:search_for_explainers)
end

test "should notify Sentry if it fails to retrieve relevant articles" do
Bot::Smooch.stubs(:search_for_similar_published_fact_checks_no_cache).raises(StandardError)
CheckSentry.expects(:notify).once
t = create_team
assert_equal [], t.search_for_similar_articles('Test')
end
end

0 comments on commit 29488b5

Please sign in to comment.