From 8dd3c02e0a6792f2b995507ca539e56a36eda526 Mon Sep 17 00:00:00 2001 From: Devin Gaffney Date: Fri, 7 Jun 2024 16:51:01 -0700 Subject: [PATCH] CV2-4713 dont wait for results when checking a disabled modality for 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 --- app/models/concerns/alegre_v2.rb | 36 +++++++++++++++++++------------ test/models/bot/alegre_v2_test.rb | 27 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/app/models/concerns/alegre_v2.rb b/app/models/concerns/alegre_v2.rb index 104f88d4c9..6ea4fab5de 100644 --- a/app/models/concerns/alegre_v2.rb +++ b/app/models/concerns/alegre_v2.rb @@ -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) @@ -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) @@ -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)) @@ -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] @@ -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 diff --git a/test/models/bot/alegre_v2_test.rb b/test/models/bot/alegre_v2_test.rb index 174bf7aac2..2ed345990c 100644 --- a/test/models/bot/alegre_v2_test.rb +++ b/test/models/bot/alegre_v2_test.rb @@ -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