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
`Query::Results`: search work packages at all given timestamps
Sebastian Fiedlschuster2f51f91f23f7
diff --git a/app/models/journal/timestamps.rb b/app/models/journal/timestamps.rbindex 46cd0ac67a3..eb75702ddd6 100644--- a/app/models/journal/timestamps.rb+++ b/app/models/journal/timestamps.rb@@ -50,7 +50,8 @@ module Journal::Timestamps# a journable at a given timestamp.#def at_timestamp(timestamp)- raise ArgumentError, "Expected timestamp to be an ActiveSupport::TimeWithZone or DateTime" unless timestamp.kind_of? ActiveSupport::TimeWithZone or timestamp.kind_of? DateTime+ raise ArgumentError, "Expected timestamp to be a Timestamp, an ActiveSupport::TimeWithZone, or a DateTime" unless timestamp.kind_of? Timestamp or timestamp.kind_of? ActiveSupport::TimeWithZone or timestamp.kind_of? DateTime+ timestamp = timestamp.to_time if timestamp.kind_of? Timestamptimestamp = timestamp.in_time_zone if timestamp.kind_of? DateTimewhere(id:diff --git a/app/models/query/results.rb b/app/models/query/results.rbindex b8aea4ed8da..36846bddf48 100644--- a/app/models/query/results.rb+++ b/app/models/query/results.rb@@ -39,27 +39,24 @@ class ::Query::Results# Returns the work package countdef work_package_count- work_package_scope- .joins(all_filter_joins)- .includes(:project)- .where(query.statement)- .references(:projects)- .count- rescue ::ActiveRecord::StatementInvalid => e- raise ::Query::StatementInvalid.new(e.message)+ work_packages.countend# Returns the work packages adhering to the filters and ordered by the provided criteria (grouping and sorting)def work_packages- work_package_scope- .where(query.statement)- .includes(all_includes)- .joins(all_joins)+ WorkPackage+ .where(id: work_package_ids).order(order_option).references(:projects).order(sort_criteria_array)end+ def work_package_ids+ # For filtering on historic data, we need to collect the work package ids+ # matching the filters for all search timestamps provided in the query.+ work_package_ids_for_all_timestamps+ end+def order_optionorder_option = [group_by_sort].compact_blank.join(', ')@@ -72,6 +69,30 @@ class ::Query::Resultsprivate+ # For filtering on historic data, this returns the work package ids+ # matching the filters for all search timestamps provided in the query.+ #+ def work_package_ids_for_all_timestamps+ query.timestamps.collect do |timestamp|+ work_packages_relation.at_timestamp(timestamp).pluck(:id)+ end.flatten.uniq+ end++ # Returns an active-record relation that applies the filters to find the matching+ # work packages.+ #+ # This can be chained with `.at_timestamp(...)` in order to search historic data+ # as required for the baseline-comparison feature.+ #+ # https://community.openproject.org/projects/openproject/work_packages/26448+ #+ def work_packages_relation+ work_package_scope+ .where(query.statement)+ .includes(all_includes)+ .joins(all_joins)+ end+def work_package_scopeWorkPackage.visiblediff --git a/spec/models/query/results_filter_on_historic_data_spec.rb b/spec/models/query/results_filter_on_historic_data_spec.rbnew file mode 100644index 00000000000..1beddef5c79--- /dev/null+++ b/spec/models/query/results_filter_on_historic_data_spec.rb@@ -0,0 +1,152 @@+#-- 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 ::Query::Results, 'Filter on historic data', type: :model, with_mail: false do+ let(:historic_time) { "2022-08-01".to_datetime }+ let(:pre_historic_time) { historic_time - 1.day }+ let(:recent_time) { 1.hour.ago }+ let!(:work_package) do+ new_work_package = create :work_package, description: "This is the original description of the work package", project: project_1+ new_work_package.update_columns created_at: historic_time+ new_work_package.journals.first.update_columns created_at: historic_time, updated_at: historic_time+ new_work_package.reload+ new_work_package.update description: "This is the current description of the work package", updated_at: recent_time+ new_work_package.journals.last.update_columns created_at: recent_time, updated_at: recent_time+ new_work_package.reload+ new_work_package+ end++ let(:project_1) { create :project }+ let(:user_1) do+ create(:user,+ firstname: 'user',+ lastname: '1',+ member_in_project: project_1,+ member_with_permissions: [:view_work_packages])+ end+++ describe "[prelims]" do+ specify "the work package has a journal entry with the historic description" do+ expect(work_package.journals.count).to eq 2+ expect(work_package.journals.first.data.description).to eq "This is the original description of the work package"+ end++ specify "the work package has its current description" do+ expect(work_package.description).to eq "This is the current description of the work package"+ end+ end++ describe "#work_packages" do+ let(:query) do+ login_as(user_1)+ build(:query, user: user_1, project: nil).tap do |query|+ query.filters.clear+ query.add_filter 'description', '~', search_term+ end+ end+ let(:results) { query.results }+ subject { results.work_packages }++ describe "filter for description containing 'current'" do+ let(:search_term) { 'current' }+ it "includes the work package matching today" do+ expect(subject).to include work_package+ end+ end++ describe "filter for description containing 'original'" do+ let(:search_term) { 'original' }+ it "does not include the work package matching only in the past" do+ expect(subject).not_to include work_package+ end+ end++ describe "when searching current and historic work packages" do+ before { query.timestamps = [historic_time, Time.zone.now] }++ describe "filter for description containing 'current'" do+ let(:search_term) { 'current' }+ it "includes the work package matching today" do+ expect(subject).to include work_package+ end+ end++ describe "filter for description containing 'original'" do+ let(:search_term) { 'original' }+ it "includes the work package matching in the past" do+ expect(subject).to include work_package+ end+ end+ end++ describe "when searching only historic work packages" do+ before { query.timestamps = [historic_time] }++ describe "filter for description containing 'current'" do+ let(:search_term) { 'current' }+ it "does not include the work package matching only in the past" do+ expect(subject).not_to include work_package+ end+ end++ describe "filter for description containing 'original'" do+ let(:search_term) { 'original' }++ it "includes the work package matching in the past" do+ expect(subject).to include work_package+ end++ it "returns the work packages in their historic state" do+ expect(subject.first.description).to eq "This is the original description of the work package"+ end+ end+ end++ describe "when searching only pre-historic work packages (i.e. when the work package does not exist yet)" do+ before { query.timestamps = [pre_historic_time] }++ describe "filter for description containing 'current'" do+ let(:search_term) { 'current' }+ it "does not include the work package matching only in the past" do+ expect(subject).not_to include work_package+ end+ end++ describe "filter for description containing 'original'" do+ let(:search_term) { 'original' }+ it "does not include the work package because it does not exist yet at that time" do+ expect(subject).not_to include work_package+ end+ end+ end++ end+end