diff --git a/ee/app/services/security/ingestion/ingest_slice_base_service.rb b/ee/app/services/security/ingestion/ingest_slice_base_service.rb index ef8ade79efcff681f918cb2adb9ebf0cb95b4bfe..227b9e414043e2fd1089a47c63b435e888d4829f 100644 --- a/ee/app/services/security/ingestion/ingest_slice_base_service.rb +++ b/ee/app/services/security/ingestion/ingest_slice_base_service.rb @@ -15,7 +15,8 @@ def initialize(pipeline, finding_maps) def execute Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.temporary_ignore_tables_in_transaction( %w[ - project_statistics + project_security_statistics + project_settings security_findings vulnerabilities vulnerability_flags diff --git a/ee/app/services/security/ingestion/tasks/increase_counters_task.rb b/ee/app/services/security/ingestion/tasks/increase_counters_task.rb index bcbfaf410d845b9ff7a6ff81b7379f4960cc1dd6..3db57f9d2065739875eff89250eb51734f96bead 100644 --- a/ee/app/services/security/ingestion/tasks/increase_counters_task.rb +++ b/ee/app/services/security/ingestion/tasks/increase_counters_task.rb @@ -6,7 +6,7 @@ module Tasks class IncreaseCountersTask < AbstractTask def execute counts_by_projects.each do |project, new_vulnerability_count| - project.statistics.increase_vulnerability_counter!(new_vulnerability_count) + project.security_statistics.increase_vulnerability_counter!(new_vulnerability_count) end end diff --git a/ee/app/services/vulnerabilities/create_service_base.rb b/ee/app/services/vulnerabilities/create_service_base.rb index fb5ac2d5d729cc338f363b95e76395840d78bb7d..fe299fa634229d20a752d6f90e7bdc61f1d6d653 100644 --- a/ee/app/services/vulnerabilities/create_service_base.rb +++ b/ee/app/services/vulnerabilities/create_service_base.rb @@ -165,5 +165,9 @@ def schedule_updating_traversal_ids_if_needed def reloaded_project @reloaded_project ||= project.reset end + + def update_security_statistics! + project.security_statistics.increase_vulnerability_counter!(1) + end end end diff --git a/ee/app/services/vulnerabilities/manually_create_service.rb b/ee/app/services/vulnerabilities/manually_create_service.rb index eecd633bc6fee566c92df5d9c93fda0f586eeb0e..0b4bd66ad72a14dff151d5619fc41438cf772c08 100644 --- a/ee/app/services/vulnerabilities/manually_create_service.rb +++ b/ee/app/services/vulnerabilities/manually_create_service.rb @@ -41,15 +41,14 @@ def execute vulnerability.vulnerability_read.update!(traversal_ids: project.namespace.traversal_ids) + update_security_statistics! + Statistics::UpdateService.update_for(vulnerability) ServiceResponse.success(payload: { vulnerability: vulnerability }) end - Project.transaction do - project.mark_as_vulnerable! - project.statistics.increase_vulnerability_counter!(1) - end + project.mark_as_vulnerable! process_archival_and_traversal_ids_changes if response.success? diff --git a/ee/app/services/vulnerabilities/removal/remove_from_project_service.rb b/ee/app/services/vulnerabilities/removal/remove_from_project_service.rb index e98956cf20d721292ba168cd9793bb40679a56c7..f5037568e28f20dfa3309ec4c4b15f1e269ec35d 100644 --- a/ee/app/services/vulnerabilities/removal/remove_from_project_service.rb +++ b/ee/app/services/vulnerabilities/removal/remove_from_project_service.rb @@ -42,9 +42,9 @@ def execute delete_resources_by_vulnerabilities delete_vulnerabilities delete_findings - end - update_project_vulnerabilities_count if update_counts + update_project_vulnerabilities_count if update_counts + end true end @@ -70,7 +70,7 @@ def delete_findings end def update_project_vulnerabilities_count - project.statistics.decrease_vulnerability_counter!(batch_size) + project.security_statistics.decrease_vulnerability_counter!(batch_size) end def batch_size diff --git a/ee/app/services/vulnerabilities/starboard_vulnerability_create_service.rb b/ee/app/services/vulnerabilities/starboard_vulnerability_create_service.rb index 58eb5a9c085cafc9a24c56ce141c52678ab983c6..53d58bf63c592d5da0d4d51991d3597209bed86f 100644 --- a/ee/app/services/vulnerabilities/starboard_vulnerability_create_service.rb +++ b/ee/app/services/vulnerabilities/starboard_vulnerability_create_service.rb @@ -51,7 +51,6 @@ def execute vulnerability_scanners cluster_agents project_settings - project_statistics ], url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/476584' ) do response = Vulnerability.transaction do @@ -63,7 +62,7 @@ def execute vulnerability.vulnerability_read.update!(traversal_ids: project.namespace.traversal_ids) project.mark_as_vulnerable! - project.statistics.increase_vulnerability_counter!(1) + update_security_statistics! Statistics::UpdateService.update_for(vulnerability) diff --git a/ee/spec/services/security/ingestion/tasks/increase_counters_task_spec.rb b/ee/spec/services/security/ingestion/tasks/increase_counters_task_spec.rb index 4999b083f72843489d0762f7ac1f85f63d0e0b53..66a54955962d5b2ce5900fdbed25ad6d79fcd199 100644 --- a/ee/spec/services/security/ingestion/tasks/increase_counters_task_spec.rb +++ b/ee/spec/services/security/ingestion/tasks/increase_counters_task_spec.rb @@ -9,13 +9,13 @@ let(:finding_map_1) { create(:finding_map, pipeline: pipeline, new_record: true) } let(:finding_map_2) { create(:finding_map, pipeline: pipeline, new_record: false) } - let(:project_statistics) { pipeline.project.statistics } + let(:security_statistics) { pipeline.project.security_statistics } let(:service_object) { described_class.new(pipeline, [finding_map_1, finding_map_2]) } subject(:execute_task) { service_object.execute } it 'increases vulnerability count' do - expect { execute_task }.to change { project_statistics.reload.vulnerability_count }.by(1) + expect { execute_task }.to change { security_statistics.reload.vulnerability_count }.by(1) end end @@ -34,8 +34,8 @@ subject(:execute_task) { service_object.execute } it 'increases the vulnerability count for projects' do - expect { execute_task }.to change { pipeline_1.project.statistics.reload.vulnerability_count }.by(1) - .and change { pipeline_2.project.statistics.reload.vulnerability_count }.by(1) + expect { execute_task }.to change { pipeline_1.project.security_statistics.reload.vulnerability_count }.by(1) + .and change { pipeline_2.project.security_statistics.reload.vulnerability_count }.by(1) end end end diff --git a/ee/spec/services/vulnerabilities/manually_create_service_spec.rb b/ee/spec/services/vulnerabilities/manually_create_service_spec.rb index a9ea9872e04c0e2c9d57e143a40c65fe7d292c3e..6fc0c25ed2fbd1c2920686d83990538461072b6c 100644 --- a/ee/spec/services/vulnerabilities/manually_create_service_spec.rb +++ b/ee/spec/services/vulnerabilities/manually_create_service_spec.rb @@ -86,12 +86,8 @@ end end - it 'does not exceed query limit' do - expect { subject }.not_to exceed_query_limit(32) - end - it 'increases vulnerability count by 1' do - expect { subject }.to change { project.reload.statistics.vulnerability_count }.by(1) + expect { subject }.to change { project.reload.security_statistics.vulnerability_count }.by(1) end it 'creates a new Vulnerability' do diff --git a/ee/spec/services/vulnerabilities/removal/remove_from_project_service_spec.rb b/ee/spec/services/vulnerabilities/removal/remove_from_project_service_spec.rb index 92a79fee9041708b1aebcfb9ec8aee52fae07b49..b6526b2a5c491b0af61939895e6ab8bbccee4762 100644 --- a/ee/spec/services/vulnerabilities/removal/remove_from_project_service_spec.rb +++ b/ee/spec/services/vulnerabilities/removal/remove_from_project_service_spec.rb @@ -5,7 +5,7 @@ RSpec.describe Vulnerabilities::Removal::RemoveFromProjectService, feature_category: :vulnerability_management do describe '#execute' do let_it_be(:project) { create(:project) } - let_it_be(:project_statistics) { project.statistics } + let_it_be(:security_statistics) { project.security_statistics } let_it_be(:vulnerabilities) do create_list( :vulnerability, @@ -93,7 +93,7 @@ .and change { Vulnerabilities::ExternalIssueLink.count }.by(-1) .and change { Vulnerabilities::FindingRemediation.count }.by(-1) .and change { Vulnerabilities::HistoricalStatistic.count }.by(-1) - .and change { project_statistics.reload.vulnerability_count }.by(-3) + .and change { security_statistics.reload.vulnerability_count }.by(-3) expect(Vulnerabilities::Statistics::AdjustmentWorker).to have_received(:perform_async).with([project.id]) end @@ -107,7 +107,7 @@ .and change { Vulnerabilities::Read.count }.by(-1) .and change { Vulnerabilities::Finding.count }.by(-1) .and change { Vulnerabilities::FindingIdentifier.count }.by(-1) - .and change { project_statistics.reload.vulnerability_count }.by(-1) + .and change { security_statistics.reload.vulnerability_count }.by(-1) .and not_change { Vulnerabilities::Flag.count } .and not_change { VulnerabilityUserMention.count } .and not_change { Vulnerabilities::Feedback.count } @@ -146,7 +146,7 @@ .and change { Vulnerabilities::Finding::Evidence.count }.by(-1) .and change { Vulnerabilities::ExternalIssueLink.count }.by(-1) .and change { Vulnerabilities::FindingRemediation.count }.by(-1) - .and change { project_statistics.reload.vulnerability_count }.by(-2) + .and change { security_statistics.reload.vulnerability_count }.by(-2) .and not_change { Vulnerabilities::Feedback.count } .and not_change { Vulnerabilities::Identifier.count } .and not_change { Vulnerabilities::HistoricalStatistic.count } diff --git a/ee/spec/services/vulnerabilities/starboard_vulnerability_create_service_spec.rb b/ee/spec/services/vulnerabilities/starboard_vulnerability_create_service_spec.rb index 1578cf3c2f32be4975803029420aef9854f47b6e..a1462cb688cd70a424ded3463fdce08b903537fc 100644 --- a/ee/spec/services/vulnerabilities/starboard_vulnerability_create_service_spec.rb +++ b/ee/spec/services/vulnerabilities/starboard_vulnerability_create_service_spec.rb @@ -103,7 +103,7 @@ end it 'increases vulnerability_count by 1' do - expect { subject }.to change { project.reload.statistics.vulnerability_count }.by(1) + expect { subject }.to change { project.reload.security_statistics.vulnerability_count }.by(1) end it 'sets the `traversal_ids` of the `vulnerability_reads` record' do