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

baseline api: track existence as meta attribute

Sebastian Fiedlschusterf7b57341f3bd
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index c4c471c9ad3..bf76669e692 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -71,7 +71,7 @@
#
class Journable::WithHistoricAttributes < SimpleDelegator
attr_accessor :timestamps, :query, :include_only_changed_attributes, :attributes_by_timestamp,
- :matches_query_filters_at_timestamps
+ :matches_query_filters_at_timestamps, :exists_at_timestamps
def initialize(journable, timestamps: nil, query: nil, include_only_changed_attributes: false)
super(journable)
@@ -88,6 +88,7 @@ class Journable::WithHistoricAttributes < SimpleDelegator
self.attributes_by_timestamp = {}
self.matches_query_filters_at_timestamps = []
+ self.exists_at_timestamps = []
end
def self.wrap(journable_or_journables, timestamps: nil, query: nil, include_only_changed_attributes: false)
@@ -132,6 +133,7 @@ class Journable::WithHistoricAttributes < SimpleDelegator
def assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)
attributes_by_timestamp[timestamp.to_s] = extract_historic_attributes_from(historic_journable:) if historic_journable
matches_query_filters_at_timestamps << timestamp if matching_journable
+ exists_at_timestamps << timestamp if historic_journable
end
def self.assign_historic_attributes_to(journables, timestamp:, historic_journables:, matching_journables:, query:)
diff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
index e7014adf9aa..2878bbe8f83 100644
--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
@@ -36,10 +36,12 @@ module API::V3::WorkPackages::EagerLoading
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
end
def self.module
@@ -61,9 +63,10 @@ module API::V3::WorkPackages::EagerLoading
extend ActiveSupport::Concern
included do
- attr_accessor :baseline_attributes, :attributes_by_timestamp, :baseline_timestamp,
+ attr_accessor :baseline_attributes, :attributes_by_timestamp, :timestamps, :baseline_timestamp,
:matches_query_filters_at_baseline_timestamp,
- :matches_query_filters_at_timestamps
+ :matches_query_filters_at_timestamps,
+ :exists_at_timestamps
end
# Does the work package match the query filter at the baseline timestamp?
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 7ddfb05684f..05a42d6cae2 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -468,21 +468,27 @@ module API
property :baseline_attributes,
as: :baselineAttributes,
- if: ->(*) { respond_to?(:baseline_attributes) and baseline_attributes.present? },
+ if: ->(*) { respond_to?(:baseline_attributes) and baseline_timestamp.present? },
getter: ->(*) do
- baseline_attributes.to_h.merge(
- {
- '_links': {
- 'self': {
- 'href': API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: baseline_timestamp)
- }
- },
- '_meta': {
- 'timestamp': baseline_timestamp.to_s,
- 'matchesFilters': matches_query_filters_at_baseline_timestamp?
- }.compact
- }
- )
+ attrs = baseline_attributes.to_h
+ if exists_at_timestamps.include?(baseline_timestamp)
+ attrs = attrs.merge({
+ '_links': {
+ 'self': {
+ 'href': API::V3::Utilities::PathHelper::ApiV3Path \
+ .work_package(id, timestamps: baseline_timestamp)
+ }
+ }
+ })
+ end
+ attrs = attrs.merge({
+ '_meta': {
+ 'timestamp': baseline_timestamp.to_s,
+ 'matchesFilters': matches_query_filters_at_baseline_timestamp?,
+ 'exists': exists_at_timestamps.include?(baseline_timestamp)
+ }.compact
+ })
+ attrs
end,
embedded: true,
uncachable: true
@@ -491,19 +497,25 @@ module API
as: :attributesByTimestamp,
if: ->(*) { respond_to?(:attributes_by_timestamp) and attributes_by_timestamp.present? },
getter: ->(*) do
- attributes_by_timestamp.to_a.to_h do |timestamp, attributes|
- [timestamp, attributes.to_h.merge(
- {
- '_links': {
- 'self': {
- 'href': API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: timestamp)
- }
- },
- '_meta': {
- 'matchesFilters': matches_query_filters_at_timestamp?(timestamp)
- }.compact
- }
- )]
+ timestamps.to_h do |timestamp|
+ attrs = attributes_by_timestamp[timestamp.to_s].to_h
+ if exists_at_timestamps.include?(timestamp)
+ attrs = attrs.merge({
+ '_links': {
+ 'self': {
+ 'href': API::V3::Utilities::PathHelper::ApiV3Path \
+ .work_package(id, timestamps: timestamp)
+ }
+ }
+ })
+ end
+ attrs = attrs.merge({
+ '_meta': {
+ 'matchesFilters': matches_query_filters_at_timestamp?(timestamp),
+ 'exists': exists_at_timestamps.include?(timestamp)
+ }.compact
+ })
+ [timestamp, attrs]
end
end,
embedded: true,
diff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rb
index d4b6d4f63ee..23809d40cd3 100644
--- a/spec/models/journable/with_historic_attributes_spec.rb
+++ b/spec/models/journable/with_historic_attributes_spec.rb
@@ -31,11 +31,11 @@ require 'spec_helper'
describe Journable::WithHistoricAttributes 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.update_columns(created_at:)
new_work_package
end
let(:original_journal) do
- create_journal(journable: work_package, timestamp: baseline_time - 1.day,
+ create_journal(journable: work_package, timestamp: created_at,
version: 1,
attributes: { subject: "The original work package" })
end
@@ -45,6 +45,7 @@ describe Journable::WithHistoricAttributes do
attributes: { subject: "The current work package" })
end
let(:baseline_time) { "2022-01-01".to_time }
+ let(:created_at) { baseline_time - 1.day }
let(:project1) { create(:project) }
let(:user1) do
create(:user,
@@ -92,6 +93,11 @@ describe Journable::WithHistoricAttributes do
expect(subject.attributes_by_timestamp["PT0S"].subject).to eq "The current work package"
end
+ it "determines for each timestamp whether the journable exists at that timestamp" do
+ expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")
+ end
+
it "determines whether the journable attributes are historic" do
expect(subject.historic?).to be false
end
@@ -110,6 +116,21 @@ describe Journable::WithHistoricAttributes do
expect(subject.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
expect(subject.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
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")] }
+ let(:search_term) { "current" }
+
+ it "does not include the timestamp in the matches_query_filters_at_timestamps array" do
+ expect(subject.matches_query_filters_at_timestamps).not_to include Timestamp.parse("2021-01-01T00:00:00Z")
+ expect(subject.matches_query_filters_at_timestamps).to include Timestamp.parse("PT0S")
+ end
+
+ it "does not include the timestamp in the exists_at_timestamps array" do
+ expect(subject.exists_at_timestamps).not_to include Timestamp.parse("2021-01-01T00:00:00Z")
+ expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")
+ end
+ end
end
describe "with include_only_changed_attributes: true" do
@@ -123,6 +144,11 @@ describe Journable::WithHistoricAttributes do
specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do
expect(subject.attributes_by_timestamp["PT0S"].subject).to be_nil
end
+
+ it "includes the timestamps in the exists_at_timestamps array" do
+ expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")
+ end
end
describe "when requesting only historic data" do
@@ -139,6 +165,10 @@ describe Journable::WithHistoricAttributes do
it "determines whether the journable attributes are historic" do
expect(subject.historic?).to be true
end
+
+ it "includes the timestamp in the exists_at_timestamps array" do
+ expect(subject.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ end
end
describe "when the work package did not exist yet at the basline date" do
@@ -148,9 +178,18 @@ describe Journable::WithHistoricAttributes do
expect(subject.subject).to eq "The current work package"
end
- it "has not attributes at the baseline date" do
+ it "has no attributes at the baseline date" do
expect(subject.attributes_by_timestamp["2021-01-01T00:00:00Z"]).to be_nil
end
+
+ it "has no baseline attributes" do
+ expect(subject.baseline_attributes).to be_nil
+ end
+
+ it "does not include the timestamp in the exists_at_timestamps array" do
+ expect(subject.exists_at_timestamps).not_to include Timestamp.parse("2021-01-01T00:00:00Z")
+ expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")
+ end
end
end
diff --git a/spec/requests/api/v3/work_packages/index_resource_spec.rb b/spec/requests/api/v3/work_packages/index_resource_spec.rb
index e1146a5f726..60fbd842753 100644
--- a/spec/requests/api/v3/work_packages/index_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/index_resource_spec.rb
@@ -245,14 +245,15 @@ describe 'API v3 Work package resource',
let(:path) { "#{api_v3_paths.work_packages}?timestamps=#{timestamps.join(',')}" }
let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z'), Timestamp.now] }
let(:baseline_time) { timestamps.first.to_time }
+ let(:created_at) { baseline_time - 1.day }
let(:work_package) do
new_work_package = create(:work_package, subject: "The current work package", project:)
- new_work_package.update_columns created_at: baseline_time - 1.day
+ new_work_package.update_columns(created_at:)
new_work_package
end
let(:original_journal) do
- create_journal(journable: work_package, timestamp: baseline_time - 1.day,
+ create_journal(journable: work_package, timestamp: created_at,
version: 1,
attributes: { subject: "The original work package" })
end
@@ -411,6 +412,67 @@ describe 'API v3 Work package resource',
end
end
end
+
+ describe "when the work package has not been present at the baseline time" do
+ let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z'), Timestamp.now] }
+ let(:created_at) { 10.days.ago }
+
+ describe "baselineAttributes" do
+ describe "_meta" do
+ describe "exists" do
+ it 'marks the work package as not existing at the baseline time' do
+ expect(subject.body)
+ .to be_json_eql(false.to_json)
+ .at_path('_embedded/elements/0/_embedded/baselineAttributes/_meta/exists')
+ end
+ end
+
+ describe "matchesFilters" do
+ it 'marks the work package as not matching the filters at the baseline time' do
+ expect(subject.body)
+ .to be_json_eql(false.to_json)
+ .at_path('_embedded/elements/0/_embedded/baselineAttributes/_meta/matchesFilters')
+ end
+ end
+ end
+
+ describe "_links" do
+ it 'is not present' do
+ expect(subject.body)
+ .not_to have_json_path('_embedded/elements/0/_embedded/baselineAttributes/_links')
+ end
+ end
+ end
+
+ describe "attributesByTimestamp" do
+ describe "baseline time" do
+ describe "_meta" do
+ describe "exists" do
+ it 'marks the work package as not existing at the baseline time' do
+ expect(subject.body)
+ .to be_json_eql(false.to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/_meta/exists")
+ end
+ end
+
+ describe "matchesFilters" do
+ it 'marks the work package as not matching the filters at the baseline time' do
+ expect(subject.body)
+ .to be_json_eql(false.to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/_meta/matchesFilters")
+ end
+ end
+ end
+
+ describe "_links" do
+ it 'is not present' do
+ expect(subject.body)
+ .not_to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/_links")
+ end
+ end
+ end
+ end
+ end
end
end
end