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

enhanced and used eager loading wrapper in order to avoid n+1, added further rendering optimizations

Jonas Jabari6c6e7ca5b0ce
diff --git a/app/components/work_packages/activities_tab/journals/index_component.rb b/app/components/work_packages/activities_tab/journals/index_component.rb
index 6d2022b92b9..469f0e3d4e1 100644
--- a/app/components/work_packages/activities_tab/journals/index_component.rb
+++ b/app/components/work_packages/activities_tab/journals/index_component.rb
@@ -63,34 +63,48 @@ module WorkPackages
journal_sorting == "desc"
end
- def journals
+ def base_journals
work_package
.journals
- .includes(:user, :notifications)
+ .includes(
+ :user,
+ :customizable_journals,
+ :attachable_journals,
+ :storable_journals,
+ :notifications
+ )
.reorder(version: journal_sorting)
.with_sequence_version
end
+ def journals
+ API::V3::Activities::ActivityEagerLoadingWrapper.wrap(base_journals)
+ end
+
def recent_journals
- if journal_sorting_desc?
- journals.first(MAX_RECENT_JOURNALS)
- else
- journals.last(MAX_RECENT_JOURNALS)
- end
+ recent_ones = if journal_sorting_desc?
+ base_journals.first(MAX_RECENT_JOURNALS)
+ else
+ base_journals.last(MAX_RECENT_JOURNALS)
+ end
+
+ API::V3::Activities::ActivityEagerLoadingWrapper.wrap(recent_ones)
end
def older_journals
- if journal_sorting_desc?
- journals.offset(MAX_RECENT_JOURNALS)
- else
- total = journals.count
- limit = [total - MAX_RECENT_JOURNALS, 0].max
- journals.limit(limit)
- end
+ older_ones = if journal_sorting_desc?
+ base_journals.offset(MAX_RECENT_JOURNALS)
+ else
+ total = base_journals.count
+ limit = [total - MAX_RECENT_JOURNALS, 0].max
+ base_journals.limit(limit)
+ end
+
+ API::V3::Activities::ActivityEagerLoadingWrapper.wrap(older_ones)
end
def journal_with_notes
- journals.where.not(notes: "")
+ base_journals.where.not(notes: "")
end
def wp_journals_grouped_emoji_reactions
diff --git a/app/components/work_packages/activities_tab/journals/item_component.rb b/app/components/work_packages/activities_tab/journals/item_component.rb
index 0030013b1fd..af733a7474c 100644
--- a/app/components/work_packages/activities_tab/journals/item_component.rb
+++ b/app/components/work_packages/activities_tab/journals/item_component.rb
@@ -75,7 +75,7 @@ module WorkPackages
end
def has_unread_notifications?
- journal.notifications.where(read_ian: false, recipient_id: User.current.id).any?
+ journal.has_unread_notifications_for_user?(User.current)
end
def notification_on_details?
diff --git a/app/components/work_packages/activities_tab/journals/item_component/details.html.erb b/app/components/work_packages/activities_tab/journals/item_component/details.html.erb
index 2bed453e20e..8a8c6c7c595 100644
--- a/app/components/work_packages/activities_tab/journals/item_component/details.html.erb
+++ b/app/components/work_packages/activities_tab/journals/item_component/details.html.erb
@@ -14,12 +14,12 @@
when :only_comments
render_empty_line(details_container) unless journal.notes.blank? && !journal.noop?
when :only_changes
- if journal.details.any?
+ if has_details?
render_details_header(details_container)
render_details(details_container)
end
else
- if journal.details.any?
+ if has_details?
render_details_header(details_container) unless journal.notes.present?
render_details(details_container)
elsif !journal.notes.present?
diff --git a/app/components/work_packages/activities_tab/journals/item_component/details.rb b/app/components/work_packages/activities_tab/journals/item_component/details.rb
index 59f0c5ed4fb..529fbcd6ab9 100644
--- a/app/components/work_packages/activities_tab/journals/item_component/details.rb
+++ b/app/components/work_packages/activities_tab/journals/item_component/details.rb
@@ -53,6 +53,14 @@ module WorkPackages
journal.id
end
+ def journal_details
+ @journal_details ||= journal.details
+ end
+
+ def has_details?
+ @has_details ||= journal_details.any?
+ end
+
def render_details_header(details_container)
details_container.with_row(
flex_layout: true,
@@ -223,7 +231,7 @@ module WorkPackages
end
def render_journal_details(details_container_inner)
- journal.details.each do |detail|
+ journal_details.each do |detail|
rendered_detail = journal.render_detail(detail)
render_single_detail(details_container_inner, rendered_detail) if rendered_detail.present?
end
diff --git a/app/models/journal.rb b/app/models/journal.rb
index 04f66834916..95da1c3ed52 100644
--- a/app/models/journal.rb
+++ b/app/models/journal.rb
@@ -177,6 +177,16 @@ class Journal < ApplicationRecord
cause_type.present?
end
+ def has_unread_notifications_for_user?(user)
+ # we optionally set the instance variable @unread_notifications in the ActivityEagerLoadingWrapper
+ # in order to avoid N+1 queries
+ if instance_variable_defined?(:@unread_notifications)
+ @unread_notifications&.any? { |notification| notification.recipient_id == user.id }
+ else
+ notifications.where(read_ian: false, recipient_id: user.id).any?
+ end
+ end
+
private
def has_file_links?
diff --git a/lib/api/v3/activities/activity_eager_loading_wrapper.rb b/lib/api/v3/activities/activity_eager_loading_wrapper.rb
index b21a7abc355..8b7f20a6123 100644
--- a/lib/api/v3/activities/activity_eager_loading_wrapper.rb
+++ b/lib/api/v3/activities/activity_eager_loading_wrapper.rb
@@ -36,6 +36,7 @@ module API
set_journable(journals)
set_predecessor(journals)
set_data(journals)
+ set_notifications(journals)
end
super
@@ -72,6 +73,20 @@ module API
end
end
+ def set_notifications(journals)
+ notifications = Notification
+ .where(journal_id: journals.map(&:id))
+ .where(read_ian: false)
+ .group_by(&:journal_id)
+
+ journals.each do |journal|
+ journal.instance_variable_set(
+ :@unread_notifications,
+ notifications[journal.id]
+ )
+ end
+ end
+
def journable_by_type_and_id(journals)
journals
.group_by(&:journable_type)