Skip to content

Commit

Permalink
Add skip_cache parameter to bypass similarity search cache in Feed API (
Browse files Browse the repository at this point in the history
#2095)

* Add skip_cache parameter to bypass similarity search cache in Feed API

- Introduced `skip_cache` parameter to Feed API, allowing requests to bypass the similarity search cache when set to `true`.
- Updated `search_for_similar_published_fact_checks` in `SmoochSearch` to support cached and non-cached search logic.
- Added `search_for_similar_published_fact_checks_no_cache` method to perform non-cached similarity searches.
- Modified `FeedResource` to handle and pass the `skip_cache` parameter.
- Implemented automated test for `skip_cache` functionality to ensure correct behavior.
- Updated logging for better traceability of cache usage in similarity searches.

* Add reviewer feedback

* Add reviewer feedback
  • Loading branch information
jayjay-w authored Oct 21, 2024
1 parent 425235f commit bc3941b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
8 changes: 6 additions & 2 deletions app/models/concerns/smooch_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,13 @@ def normalized_query_hash(type, query, team_ids, after, feed_id, language)

# "type" is text, video, audio or image
# "query" is either a piece of text of a media URL
def search_for_similar_published_fact_checks(type, query, team_ids, after = nil, feed_id = nil, language = nil)
Rails.cache.fetch("smooch:search_results:#{self.normalized_query_hash(type, query, team_ids, after, feed_id, language)}", expires_in: 2.hours) do
def search_for_similar_published_fact_checks(type, query, team_ids, after = nil, feed_id = nil, language = nil, skip_cache = false)
if skip_cache
self.search_for_similar_published_fact_checks_no_cache(type, query, team_ids, after, feed_id, language)
else
Rails.cache.fetch("smooch:search_results:#{self.normalized_query_hash(type, query, team_ids, after, feed_id, language)}", expires_in: 2.hours) do
self.search_for_similar_published_fact_checks_no_cache(type, query, team_ids, after, feed_id, language)
end
end
end

Expand Down
16 changes: 9 additions & 7 deletions app/resources/api/v2/feed_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class FeedResource < BaseResource
filter :feed_id, apply: ->(records, _value, _options) { records }
filter :webhook_url, apply: ->(records, _value, _options) { records }
filter :skip_save_request, apply: ->(records, _value, _options) { records }
filter :skip_cache, apply: ->(records, _value, _options) { records }

paginator :none

def self.records(options = {}, skip_save_request = false)
def self.records(options = {}, skip_save_request = false, skip_cache = false)
team_ids = self.workspaces(options).map(&:id)
Team.current ||= Team.find_by_id(team_ids[0].to_i)
filters = options[:filters] || {}
Expand All @@ -34,28 +35,29 @@ def self.records(options = {}, skip_save_request = false)
after = Time.parse(after) unless after.blank?
feed_id = filters.dig(:feed_id, 0).to_i
skip_save_request = skip_save_request || filters.dig(:skip_save_request, 0) == 'true'
skip_cache = skip_cache || filters.dig(:skip_cache, 0) == 'true'

return ProjectMedia.none if team_ids.blank? || query.blank?

if feed_id > 0
return get_results_from_feed_teams(team_ids, feed_id, query, type, after, webhook_url, skip_save_request)
return get_results_from_feed_teams(team_ids, feed_id, query, type, after, webhook_url, skip_save_request, skip_cache)
elsif ApiKey.current
return get_results_from_api_key_teams(type, query, after)
return get_results_from_api_key_teams(type, query, after, skip_cache)
end
end

def self.get_results_from_api_key_teams(type, query, after)
def self.get_results_from_api_key_teams(type, query, after, skip_cache)
RequestStore.store[:pause_database_connection] = true # Release database connection during Bot::Alegre.request_api
team_ids = ApiKey.current.bot_user.team_ids
Bot::Smooch.search_for_similar_published_fact_checks(type, query, team_ids, after)
Bot::Smooch.search_for_similar_published_fact_checks(type, query, team_ids, after, skip_cache)
end

def self.get_results_from_feed_teams(team_ids, feed_id, query, type, after, webhook_url, skip_save_request)
def self.get_results_from_feed_teams(team_ids, feed_id, query, type, after, webhook_url, skip_save_request, skip_cache)
return ProjectMedia.none unless can_read_feed?(feed_id, team_ids)
feed = Feed.find(feed_id)
RequestStore.store[:pause_database_connection] = true # Release database connection during Bot::Alegre.request_api
RequestStore.store[:smooch_bot_settings] = feed.get_smooch_bot_settings.to_h
results = Bot::Smooch.search_for_similar_published_fact_checks(type, query, feed.team_ids, after, feed_id)
results = Bot::Smooch.search_for_similar_published_fact_checks(type, query, feed.team_ids, after, feed_id, skip_cache)
Feed.delay({ retry: 0, queue: 'feed' }).save_request(feed_id, type, query, webhook_url, results.to_a.map(&:id)) unless skip_save_request
results
end
Expand Down
20 changes: 16 additions & 4 deletions test/controllers/feeds_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setup
@f = create_feed published: true
@f.teams = [@t1, @t2]
FeedTeam.update_all(shared: true)
Bot::Smooch.stubs(:search_for_similar_published_fact_checks).with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id).returns([@pm1, @pm2])
Bot::Smooch.stubs(:search_for_similar_published_fact_checks).with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id, false).returns([@pm1, @pm2])
end

