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

speedup project index api

ulferts64fcbb44fbc9
diff --git a/lib/api/v3/projects/project_collection_representer.rb b/lib/api/v3/projects/project_collection_representer.rb
index 96850ec2ecc..faf015c8db4 100644
--- a/lib/api/v3/projects/project_collection_representer.rb
+++ b/lib/api/v3/projects/project_collection_representer.rb
@@ -40,6 +40,12 @@ module API
self.to_eager_load = ::API::V3::Projects::ProjectRepresenter.to_eager_load
self.checked_permissions = ::API::V3::Projects::ProjectRepresenter.checked_permissions
+
+ def initialize(models, self_link, current_user:)
+ super
+
+ @represented = ::API::V3::Projects::ProjectEagerLoadingWrapper.wrap(represented)
+ end
end
end
end
diff --git a/lib/api/v3/projects/project_eager_loading_wrapper.rb b/lib/api/v3/projects/project_eager_loading_wrapper.rb
new file mode 100644
index 00000000000..d99a73493ba
--- /dev/null
+++ b/lib/api/v3/projects/project_eager_loading_wrapper.rb
@@ -0,0 +1,77 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module Projects
+ class ProjectEagerLoadingWrapper < SimpleDelegator
+ private_class_method :new
+
+ # Because of the ruby method lookup,
+ # wrapping the work_package here and define the
+ # available_custom_fields methods on the wrapper does not suffice.
+ # We thus extend each work package.
+ def initialize(project)
+ super
+ project.extend(CustomFieldAccessorPatch)
+ end
+
+ ##
+ # Workaround against warnings in flatten
+ # delegator does not forward private method #to_ary
+ def to_ary
+ __getobj__.send(:to_ary)
+ end
+
+ class << self
+ def wrap(projects)
+ custom_fields = if projects && !projects.empty?
+ projects.first.available_custom_fields
+ end
+
+ projects
+ .map { |project| new(project) }
+ .each { |project| project.available_custom_fields = custom_fields }
+ end
+ end
+
+ module CustomFieldAccessorPatch
+ def available_custom_fields
+ @available_custom_fields
+ end
+
+ def available_custom_fields=(fields)
+ @available_custom_fields = fields
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rb
index 29a09bc212a..e4a4c680e8a 100644
--- a/lib/api/v3/projects/project_representer.rb
+++ b/lib/api/v3/projects/project_representer.rb
@@ -219,6 +219,9 @@ module API
'Project'
end
+ self.to_eager_load = [:status,
+ custom_values: :custom_field]
+
self.checked_permissions = [:add_work_packages]
end
end
diff --git a/lib/api/v3/projects/projects_api.rb b/lib/api/v3/projects/projects_api.rb
index 55128ddfac2..3221c220fda 100644
--- a/lib/api/v3/projects/projects_api.rb
+++ b/lib/api/v3/projects/projects_api.rb
@@ -32,7 +32,7 @@ module API
class ProjectsAPI < ::API::OpenProjectAPI
resources :projects do
get &::API::V3::Utilities::Endpoints::Index.new(model: Project,
- scope: -> { Project.visible(User.current).includes(:enabled_modules) })
+ scope: -> { Project.visible(User.current).includes(:enabled_modules).includes(ProjectRepresenter.to_eager_load) })
.mount
post &::API::V3::Utilities::Endpoints::Create.new(model: Project)