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
baseline api: provide wrapper `Journable::WithAttributesAtTimestamps`
Sebastian Fiedlschuster6dcfea8cc82c
diff --git a/app/models/journable/with_attributes_at_timestamps.rb b/app/models/journable/with_attributes_at_timestamps.rbnew file mode 100644index 00000000000..fc2df642f1d--- /dev/null+++ b/app/models/journable/with_attributes_at_timestamps.rb@@ -0,0 +1,160 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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-2013 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 COPYRIGHT and LICENSE files for more details.+#++++# This class is used to wrap a Journable and provide access to its attributes at given timestamps.+# It is used to provide the old and new values of a journable in the journables's payload.+# https://github.com/opf/openproject/pull/11783+#+# Usage:+#+# # Wrap single work package+# timestamps = [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")]+# work_package = WorkPackage.find(1)+# work_package = Journable::WithAttributesAtTimestamps.wrap(work_package, timestamps:)+#+# # Wrap multiple work packages+# timestamps = query.timestamps+# work_packages = query.results.work_packages+# work_packages = Journable::WithAttributesAtTimestamps.wrap_multiple(work_packages, timestamps:)+#+# # Access historic attributes at timestamps after wrapping+# work_package = Journable::WithAttributesAtTimestamps.wrap(work_package, timestamps:)+# work_package.subject # => "Subject at PT0S (current time)"+# work_package.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject # => "Subject at 2022-01-01 (baseline time)"+#+# # Check at which timestamps the work package matches query filters after wrapping+# query.timestamps # => [<Timestamp 2022-01-01T00:00:00Z>, <Timestamp PT0S>]+# work_package = Journable::WithAttributesAtTimestamps.wrap(work_package, query:)+# work_package.matches_query_at_timestamps # => [<Timestamp 2022-01-01T00:00:00Z>]+#+# # Include only changed attributes in payload+# # i.e. only historic attributes that differ from the work_package's attributes+# timestamps = [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")]+# work_package = Journable::WithAttributesAtTimestamps.wrap(work_package, timestamps:, include_only_changed_attributes: true)+# work_package.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject # => "Subject at 2022-01-01 (baseline time)"+# work_package.attributes_at_timestamps["PT0S"].subject # => nil+#+# # Simplified interface for two timestamps+# query.timestamps # => [<Timestamp 2022-01-01T00:00:00Z>, <Timestamp PT0S>]+# work_package = Journable::WithAttributesAtTimestamps.wrap(work_package, query:)+# work_package.baseline_timestamp # => [<Timestamp 2022-01-01T00:00:00Z>]+# work_package.current_timestamp # => [<Timestamp PT0S>]+# work_package.matches_query_filter_at_baseline_timestamp?+# work_package.matches_query_filter_at_current_timestamp?+# work_package.baseline_attributes.subject # => "Subject at 2022-01-01 (baseline time)"+# work_package.subject # => "Subject at PT0S (current time)"+#+class Journable::WithAttributesAtTimestamps < SimpleDelegator+ attr_accessor :timestamps, :query, :include_only_changed_attributes+ attr_accessor :attributes_at_timestamps, :matches_query_at_timestamps++ def initialize(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)+ super(journable)++ if query and not journable.is_a? WorkPackage+ raise Journable::NotImplementedError, "Journable::WithAttributesAtTimestamps with query is only implemented for WorkPackages" \+ "at the moment because Query objects currently only support work packages."+ end++ self.query = query+ self.timestamps = timestamps || query.timestamps || []+ self.include_only_changed_attributes = include_only_changed_attributes++ self.attributes_at_timestamps = {}+ self.matches_query_at_timestamps = []+ end++ def self.wrap(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)+ journable = journable.at_timestamp(timestamps.last) if timestamps.last.try(:historic?)+ journable = new(journable, timestamps:, query:, include_only_changed_attributes:)+ timestamps.each do |timestamp|+ historic_journable = journable.at_timestamp(timestamp)+ matching_journable = query_work_packages(query:, timestamp:).find_by(id: journable.id) if query+ journable.assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)+ end+ journable+ end++ def self.wrap_multiple(journables, timestamps: nil, query: nil, include_only_changed_attributes: false)+ journables = journables.first.class.at_timestamp(timestamps.last).where(id: journables.map(&:id)) if timestamps.last.try(:historic?)+ journables = journables.map { |j| new(j, timestamps:, query:, include_only_changed_attributes:) }+ (timestamps || query.timestamps || []).each do |timestamp|+ historic_journables = WorkPackage.at_timestamp(timestamp).where(id: journables.map(&:id))+ matching_journables = query_work_packages(query:, timestamp:) if query+ journables.each do |journable|+ historic_journable = historic_journables.detect { |j| j.id == journable.id }+ matching_journable = matching_journables.detect { |j| j.id == journable.id } if query+ journable.assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)+ end+ end+ journables+ end++ def assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)+ attributes_at_timestamps[timestamp.to_s] = extract_historic_attributes_from(historic_journable:) if historic_journable+ matches_query_at_timestamps << timestamp if matching_journable+ end++ def extract_historic_attributes_from(historic_journable:)+ OpenStruct.new(+ historic_journable.attributes.select do |key, value|+ not include_only_changed_attributes \+ or not respond_to?(key) \+ or value != send(key)+ end+ )+ end++ def baseline_timestamp+ timestamps.first+ end++ def baseline_attributes+ attributes_at_timestamps[baseline_timestamp.to_s]+ end++ def matches_query_filter_at_baseline_timestamp?+ matches_query_at_timestamps.include?(baseline_timestamp)+ end++ def current_timestamp+ timestamps.last+ end++ def matches_query_filter_at_current_timestamp?+ matches_query_at_timestamps.include?(current_timestamp)+ end++ def self.query_work_packages(query:, timestamp: nil)+ query = query.dup+ query.timestamps = [timestamp] if timestamp+ query.results.work_packages+ end++ class NotImplemented < StandardError; end+enddiff --git a/app/models/timestamp.rb b/app/models/timestamp.rbindex 586f674d3dc..c0c6dccab06 100644--- a/app/models/timestamp.rb+++ b/app/models/timestamp.rb@@ -121,6 +121,10 @@ class Timestampself == otherend+ def historic?+ self != Timestamp.now+ end+class Exception < StandardError; end# rubocop:disable Metrics/AbcSizediff --git a/spec/models/journable/with_attributes_at_timestamps_spec.rb b/spec/models/journable/with_attributes_at_timestamps_spec.rbnew file mode 100644index 00000000000..459e877dc9d--- /dev/null+++ b/spec/models/journable/with_attributes_at_timestamps_spec.rb@@ -0,0 +1,312 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# 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-2013 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++describe Journable::WithAttributesAtTimestamps do+ let(:work_package) do+ new_work_package = create(:work_package, subject: "The current work package", project: project1)+ new_work_package.update_columns created_at: baseline_time - 1.day+ new_work_package+ end+ let(:original_journal) do+ create_journal(journable: work_package, timestamp: baseline_time - 1.day,+ version: 1,+ attributes: { subject: "The original work package" })+ end+ let(:current_journal) do+ create_journal(journable: work_package, timestamp: 1.day.ago,+ version: 2,+ attributes: { subject: "The current work package" })+ end+ let(:baseline_time) { "2022-01-01".to_time }+ let(:project1) { create(:project) }+ let(:user1) do+ create(:user,+ firstname: 'user',+ lastname: '1',+ member_in_project: project1,+ member_with_permissions: %i[view_work_packages view_file_links])+ end++ def create_journal(journable:, version:, timestamp:, attributes: {})+ work_package_attributes = work_package.attributes.except("id")+ journal_attributes = work_package_attributes \+ .extract!(*Journal::WorkPackageJournal.attribute_names) \+ .symbolize_keys.merge(attributes)+ create(:work_package_journal, version:,+ journable:, created_at: timestamp, updated_at: timestamp,+ data: build(:journal_work_package_journal, journal_attributes))+ end++ before do+ WorkPackage.destroy_all+ work_package+ Journal.destroy_all+ original_journal+ current_journal+ end++ describe ".wrap" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }+ let(:query) { nil }+ let(:include_only_changed_attributes) { nil }+ subject { described_class.wrap(work_package, timestamps:, query:, include_only_changed_attributes:) }++ it "returns a Journable::WithAttributesAtTimestamps instance" do+ is_expected.to be_a described_class+ end++ it "provides access to the work-package attributes" do+ expect(subject.subject).to eq "The current work package"+ end++ it "provides access to the work-package attributes at timestamps" do+ expect(subject.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"+ expect(subject.attributes_at_timestamps["PT0S"].subject).to eq "The current work package"+ end++ it "determines whether the journable attributes are historic" do+ expect(subject.historic?).to eq false+ end++ describe "when providing a query" do+ let(:query) do+ login_as(user1)+ build(:query, user: nil, project: nil).tap do |query|+ query.filters.clear+ query.add_filter 'subject', '~', search_term+ end+ end+ let(:search_term) { "original" }++ it "determines for each timestamp whether the journable matches the query at that timestamp" do+ expect(subject.matches_query_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")+ expect(subject.matches_query_at_timestamps).not_to include Timestamp.parse("PT0S")+ end+ end++ describe "with include_only_changed_attributes: true" do+ let(:include_only_changed_attributes) { true }++ it "provides access to the work-package attributes at timestamps where the attribute is different from the work package's attribute" do+ expect(subject.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"+ end++ specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do+ expect(subject.attributes_at_timestamps["PT0S"].subject).to eq nil+ end+ end++ describe "when requesting only historic data" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }++ it "provides access to the historic work-package attributes" do+ expect(subject.subject).to eq "The original work package"+ end++ it "provides access to the historic work-package attributes at timestamps" do+ expect(subject.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"+ end++ it "determines whether the journable attributes are historic" do+ expect(subject.historic?).to eq true+ end+ end++ describe "when the work package did not exist yet at the basline date" do+ let(:timestamps) { [Timestamp.parse("2021-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }++ it "provides access to the work-package attributes" do+ expect(subject.subject).to eq "The current work package"+ end++ it "has not attributes at the baseline date" do+ expect(subject.attributes_at_timestamps["2021-01-01T00:00:00Z"]).to be_nil+ end+ end+ end++ describe ".wrap_multiple" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }+ let(:query) { nil }+ let(:include_only_changed_attributes) { nil }+ subject { described_class.wrap_multiple(work_packages, timestamps:, query:, include_only_changed_attributes:) }++ context "with a single work package" do+ let(:work_packages) { [work_package] }++ it "returns an array of Journable::WithAttributesAtTimestamps instances" do+ is_expected.to all be_a described_class+ end++ it "provides access to the work-package attributes" do+ expect(subject.first.subject).to eq "The current work package"+ end++ it "provides access to the work-package attributes at timestamps" do+ expect(subject.first.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"+ expect(subject.first.attributes_at_timestamps["PT0S"].subject).to eq "The current work package"+ end++ describe "when providing a query" do+ let(:query) do+ login_as(user1)+ build(:query, user: nil, project: nil).tap do |query|+ query.filters.clear+ query.add_filter 'subject', '~', search_term+ end+ end+ let(:search_term) { "original" }++ it "determines for each timestamp whether the journables matches the query at that timestamp" do+ expect(subject.first.matches_query_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")+ expect(subject.first.matches_query_at_timestamps).not_to include Timestamp.parse("PT0S")+ end+ end++ describe "with include_only_changed_attributes: true" do+ let(:include_only_changed_attributes) { true }++ it "provides access to the work-package attributes at timestamps where the attribute is different from the work package's attribute" do+ expect(subject.first.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"+ end++ specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do+ expect(subject.first.attributes_at_timestamps["PT0S"].subject).to eq nil+ end+ end++ describe "when requesting only historic data" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }++ it "provides access to the historic work-package attributes" do+ expect(subject.first.subject).to eq "The original work package"+ end++ it "provides access to the historic work-package attributes at timestamps" do+ expect(subject.first.attributes_at_timestamps["2022-01-01T00:00:00Z"].subject).to eq "The original work package"+ end++ it "determines whether the journable attributes are historic" do+ expect(subject.first.historic?).to eq true+ end+ end+ end+ end++ describe "#baseline_timestamp" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }+ let(:journable) { described_class.wrap(work_package, timestamps:) }+ subject { journable.baseline_timestamp }++ it "provides simplified access to the baseline timestamp, which is the first given timestamp" do+ expect(subject).to eq Timestamp.parse("2022-01-01T00:00:00Z")+ end+ end++ describe "#baseline_attributes" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }+ let(:journable) { described_class.wrap(work_package, timestamps:) }+ subject { journable.baseline_attributes }++ it "provides access to the work-package attributes at the baseline timestamp" do+ expect(subject.subject).to eq "The original work package"+ end+ end++ describe "#matches_query_filter_at_baseline_timestamp?" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }+ let(:journable) { described_class.wrap(work_package, timestamps: timestamps, query: query) }+ let(:query) do+ login_as(user1)+ build(:query, user: nil, project: nil).tap do |query|+ query.filters.clear+ query.add_filter 'subject', '~', search_term+ end+ end++ subject { journable.matches_query_filter_at_baseline_timestamp? }++ describe "providing a filter that matches at the baseline timestamp" do+ let(:search_term) { "original" }++ it "determines whether the journable matches the query at the baseline timestamp" do+ expect(subject).to eq true+ end+ end++ describe "providing a filter that matches at the current timestamp" do+ let(:search_term) { "current" }++ it "determines whether the journable matches the query at the baseline timestamp" do+ expect(subject).to eq false+ end+ end+ end++ describe "#current_timestamp" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }+ let(:journable) { described_class.wrap(work_package, timestamps:) }+ subject { journable.current_timestamp }++ it "provides simplified access to the current timestamp, which is the last given timestamp" do+ expect(subject).to eq Timestamp.parse("PT0S")+ end+ end++ describe "#matches_query_filter_at_current_timestamp?" do+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }+ let(:journable) { described_class.wrap(work_package, timestamps: timestamps, query: query) }+ let(:query) do+ login_as(user1)+ build(:query, user: nil, project: nil).tap do |query|+ query.filters.clear+ query.add_filter 'subject', '~', search_term+ end+ end++ subject { journable.matches_query_filter_at_current_timestamp? }++ describe "providing a filter that matches at the baseline timestamp" do+ let(:search_term) { "original" }++ it "determines whether the journable matches the query at the current timestamp" do+ expect(subject).to eq false+ end+ end++ describe "providing a filter that matches at the current timestamp" do+ let(:search_term) { "current" }++ it "determines whether the journable matches the query at the current timestamp" do+ expect(subject).to eq true+ end+ end+ end+end\ No newline at end of file