From fa85b6b3b061b1b48a46e6b6d7892f1e27e24a55 Mon Sep 17 00:00:00 2001 From: Mohamed El-Sawy Date: Mon, 9 Sep 2024 11:23:47 +0300 Subject: [PATCH] CSV-5225: ignore nested documents that exceeds nested_objects_limit (#2025) * CSV-5225: ignore nested documents that exceeds nested_objects_limit * CV2-5225: add tests * CV2-5225: cleanup * CV2-5225: apply nested_objects_limit on tags and requests --- app/lib/check_elastic_search.rb | 1 + app/models/annotations/tag.rb | 7 +++++++ app/models/tipline_request.rb | 5 +++++ test/controllers/elastic_search_9_test.rb | 23 ++++++++++++++++++++++- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/lib/check_elastic_search.rb b/app/lib/check_elastic_search.rb index 00cb5681ab..35268006bb 100644 --- a/app/lib/check_elastic_search.rb +++ b/app/lib/check_elastic_search.rb @@ -82,6 +82,7 @@ def get_fresh_value(data) def add_update_nested_obj(options) return if self.disable_es_callbacks || RequestStore.store[:disable_es_callbacks] + return if options[:op] == 'create' && self.respond_to?(:hit_nested_objects_limit?) && self.hit_nested_objects_limit? model = { klass: self.class.name, id: self.id } ElasticSearchWorker.perform_in(1.second, YAML::dump(model), YAML::dump(options), 'create_update_doc_nested') end diff --git a/app/models/annotations/tag.rb b/app/models/annotations/tag.rb index d365976f03..4755b39d62 100644 --- a/app/models/annotations/tag.rb +++ b/app/models/annotations/tag.rb @@ -84,6 +84,13 @@ def self.run_bulk_create_callbacks(ids_json, pmids_json) ProjectMedia.where(id: pmids).find_each { |pm| pm.tags_as_sentence } end + def hit_nested_objects_limit? + ret = false + pm = self.project_media + ret = pm.get_annotations(self.annotation_type).count > CheckConfig.get('nested_objects_limit', 10000, :integer) unless pm.nil? + ret + end + private def get_tag_text_reference diff --git a/app/models/tipline_request.rb b/app/models/tipline_request.rb index 385f64d258..ad0fb75147 100644 --- a/app/models/tipline_request.rb +++ b/app/models/tipline_request.rb @@ -64,6 +64,11 @@ def associated_graphql_id Base64.encode64("#{self.associated_type}/#{self.associated_id}") end + def hit_nested_objects_limit? + associated = self.associated + associated.tipline_requests.count > CheckConfig.get('nested_objects_limit', 10000, :integer) + end + private def set_team_and_user diff --git a/test/controllers/elastic_search_9_test.rb b/test/controllers/elastic_search_9_test.rb index a015628a52..6493472a94 100644 --- a/test/controllers/elastic_search_9_test.rb +++ b/test/controllers/elastic_search_9_test.rb @@ -206,7 +206,7 @@ def setup test "shoud add team filter by default" do t = create_team t2 = create_team - pm1 = create_project_media team: t, quote: 'test', disable_es_callbacks: false + pm1 = create_project_media team: t, quote: 'test', disable_es_callbacks: false pm2 = create_project_media team: t2, quote: 'test', disable_es_callbacks: false ProjectMedia.where(id: [pm1.id, pm2.id]).update_all(project_id: nil) options = { @@ -230,5 +230,26 @@ def setup Team.unstub(:current) end + test "should ignore index document that exceeds nested objects limit" do + team = create_team + pm = create_project_media team: team + stub_configs({ 'nested_objects_limit' => 2 }) do + tr = create_tipline_request associated: pm, disable_es_callbacks: false + tr2 = create_tipline_request associated: pm, disable_es_callbacks: false + tr3 = create_tipline_request associated: pm, disable_es_callbacks: false + t = create_tag annotated: pm, disable_es_callbacks: false + t2 = create_tag annotated: pm, disable_es_callbacks: false + t3 = create_tag annotated: pm, disable_es_callbacks: false + sleep 2 + es = $repository.find(pm.get_es_doc_id) + requests = es['requests'] + assert_equal 2, requests.size + assert_equal [tr.id, tr2.id], requests.collect{|r| r['id']}.sort + tags = es['tags'] + assert_equal 2, tags.size + assert_equal [t.id, t2.id], tags.collect{|i| i['id']}.sort + end + end + # Please add new tests to test/controllers/elastic_search_10_test.rb end