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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
adapt eager loading for wp collection to attributes_at_timestamp changes
ulferts707fdcc88fda
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rbindex 50bc7948adf..2e8231d7904 100644--- a/app/models/journable/with_historic_attributes.rb+++ b/app/models/journable/with_historic_attributes.rb@@ -71,7 +71,9 @@#class Journable::WithHistoricAttributes < SimpleDelegatorattr_accessor :timestamps, :query, :include_only_changed_attributes, :attributes_by_timestamp,- :matches_query_filters_at_timestamps, :exists_at_timestamps+ :matches_query_filters_at_timestamps, :exists_at_timestamps,+ :journables_by_timestamp+def initialize(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)super(journable)@@ -89,6 +91,7 @@ class Journable::WithHistoricAttributes < SimpleDelegatorself.attributes_by_timestamp = {}self.matches_query_filters_at_timestamps = []self.exists_at_timestamps = []+ self.journables_by_timestamp = {}enddef self.wrap(journable_or_journables, timestamps: nil, query: nil, include_only_changed_attributes: false)@@ -134,6 +137,7 @@ class Journable::WithHistoricAttributes < SimpleDelegatordef assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)attributes_by_timestamp[timestamp.to_s] = extract_historic_attributes_from(historic_journable:) if historic_journable+ journables_by_timestamp[timestamp.to_s] = historic_journablematches_query_filters_at_timestamps << timestamp if matching_journableexists_at_timestamps << timestamp if historic_journableenddiff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rbindex 2878bbe8f83..713b25a00ca 100644--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb@@ -26,22 +26,30 @@# See COPYRIGHT and LICENSE files for more details.#++-# rubocop:disable Metrics/AbcSizemodule API::V3::WorkPackages::EagerLoadingclass HistoricAttributes < Baseattr_accessor :timestamps, :querydef apply(work_package)- work_package_with_historic_attributes = work_packages_with_historic_attributes.detect { |wp| wp.id == work_package.id }- work_package.attributes = work_package_with_historic_attributes.attributes.except('timestamp')- work_package.baseline_attributes = work_package_with_historic_attributes.baseline_attributes- work_package.attributes_by_timestamp = work_package_with_historic_attributes.attributes_by_timestamp- work_package.timestamps = work_package_with_historic_attributes.timestamps- work_package.baseline_timestamp = work_package_with_historic_attributes.baseline_timestamp- work_package.matches_query_filters_at_baseline_timestamp = \- work_package_with_historic_attributes.matches_query_filters_at_baseline_timestamp?- work_package.matches_query_filters_at_timestamps = work_package_with_historic_attributes.matches_query_filters_at_timestamps- work_package.exists_at_timestamps = work_package_with_historic_attributes.exists_at_timestamps+ work_package_with_historic_attributes = work_packages_with_historic_attributes[work_package.id]++ set_non_delegated_properties(work_package,+ work_package_with_historic_attributes,+ work_package_with_historic_attributes.timestamps.last.to_s)++ work_package.at_timestamps = work_package_with_historic_attributes+ .journables_by_timestamp+ .values+ .map do |wp|+ HistoricAttributesDelegator.new(wp).tap do |wrapped_wp|+ # TODO: Don't know why that needs to be casted+ wrapped_wp.timestamp = Timestamp.new(wp.timestamp)++ set_non_delegated_properties(wrapped_wp,+ work_package_with_historic_attributes,+ wrapped_wp.timestamp)+ end+ endenddef self.module@@ -50,11 +58,20 @@ module API::V3::WorkPackages::EagerLoadingprivate+ def set_non_delegated_properties(work_package, source, timestamp)+ work_package.matches_filters_at_timestamp = source.matches_query_filters_at_timestamps.include?(timestamp)+ work_package.exists_at_timestamp = source.exists_at_timestamps.include?(timestamp)+ work_package.attributes_changed_to_baseline = (source.attributes_by_timestamp[timestamp.to_s]&.to_h || {}).keys.map(&:to_s)+ work_package.with_query = source.query.present?+ end++ # TODO: prepare by indexing by iddef work_packages_with_historic_attributes@work_packages_with_historic_attributes ||= begin@timestamps ||= @query.try(:timestamps) || []Journable::WithHistoricAttributes \.wrap_multiple(work_packages, timestamps: @timestamps, query: @query, include_only_changed_attributes: true)+ .index_by(&:id)endendend@@ -63,25 +80,27 @@ module API::V3::WorkPackages::EagerLoadingextend ActiveSupport::Concernincluded do- attr_accessor :baseline_attributes, :attributes_by_timestamp, :timestamps, :baseline_timestamp,- :matches_query_filters_at_baseline_timestamp,- :matches_query_filters_at_timestamps,- :exists_at_timestamps- end+ attr_accessor :at_timestamps,+ :attributes_changed_to_baseline,+ # TODO: should be possible to get rid of. Added to cast it to a Timestamp again+ :timestamp++ attr_writer :with_query,+ :exists_at_timestamp,+ :matches_filters_at_timestamp- # Does the work package match the query filter at the baseline timestamp?- # Returns `nil` if no query is given.- #- def matches_query_filters_at_baseline_timestamp?- matches_query_filters_at_timestamps.any? ? matches_query_filters_at_baseline_timestamp : nil+ def with_query?; @with_query; end+ def exists_at_timestamp?; @exists_at_timestamp; end+ def matches_filters_at_timestamp?; @matches_filters_at_timestamp; endend- # Does the work package match the query filter at the given timestamp?- # Returns `nil` if no query is given.- #- def matches_query_filters_at_timestamp?(timestamp)- matches_query_filters_at_timestamps.any? ? matches_query_filters_at_timestamps.include?(timestamp) : nil+ def wrapped?+ trueendend++ # TODO: Get this in line with the rest of the eager loading+ class HistoricAttributesDelegator < SimpleDelegator+ include HistoricAttributesAccessors+ endend-# rubocop:enable Metrics/AbcSizediff --git a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rbindex 70e40b298b1..e55192f0bae 100644--- a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb+++ b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb@@ -54,20 +54,14 @@ module APISUPPORTED_PROPERTIES = (SUPPORTED_NON_LINK_PROPERTIES + SUPPORTED_LINK_PROPERTIES).freeze- def initialize(model, current_user:, timestamp:)- super(model.journables_by_timestamp[timestamp.to_s], current_user:, embed_links:, timestamps: [timestamp])-- self.model_with_history = model- self.timestamp = timestamp+ def initialize(model, current_user:)+ super(model, current_user:, embed_links:, timestamps: [model.timestamp])endprivate- attr_accessor :timestamp,- :model_with_history-def representable_map(*)- Representable::Binding::Map.new(super.select { |binding| rendered_properties.include?(binding.name) })+ Representable::Binding::Map.new(super.select { |bind| rendered_properties.include?(bind.name) })enddef compile_links_for(configs, *args)@@ -77,29 +71,30 @@ module APIdef rendered_properties@rendered_properties ||= begin- # This conversion is good enough for the set of supported properties as it- # * Converts assigned_to_id to assignee- # * does not mess with `start_date` and `due_date`- changed_properties = (model_with_history.attributes_by_timestamp[timestamp.to_s]&.to_h || {})- .keys- .map(&:to_s)- .map do |property|- if property.ends_with?('_id')- API::Utilities::PropertyNameConverter.from_ar_name(property)- else- property- end- end+ properties = (changed_properties_as_api_name & SUPPORTED_PROPERTIES) + ['_meta']- properties = (changed_properties & SUPPORTED_PROPERTIES) + ['_meta']-- if model_with_history.exists_at_timestamps.include?(timestamp.to_s)+ if represented.exists_at_timestamp?properties + ["links", :self]elsepropertiesendendend++ def changed_properties_as_api_name+ # This conversion is good enough for the set of supported properties as it+ # * Converts assigned_to_id to assignee+ # * does not mess with `start_date` and `due_date`+ represented+ .attributes_changed_to_baseline+ .map do |property|+ if property.ends_with?('_id')+ API::Utilities::PropertyNameConverter.from_ar_name(property)+ else+ property+ end+ end+ endendendenddiff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rbindex 5d634289418..3523f8b66b1 100644--- a/lib/api/v3/work_packages/work_package_representer.rb+++ b/lib/api/v3/work_packages/work_package_representer.rb@@ -332,26 +332,22 @@ module APIproperty :_meta,if: ->(*) {- represented.respond_to?(:matches_query_filters_at_timestamps) &&- represented.respond_to?(:timestamps) &&- represented.timestamps != [Timestamp.now]+ timestamps_active?},getter: ->(*) {{- # This meta property states whether the attributes of the work package at the- # last given timestamp (commonly the current time) match the filters of the- # query. https://github.com/opf/openproject/pull/11783- 'matchesFilters': represented.matches_query_filters_at_timestamp?(timestamps.last),-- # This meta property states whether the work package exists at the last given- # timestamp (commonly the current time).+ # This meta property states whether the work package exists at time.# https://github.com/opf/openproject/pull/11783#issuecomment-1374897874- #- 'exists': represented.exists_at_timestamps.include?(timestamps.last),+ 'exists': represented.exists_at_timestamp?,# This meta property holds the timestamp of the data of the work package.#- 'timestamp': timestamps.last.to_s+ 'timestamp': timestamps.last.to_s,++ # This meta property states whether the attributes of the work package at the+ # timestamp match the filters of the query.+ # https://github.com/opf/openproject/pull/11783+ 'matchesFilters': represented.with_query? ? represented.matches_filters_at_timestamp? : nil}.compact},uncacheable: true,@@ -480,16 +476,12 @@ module APIproperty :attributes_by_timestamp,if: ->(*) {- represented.respond_to?(:attributes_by_timestamp) &&- represented.attributes_by_timestamp.present? &&- represented.respond_to?(:timestamps) &&- represented.timestamps != [Timestamp.now]+ timestamps_active?},getter: ->(*) do- represented.timestamps.collect do |timestamp|+ represented.at_timestamps.map do |work_package_at_timestamp|API::V3::WorkPackages::WorkPackageAtTimestampRepresenter- .create(represented,- timestamp:,+ .create(work_package_at_timestamp,current_user:)endend,@@ -683,6 +675,10 @@ module API@ordered_custom_actions ||= represented.custom_actions(current_user).to_a.sort_by(&:position)end+ def timestamps_active?+ timestamps.any?+ end+# Attachments need to be eager loaded for the descriptionself.to_eager_load = %i[parenttypediff --git a/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rbindex b50d2111871..952e0d89e5a 100644--- a/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb@@ -85,50 +85,34 @@ describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' dendlet(:timestamp) { Timestamp.new }- let(:attributes_by_timestamp) do- {- timestamp.to_s => OpenStruct.new(work_package.attributes)- }- end- let(:journables_by_timestamp) do- {- timestamp.to_s => work_package- }- end- let(:exists_at_timestamps) { [timestamp.to_s] }- let(:matches_query_filters_at_timestamps) { { timestamp.to_s => true } }+ let(:attributes_changed_to_baseline) { work_package.attributes.keys }+ let(:exists_at_timestamp) { true }+ let(:with_query) { true }+ let(:matches_filters_at_timestamp) { true }let(:model) do- # Mimicking the eager loading wrapperwork_package.tap do |model|+ # Mimicking the eager loading wrapperallow(model)- .to receive(:journables_by_timestamp)- .and_return(journables_by_timestamp)- allow(model)- .to receive(:attributes_by_timestamp)- .and_return(attributes_by_timestamp)+ .to receive(:timestamp)+ .and_return(timestamp)allow(model)- .to receive(:exists_at_timestamps)- .and_return(exists_at_timestamps)+ .to receive(:attributes_changed_to_baseline)+ .and_return(attributes_changed_to_baseline)allow(model)- .to receive(:matches_query_filters_at_timestamps)- .and_return(matches_query_filters_at_timestamps)+ .to receive(:exists_at_timestamp?)+ .and_return(exists_at_timestamp)allow(model)- .to receive(:matches_query_filters_at_timestamp?)- .with(timestamp)- .and_return(matches_query_filters_at_timestamps[timestamp.to_s])- #allow(model)- # .to receive(:respond_to?)- # .and_call_original+ .to receive(:with_query?)+ .and_return(with_query)allow(model)- .to receive(:respond_to?)- .with(:matches_query_filters_at_timestamps)- .and_return(true)+ .to receive(:matches_filters_at_timestamp?)+ .and_return(matches_filters_at_timestamp)endendlet(:representer) do- described_class.create(model, current_user:, timestamp:)+ described_class.create(model, current_user:)endsubject(:generated) { representer.to_json }@@ -188,11 +172,7 @@ describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' dendcontext 'with a subset of supported properties' do- let(:attributes_by_timestamp) do- {- timestamp.to_s => OpenStruct.new(work_package.attributes.slice('start_date', 'assigned_to_id', 'version_id'))- }- end+ let(:attributes_changed_to_baseline) { %w[start_date assigned_to_id version_id] }let(:expected_json) do{@@ -226,11 +206,7 @@ describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' dendcontext 'without a linked property' do- let(:attributes_by_timestamp) do- {- timestamp.to_s => OpenStruct.new(work_package.attributes.slice('subject', 'start_date'))- }- end+ let(:attributes_changed_to_baseline) { %w[subject start_date] }let(:expected_json) do{@@ -259,11 +235,7 @@ describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' dcontext 'with a nil value for a linked property' dolet(:assigned_to) { nil }- let(:attributes_by_timestamp) do- {- timestamp.to_s => OpenStruct.new(work_package.attributes.slice('assigned_to_id'))- }- end+ let(:attributes_changed_to_baseline) { %w[assigned_to_id] }let(:expected_json) do{@@ -291,13 +263,11 @@ describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'rendering' dendcontext 'without the timestamp being in the attributes_by_timestamp collection' do- let(:attributes_by_timestamp) do- {}- end+ let(:attributes_changed_to_baseline) { %w[] }- let(:exists_at_timestamps) { [] }+ let(:exists_at_timestamp) { false }- let(:matches_query_filters_at_timestamps) { { timestamp.to_s => false } }+ let(:matches_filters_at_timestamp) { false }let(:expected_json) do{