def teardown
Expand All @@ -44,7 +44,7 @@ def teardown
b.api_key = a
b.save!
create_team_user team: @t1, user: b
Bot::Smooch.stubs(:search_for_similar_published_fact_checks).with('text', 'Foo', [@t1.id], nil).returns([@pm1])
Bot::Smooch.stubs(:search_for_similar_published_fact_checks).with('text', 'Foo', [@t1.id], nil, false).returns([@pm1])

authenticate_with_token a
get :index, params: { filter: { type: 'text', query: 'Foo' } }
Expand All @@ -54,6 +54,9 @@ def teardown
end

test "should request feed data" do
Bot::Smooch.stubs(:search_for_similar_published_fact_checks)
.with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id, false)
.returns([@pm1, @pm2])
authenticate_with_token @a
get :index, params: { filter: { type: 'text', query: 'Foo', feed_id: @f.id } }
assert_response :success
Expand All @@ -65,13 +68,13 @@ def teardown
Sidekiq::Testing.fake! do
authenticate_with_token @a

Bot::Smooch.stubs(:search_for_similar_published_fact_checks).with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id).returns([@pm1, @pm2])
Bot::Smooch.stubs(:search_for_similar_published_fact_checks).with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id, false).returns([@pm1, @pm2])
get :index, params: { filter: { type: 'text', query: 'Foo', feed_id: @f.id } }
assert_response :success
assert_equal 'Foo', json_response['data'][0]['attributes']['organization']
assert_equal 'Bar', json_response['data'][1]['attributes']['organization']

Bot::Smooch.stubs(:search_for_similar_published_fact_checks).with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id).returns([@pm2, @pm1])
Bot::Smooch.stubs(:search_for_similar_published_fact_checks).with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id, false).returns([@pm2, @pm1])
get :index, params: { filter: { type: 'text', query: 'Foo', feed_id: @f.id } }
assert_response :success
assert_equal 'Bar', json_response['data'][0]['attributes']['organization']
Expand Down Expand Up @@ -107,6 +110,9 @@ def teardown

test "should save request query" do
Bot::Alegre.stubs(:request).returns({})
Bot::Smooch.stubs(:search_for_similar_published_fact_checks)
.with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id, false)
.returns([@pm1, @pm2])
Sidekiq::Testing.inline!
authenticate_with_token @a
assert_difference 'Request.count' do
Expand All @@ -122,6 +128,9 @@ def teardown

test "should save relationship between request and results" do
Bot::Alegre.stubs(:request).returns({})
Bot::Smooch.stubs(:search_for_similar_published_fact_checks)
.with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id, false)
.returns([@pm1, @pm2])
Sidekiq::Testing.inline!
authenticate_with_token @a
assert_difference 'Request.count' do
Expand All @@ -133,6 +142,9 @@ def teardown

test "should not save request when skip_save_request is true" do
Bot::Alegre.stubs(:request).returns({})
Bot::Smooch.stubs(:search_for_similar_published_fact_checks)
.with('text', 'Foo', [@t1.id, @t2.id], nil, @f.id, false)
.returns([@pm1, @pm2])
Sidekiq::Testing.inline!
authenticate_with_token @a
assert_no_difference 'Request.count' do
Expand Down

0 comments on commit bc3941b

Please sign in to comment.