diff --git a/app/models/group.rb b/app/models/group.rb index 6810623052..54895740bb 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -91,19 +91,22 @@ def shared_namespace_metadata_keys(namespace) metadata_fields end - def aggregated_samples_count + def aggregated_samples_count # rubocop:disable Metrics/AbcSize aggregated_samples_count = samples_count return aggregated_samples_count unless shared_namespaces.any? - shared_projects_ids = [] - shared_groups.self_and_descendants.where(type: [Namespaces::ProjectNamespace.sti_name]).where.not(id: self_and_descendants_of_type([Namespaces::ProjectNamespace.sti_name]).ids).each do |shared_project_namespace| - aggregated_samples_count += shared_project_namespace.project.samples.size - shared_projects_ids.push(shared_project_namespace.id) + projects_ids = [] + shared_groups.self_and_descendants.where(type: [Namespaces::ProjectNamespace.sti_name]) + .where.not(id: self_and_descendants_of_type([Namespaces::ProjectNamespace.sti_name]).ids) + .find_each do |project_namespace| + aggregated_samples_count += project_namespace.project.samples.size + projects_ids.push(project_namespace.id) end - shared_project_namespaces.where.not(id: self_and_descendants_of_type([Namespaces::ProjectNamespace.sti_name]).ids).each do |shared_project_namespace| - aggregated_samples_count += shared_project_namespace.project.samples.size if not shared_projects_ids.include?(shared_project_namespace.id) + shared_project_namespaces.where.not(id: self_and_descendants_of_type([Namespaces::ProjectNamespace.sti_name]).ids) + .find_each do |project_namespace| + aggregated_samples_count += project_namespace.project.samples.size if projects_ids.exclude?(project_namespace.id) end aggregated_samples_count end