Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

improve performance of relations visible scope

ulferts8b563f7e577b
diff --git a/app/components/work_package_relations_tab/relations_mediator.rb b/app/components/work_package_relations_tab/relations_mediator.rb
index f333793f569..5de66987a39 100644
--- a/app/components/work_package_relations_tab/relations_mediator.rb
+++ b/app/components/work_package_relations_tab/relations_mediator.rb
@@ -38,7 +38,7 @@ class WorkPackageRelationsTab::RelationsMediator
end
def visible_relations
- @visible_relations ||= work_package.relations.includes(:to, :from).visible
+ @visible_relations ||= work_package.visible_relations.includes(:to, :from)
end
def visible_children
diff --git a/app/models/relations/scopes/visible.rb b/app/models/relations/scopes/visible.rb
index e2522cc1fdb..da9a820ae10 100644
--- a/app/models/relations/scopes/visible.rb
+++ b/app/models/relations/scopes/visible.rb
@@ -34,10 +34,25 @@ module Relations::Scopes
# Returns all relationships visible to the user. The relationships have to be:
# * Start (from_id) on a work package visible to the user (view_work_packages in the work package's project)
# * End (to_id) on a work package visible to the user (view_work_packages in the work package's project)
+ #
+ # In some cases, the resulting SQL query as is might not run in a performant matter. This happens in
+ # cases where there are a lot of work packages and relations. PostgreSql then sometimes chooses to use a
+ # full table scan on work_packages expecting a lot of results.
+ # Then, the query can be optimized by providing a +work_package_focus_scope+ which is used as a subquery
+ # on work_packages on the id column. This is beneficial if not all work packages are to be
+ # considered but only a subset. As this directly excludes work packages, it can also be used
+ # the wrong way.
# @param [User] user
- def visible(user = User.current)
- where(from_id: WorkPackage.visible(user))
- .where(to_id: WorkPackage.visible(user))
+ # @param [ActiveRecord::Relation, Arel::SelectManager] work_package_focus_scope
+ def visible(user = User.current, work_package_focus_scope: nil)
+ wp_arel = work_package_focus_scope.respond_to?(:arel) ? work_package_focus_scope.arel : work_package_focus_scope
+ visible_work_packages = WorkPackage.visible(user)
+
+ visible_work_packages = visible_work_packages.where(WorkPackage.arel_table[:id].in(wp_arel)) if wp_arel
+
+ with(visible_work_packages:)
+ .where(from_id: WorkPackage.from("visible_work_packages #{WorkPackage.table_name}").select(:id))
+ .where(to_id: WorkPackage.from("visible_work_packages #{WorkPackage.table_name}").select(:id))
end
end
end
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index a16611e48b0..9e1a6eb3441 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -242,11 +242,6 @@ class WorkPackage < ApplicationRecord
.exists?
end
- def visible_relations(user)
- relations
- .visible(user)
- end
-
def add_time_entry(attributes = {})
attributes.reverse_merge!(
project:,
diff --git a/app/models/work_packages/relations.rb b/app/models/work_packages/relations.rb
index 12dc4d3da50..b17a9f63949 100644
--- a/app/models/work_packages/relations.rb
+++ b/app/models/work_packages/relations.rb
@@ -87,4 +87,31 @@ module WorkPackages::Relations
dependent: :nullify,
inverse_of: :to
end
+
+ def visible_relations(user = User.current)
+ # The query in here is used to improve performance by reducing the number of
+ # work packages that have to be checked for whether they are visible.
+ # Since the method looks for relations on the current work package, only work packages
+ # that are on either end of a relation with the current work package need to be considered.
+ # The number should be quite small compared to the total number of work packages.
+ relation_from_or_to = Relation
+ .select(
+ <<~SQL.squish
+ CASE
+ WHEN from_id = #{id}
+ THEN to_id
+ ELSE from_id
+ END id
+ SQL
+ )
+ .where("relations.from_id = :id OR relations.to_id = :id", id: id)
+ .arel
+
+ self_id = Arel.sql("SELECT #{id} AS id")
+
+ wp_focus_scope = Arel::Nodes::UnionAll.new(relation_from_or_to, self_id)
+
+ relations
+ .visible(user, work_package_focus_scope: wp_focus_scope)
+ end
end
diff --git a/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb b/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
index 482b820fa2e..7763d4dc3cd 100644
--- a/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
+++ b/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
@@ -155,7 +155,7 @@ module XlsExport::WorkPackage::Exporter
end
def work_package_relations(work_package)
- work_package.relations.visible
+ work_package.visible_relations
end
end
end
diff --git a/spec/models/work_package/work_package_relations_spec.rb b/spec/models/work_package/work_package_relations_spec.rb
index 4132ad113c6..2fca4a7988b 100644
--- a/spec/models/work_package/work_package_relations_spec.rb
+++ b/spec/models/work_package/work_package_relations_spec.rb
@@ -254,4 +254,36 @@ RSpec.describe WorkPackage do
end
end
end
+
+ describe "#visible_relations" do
+ let!(:user) { create(:user) }
+ let!(:view_work_packages_role) { create(:project_role, permissions: %i[view_work_packages]) }
+ let!(:no_permission_role) { create(:project_role, permissions: %i[]) }
+ let!(:sharing_role) { create(:work_package_role, permissions: %i[view_work_packages]) }
+
+ let!(:visible_project) { create(:project, members: { user => view_work_packages_role }) }
+ let!(:invisible_project) { create(:project, members: { user => no_permission_role }) }
+
+ let!(:origin) { create(:work_package, project: visible_project) }
+ let!(:visible_work_package) { create(:work_package, project: visible_project) }
+ let!(:another_visible_work_package) { create(:work_package, project: visible_project) }
+ let!(:invisible_work_package) { create(:work_package, project: invisible_project) }
+ let!(:other_work_package) { create(:work_package, project: visible_project) }
+ let!(:shared_work_package) do
+ create(:work_package, project: invisible_project) do |wp|
+ create(:work_package_member, entity: wp, user: user, roles: [sharing_role])
+ end
+ end
+
+ let!(:visible_relation) { create(:relation, from: origin, to: visible_work_package) }
+ let!(:inverted_visible_relation) { create(:relation, to: origin, from: another_visible_work_package) }
+ let!(:invisible_relation) { create(:relation, from: origin, to: invisible_work_package) }
+ let!(:shared_relation) { create(:relation, from: origin, to: shared_work_package) }
+ let!(:other_relation) { create(:relation, from: other_work_package, to: visible_work_package) }
+
+ it "returns all relations from the called on work package visible to the user" do
+ expect(origin.visible_relations(user))
+ .to contain_exactly(visible_relation, inverted_visible_relation, shared_relation)
+ end
+ end
end