Skip to content

Commit

Permalink
CV2-4713 dont wait for results when checking a disabled modality for …
Browse files Browse the repository at this point in the history
…a given workspace (#1915)

* CV2-4713 dont wait for results when checking a disabled modality for a given workspace

* update test name

* fix test

* flip logic
  • Loading branch information
DGaffney authored Jun 7, 2024
1 parent 6080654 commit 8dd3c02
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
36 changes: 22 additions & 14 deletions app/models/concerns/alegre_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ def get_confirmed_items_async(project_media, field, threshold=nil)
end

def get_similar_items_v2(project_media, field, threshold=nil)
type = get_type(project_media)
if !should_get_similar_items_of_type?('master', project_media.team_id) || !should_get_similar_items_of_type?(type, project_media.team_id)
if similarity_disabled_for_project_media?(project_media)
{}
else
suggested_or_confirmed = get_suggested_items(project_media, field, threshold)
Expand All @@ -365,9 +364,13 @@ def get_similar_items_v2(project_media, field, threshold=nil)
end
end

def similarity_disabled_for_project_media?(project_media)
type = Bot::Alegre.get_type(project_media)
!should_get_similar_items_of_type?('master', project_media.team_id) || !should_get_similar_items_of_type?(type, project_media.team_id)
end

def get_similar_items_v2_async(project_media, field, threshold=nil)
type = get_type(project_media)
if !should_get_similar_items_of_type?('master', project_media.team_id) || !should_get_similar_items_of_type?(type, project_media.team_id)
if similarity_disabled_for_project_media?(project_media)
return false
else
get_suggested_items_async(project_media, field, threshold)
Expand Down Expand Up @@ -398,8 +401,7 @@ def get_cached_data(required_keys)
end

def get_similar_items_v2_callback(project_media, field)
type = get_type(project_media)
if !should_get_similar_items_of_type?('master', project_media.team_id) || !should_get_similar_items_of_type?(type, project_media.team_id)
if similarity_disabled_for_project_media?(project_media)
return {}
else
cached_data = get_cached_data(get_required_keys(project_media, field))
Expand Down Expand Up @@ -427,6 +429,19 @@ def is_cached_data_not_good(cached_data)
cached_data.values.collect{|x| x.nil?}.include?(true)
end

def wait_for_results(project_media, args)
return {} if similarity_disabled_for_project_media?(project_media)
cached_data = get_cached_data(get_required_keys(project_media, nil))
timeout = args[:timeout] || 60
start_time = Time.now
while start_time + timeout > Time.now && is_cached_data_not_good(cached_data) #more robust for any type of null response
sleep(1)
cached_data = get_cached_data(get_required_keys(project_media, nil))
end
CheckSentry.notify(AlegreTimeoutError.new('Timeout when waiting for async response from Alegre'), params: args.merge({ cached_data: cached_data }).merge({time: Time.now, start_time: start_time, timeout: timeout})) if start_time + timeout < Time.now
return cached_data
end

def get_items_with_similar_media_v2(args={})
media_url = args[:media_url]
project_media = args[:project_media]
Expand All @@ -442,14 +457,7 @@ def get_items_with_similar_media_v2(args={})
project_media.type = type
end
get_similar_items_v2_async(project_media, nil, threshold)
cached_data = get_cached_data(get_required_keys(project_media, nil))
timeout = args[:timeout] || 60
start_time = Time.now
while start_time + timeout > Time.now && is_cached_data_not_good(cached_data) #more robust for any type of null response
sleep(1)
cached_data = get_cached_data(get_required_keys(project_media, nil))
end
CheckSentry.notify(AlegreTimeoutError.new('Timeout when waiting for async response from Alegre'), params: args.merge({ cached_data: cached_data.merge(time: Time.now, start_time: start_time, timeout: timeout) })) if start_time + timeout < Time.now
wait_for_results(project_media, args)
response = get_similar_items_v2_callback(project_media, nil)
delete(project_media, nil) if project_media.is_a?(TemporaryProjectMedia)
return response
Expand Down
27 changes: 27 additions & 0 deletions test/models/bot/alegre_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,33 @@ def teardown
assert_equal({}, Bot::Alegre.get_similar_items_v2(pm, nil))
end

test "should return a similarity_disabled_for_project_media? of true for a disabled workspace" do
tbi = TeamBotInstallation.where(team: @team, user: @bot).last
tbi.set_image_similarity_enabled = false
tbi.save!
Bot::Alegre.stubs(:merge_suggested_and_confirmed).never
pm = create_project_media team: @team, media: create_uploaded_image
assert_equal(true, Bot::Alegre.similarity_disabled_for_project_media?(pm))
end

test "should return a similarity_disabled_for_project_media? of true for an enabled workspace" do
tbi = TeamBotInstallation.where(team: @team, user: @bot).last
tbi.set_image_similarity_enabled = true
tbi.save!
Bot::Alegre.stubs(:merge_suggested_and_confirmed).never
pm = create_project_media team: @team, media: create_uploaded_image
assert_equal(false, Bot::Alegre.similarity_disabled_for_project_media?(pm))
end

test "should not wait for a response when disabled" do
tbi = TeamBotInstallation.where(team: @team, user: @bot).last
tbi.set_image_similarity_enabled = false
tbi.save!
Bot::Alegre.stubs(:merge_suggested_and_confirmed).never
pm = create_project_media team: @team, media: create_uploaded_image
assert_equal({}, Bot::Alegre.wait_for_results(pm, {}))
end

test "should not relate project media for video if disabled on workspace" do
tbi = TeamBotInstallation.where(team: @team, user: @bot).last
tbi.set_video_similarity_enabled = false
Expand Down

0 comments on commit 8dd3c02

Please sign in to comment.