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

[54299] Total values are unset for work packages without any children

Christophe Bliard734a276c92a7
diff --git a/app/services/work_packages/update_ancestors/loader.rb b/app/services/work_packages/update_ancestors/loader.rb
index 908d43b19b5..43530105023 100644
--- a/app/services/work_packages/update_ancestors/loader.rb
+++ b/app/services/work_packages/update_ancestors/loader.rb
@@ -46,17 +46,6 @@ class WorkPackages::UpdateAncestors::Loader
@descendants[queried_work_package]
end
- def leaves_of(queried_work_package)
- @leaves ||= Hash.new do |hash, wp|
- hash[wp] = replaced_related_of(wp, :leaves) do |leaf|
- # Mimic work package by implementing the closed? interface
- leaf.send(:"closed?=", leaf.is_closed)
- end
- end
-
- @leaves[queried_work_package]
- end
-
def children_of(queried_work_package)
@children ||= Hash.new do |hash, wp|
hash[wp] = descendants_of(wp).select { |d| d.parent_id == wp.id }
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rb
index 0e058e285e5..7627840b488 100644
--- a/app/services/work_packages/update_ancestors_service.rb
+++ b/app/services/work_packages/update_ancestors_service.rb
@@ -119,16 +119,14 @@ class WorkPackages::UpdateAncestorsService
ancestor.derived_done_ratio = compute_derived_done_ratio(ancestor, loader)
end
- def compute_derived_done_ratio(work_package, _loader)
+ def compute_derived_done_ratio(work_package, loader)
return if work_package.derived_estimated_hours.nil? || work_package.derived_remaining_hours.nil?
+ return if work_package.derived_estimated_hours.zero?
+ return if no_children?(work_package, loader)
- if work_package.derived_estimated_hours.zero?
- nil
- else
- work_done = (work_package.derived_estimated_hours - work_package.derived_remaining_hours)
- progress = (work_done.to_f / work_package.derived_estimated_hours) * 100
- progress.round
- end
+ work_done = (work_package.derived_estimated_hours - work_package.derived_remaining_hours)
+ progress = (work_done.to_f / work_package.derived_estimated_hours) * 100
+ progress.round
end
# Sets the ignore_non_working_days to true if any descendant has its value set to true.
@@ -149,22 +147,22 @@ class WorkPackages::UpdateAncestorsService
end
def derive_total_estimated_and_remaining_hours(work_package, loader)
- descendants = loader.descendants_of(work_package)
-
- work_package.derived_estimated_hours = total(all_estimated_hours([work_package] + descendants))
- work_package.derived_remaining_hours = total(all_remaining_hours([work_package] + descendants))
+ work_package.derived_estimated_hours = derive_total(work_package, :estimated_hours, loader)
+ work_package.derived_remaining_hours = derive_total(work_package, :remaining_hours, loader)
end
- def total(hours)
- hours.empty? ? nil : hours.sum.to_f
- end
+ def derive_total(work_package, attribute, loader)
+ return if no_children?(work_package, loader)
+
+ work_packages = [work_package] + loader.descendants_of(work_package)
+ values = work_packages.filter_map(&attribute)
+ return if values.empty?
- def all_estimated_hours(work_packages)
- work_packages.filter_map(&:estimated_hours)
+ values.sum.to_f
end
- def all_remaining_hours(work_packages)
- work_packages.filter_map(&:remaining_hours)
+ def no_children?(work_package, loader)
+ loader.descendants_of(work_package).none?
end
def modified_attributes_justify_derivation?(attributes)
diff --git a/app/workers/work_packages/update_progress_job.rb b/app/workers/work_packages/update_progress_job.rb
index f072f543869..a287b0bb505 100644
--- a/app/workers/work_packages/update_progress_job.rb
+++ b/app/workers/work_packages/update_progress_job.rb
@@ -192,6 +192,8 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
SQL
end
+ # Computes total work, total remaining work and total % complete for all work
+ # packages having children.
def update_totals
execute(<<~SQL)
UPDATE temp_wp_progress_values
@@ -203,6 +205,7 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
END
FROM (
SELECT wp_tree.ancestor_id AS id,
+ MAX(generations) AS generations,
SUM(estimated_hours) AS total_work,
SUM(remaining_hours) AS total_remaining_work
FROM work_package_hierarchies wp_tree
@@ -210,6 +213,7 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
GROUP BY wp_tree.ancestor_id
) totals
WHERE temp_wp_progress_values.id = totals.id
+ AND totals.generations > 0
SQL
end
diff --git a/spec/migrations/update_progress_calculation_spec.rb b/spec/migrations/update_progress_calculation_spec.rb
index 8ce33af5d5b..104330cccc0 100644
--- a/spec/migrations/update_progress_calculation_spec.rb
+++ b/spec/migrations/update_progress_calculation_spec.rb
@@ -68,10 +68,11 @@ RSpec.describe UpdateProgressCalculation, type: :model do
context "when a work package progress values are not changed" do
let_work_packages(<<~TABLE)
- subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
wp all unset | | | | | |
wp only pc set | | | 60% | | |
- wp all set consistent | 10h | 4h | 60% | 10h | 4h | 60%
+ wp parent consistent | 10h | 4h | 60% | 20h | 8h | 60%
+ wp all set consistent | 10h | 4h | 60% | | |
TABLE
before do
@@ -81,13 +82,14 @@ RSpec.describe UpdateProgressCalculation, type: :model do
it "does create a journal entry only for the work package with a total % complete" do
expect(wp_all_unset.journals.count).to eq(1)
expect(wp_only_pc_set.journals.count).to eq(1)
+ expect(wp_all_set_consistent.journals.count).to eq(1)
# This one will receive a journal entry since we treat the total % complete field
# as if it was introduced by the migration (OP 14.0) even though the field existed before.
# But the calculation was off and we did not present the activity on the field anyway.
- expect(wp_all_set_consistent.journals.count).to eq(2)
+ expect(wp_parent_consistent.journals.count).to eq(2)
- expect(wp_all_set_consistent.last_journal.get_changes)
+ expect(wp_parent_consistent.last_journal.get_changes)
.to include("derived_done_ratio" => [nil, 60],
"cause" => [nil, { "feature" => "progress_calculation_changed", "type" => "system_update" }])
end
@@ -657,8 +659,8 @@ RSpec.describe UpdateProgressCalculation, type: :model do
to: <<~TABLE
hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
parent | To do (0%) | | | 0% | 20h | 0h | 100%
- child1 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%
- child2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%
+ child1 | Done (100%) | 10h | 0h | 100% | | |
+ child2 | Done (100%) | 10h | 0h | 100% | | |
TABLE
)
end
@@ -687,14 +689,14 @@ RSpec.describe UpdateProgressCalculation, type: :model do
subject | work | remaining work | ∑ work | ∑ remaining work | ∑ % complete
grandparent | 10h | 1h | 100h | 55h | 45%
parent 1 | 10h | 2h | 40h | 14h | 65%
- child 11 | 10h | 3h | 10h | 3h | 70%
- child 12 | 10h | 4h | 10h | 4h | 60%
- child 13 | 10h | 5h | 10h | 5h | 50%
+ child 11 | 10h | 3h | | |
+ child 12 | 10h | 4h | | |
+ child 13 | 10h | 5h | | |
parent 2 | 10h | 6h | 50h | 40h | 20%
- child 21 | 10h | 7h | 10h | 7h | 30%
- child 22 | 10h | 8h | 10h | 8h | 20%
- child 23 | 10h | 9h | 10h | 9h | 10%
- child 24 | 10h | 10h | 10h | 10h | 0%
+ child 21 | 10h | 7h | | |
+ child 22 | 10h | 8h | | |
+ child 23 | 10h | 9h | | |
+ child 24 | 10h | 10h | | |
TABLE
)
end
@@ -712,7 +714,7 @@ RSpec.describe UpdateProgressCalculation, type: :model do
to: <<~TABLE
subject | work | remaining work | ∑ work | ∑ remaining work | ∑ % complete
parent | 0h | 0h | 0h | 0h |
- child | 0h | 0h | 0h | 0h |
+ child | 0h | 0h | | |
TABLE
)
end
@@ -731,8 +733,8 @@ RSpec.describe UpdateProgressCalculation, type: :model do
to: <<~TABLE
hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
parent | | | | 20h | 0h | 100%
- child1 | 10h | 0h | 100% | 10h | 0h | 100%
- child2 | 10h | 0h | 100% | 10h | 0h | 100%
+ child1 | 10h | 0h | 100% | | |
+ child2 | 10h | 0h | 100% | | |
TABLE
)
end
@@ -759,53 +761,14 @@ RSpec.describe UpdateProgressCalculation, type: :model do
end
end
- context "when work is set to 0h and remaining work is unset" do
- it "sets remaining work, total work, and total remaining work to 0h, unsets % complete, and keeps total % complete unset" do
- expect_migrates(
- from: <<~TABLE,
- subject | work | remaining work | % complete
- wp all unset | 0h | |
- wp 0% | 0h | | 0%
- wp 30% | 0h | | 30%
- wp 100% | 0h | | 100%
- TABLE
- to: <<~TABLE
- subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
- wp all unset | 0h | 0h | | 0h | 0h |
- wp 0% | 0h | 0h | | 0h | 0h |
- wp 30% | 0h | 0h | | 0h | 0h |
- wp 100% | 0h | 0h | | 0h | 0h |
- TABLE
- )
- end
- end
-
- context "when work and remaining work are set and not 0h" do
- it "sets total work, total remaining work, and total % complete accordingly same as work, remaining work and % complete" do
- expect_migrates(
- from: <<~TABLE,
- subject | work | remaining work |
- wp w set | 10h | |
- wp rw set | | 5h |
- wp w rw set | 10h | 5h |
- TABLE
- to: <<~TABLE
- subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
- wp w set | 10h | 10h | 0% | 10h | 10h | 0%
- wp rw set | 5h | 5h | 0% | 5h | 5h | 0%
- wp w rw set | 10h | 5h | 50% | 10h | 5h | 50%
- TABLE
- )
- end
- end
-
context "when ∑ % complete has had some values (including wrong ones)" do
let_work_packages(<<~TABLE)
- hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete |
- wp zero | | | 0 | | | 0 |
- wp correct | 100h | 50h | 50 | 100h | 50h | 50 |
- wp wrong | | | 90 | 100h | 50h | 90 |
- wp wrong child | 100h | 50h | 20 | 10h | 10h | 20 |
+ hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete |
+ wp zero | | | 0 | | | 0 |
+ wp correct | 100h | 50h | 50 | 100h | 50h | 50 |
+ wp correct child | | | | | | |
+ wp wrong | | | 90 | 100h | 50h | 90 |
+ wp wrong child | 100h | 50h | 20 | 10h | 10h | 20 |
TABLE
before do
@@ -813,20 +776,28 @@ RSpec.describe UpdateProgressCalculation, type: :model do
end
it "fixes the total values and sets ∑ % complete to nil (not 0) but keeps % complete (unless wrong)" do
- expect_work_packages(WorkPackage.all, <<~TABLE)
- subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete |
- wp zero | | | 0 | | | |
- wp correct | 100h | 50h | 50 | 100h | 50h | 50 |
- wp wrong | | | 90 | 100h | 50h | 50 |
- wp wrong child | 100h | 50h | 50 | 100h | 50h | 50 |
+ expect_work_packages(table_work_packages.map(&:reload), <<~TABLE)
+ subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete |
+ wp zero | | | 0 | | | |
+ wp correct | 100h | 50h | 50 | 100h | 50h | 50 |
+ wp correct child | | | | | | |
+ wp wrong | | | 90 | 100h | 50h | 50 |
+ wp wrong child | 100h | 50h | 50 | | | |
TABLE
end
- it "creates no journal for the work package transitioning from 0 to nil and reworks the pre migration journals" do
- expect(wp_zero.journals.count).to eq(1)
+ it "reworks the pre migration journals to unset ∑ % complete" do
+ pre_journals = table_work_packages
+ .map(&:reload)
+ .flat_map { |wp| wp.journals[...-1] } # remove the last journal
- expect(wp_zero.journals.first.get_changes.keys)
- .not_to include("derived_done_ratio")
+ pre_journals.each do |journal|
+ expect(journal.get_changes.keys).not_to include("derived_done_ratio")
+ end
+ end
+
+ it "creates no journals for work packages transitioning only ∑ % complete from 0 to nil" do
+ expect(wp_zero.journals.count).to eq(1)
end
it "creates a journal for the correct work package as the old ∑ % complete value has been set to null during the job" do
@@ -839,7 +810,7 @@ RSpec.describe UpdateProgressCalculation, type: :model do
.to eql [nil, 50]
end
- it "creates a journal for the work package transitioning from 90 to 50 and reworks the pre migration journals" do
+ it "creates a journal for the work package transitioning ∑ % complete from 90 to 50" do
expect(wp_wrong.journals.count).to eq(2)
expect(wp_wrong.journals.first.get_changes.keys)
@@ -848,24 +819,14 @@ RSpec.describe UpdateProgressCalculation, type: :model do
expect(wp_wrong.journals.last.get_changes["derived_done_ratio"])
.to eql [nil, 50]
end
-
- it "creates a journal for the work package transitioning from 20 to 50 and reworks the pre migration journals" do
- expect(wp_wrong_child.journals.count).to eq(2)
-
- expect(wp_wrong_child.journals.first.get_changes.keys)
- .not_to include("derived_done_ratio")
-
- expect(wp_wrong_child.journals.last.get_changes["derived_done_ratio"])
- .to eql [nil, 50]
- end
end
end
describe "error during job execution" do
let_work_packages(<<~TABLE)
- subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
- wp working | | 4h | 60% | 10h | 4h | 60%
- wp breaking | | 4h | 60% | 10h | 4h | 60%
+ subject | work | remaining work | % complete
+ wp working | | 4h | 60%
+ wp breaking | | 4h | 60%
TABLE
before do
@@ -897,9 +858,9 @@ RSpec.describe UpdateProgressCalculation, type: :model do
it "does not update the work packages" do
expect_work_packages(WorkPackage.all, <<~TABLE)
- subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
- wp working | | 4h | 60% | 10h | 4h | 60%
- wp breaking | | 4h | 60% | 10h | 4h | 60%
+ subject | work | remaining work | % complete
+ wp working | | 4h | 60%
+ wp breaking | | 4h | 60%
TABLE
end
end
diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rb
index ce44ef03c83..b549fca80e5 100644
--- a/spec/services/work_packages/update_ancestors_service_spec.rb
+++ b/spec/services/work_packages/update_ancestors_service_spec.rb
@@ -86,7 +86,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
context "when changing child status to a status with a default done ratio" do
%i[status status_id].each do |field|
context "with the #{field} field" do
- it "recomputes child remaining work and update ancestors total % complete accordingly" do
+ it "recomputes child remaining work and updates ancestors total % complete accordingly" do
value =
case field
when :status then closed_status
@@ -98,7 +98,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
expect_work_packages([parent, child], <<~TABLE)
| subject | work | total work | remaining work | total remaining work | % complete | total % complete |
| parent | 10h | 15h | 10h | 10h | 0% | 33% |
- | child | 5h | 5h | 0h | 0h | 100% | 100% |
+ | child | 5h | | 0h | | 100% | |
TABLE
end
end
@@ -126,7 +126,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
expect_work_packages(table_work_packages, <<~TABLE)
hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete
parent | Open | | 15h | | 0h | 0% | 100%
- child1 | Closed | 10h | 10h | 0h | 0h | 100% | 100%
+ child1 | Closed | 10h | | 0h | | 100% |
child2 | Closed | 5h | | 0h | | 100% |
TABLE
end
@@ -858,10 +858,11 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
end
- describe "estimated_hours propagation" do
+ describe "work propagation" do
shared_let(:parent) { create(:work_package, subject: "parent") }
+ shared_let(:child) { create(:work_package, subject: "child", parent:) }
- context "when setting estimated hours of a work package" do
+ context "when setting work of a work package having children without any work value" do
before do
parent.estimated_hours = 2.0
end
@@ -871,7 +872,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.call(%i(estimated_hours))
end
- it "sets its derived value to the same value" do
+ it "sets its total work to the same value" do
expect { call_result }
.to change(parent, :derived_estimated_hours).from(nil).to(2.0)
expect(call_result).to be_success
@@ -880,7 +881,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
context "for the new ancestors chain" do
- context "with parent having no work" do
+ context "with parent having no work set" do
let_work_packages(<<~TABLE)
hierarchy | work |
parent | |
@@ -894,17 +895,17 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.call(%i(estimated_hours))
end
- it "sets parent derived remaining work to the sum of children remaining work" do
+ it "sets parent total work to the sum of children work" do
expect(call_result).to be_success
updated_work_packages = call_result.dependent_results.map(&:result)
expect_work_packages(updated_work_packages, <<~TABLE)
- subject | derived work |
- parent | 2.5h |
+ subject | total work |
+ parent | 2.5h |
TABLE
end
end
- context "with parent having some remaining work" do
+ context "with parent having work set" do
let_work_packages(<<~TABLE)
hierarchy | work |
parent | 5.25h |
@@ -918,12 +919,12 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.call(%i(estimated_hours))
end
- it "sets parent derived work to the sum of itself and children remaining work" do
+ it "sets parent total work to the sum of itself and children work" do
expect(call_result).to be_success
updated_work_packages = call_result.dependent_results.map(&:result)
expect_work_packages(updated_work_packages, <<~TABLE)
- subject | derived work |
- parent | 7.75h |
+ subject | total work |
+ parent | 7.75h |
TABLE
end
end
@@ -941,22 +942,23 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.call(%i(estimated_hours))
end
- it "does not update the parent derived work" do
+ it "does not update the parent total work" do
expect(call_result).to be_success
expect(call_result.dependent_results).to be_empty
expect_work_packages([parent.reload], <<~TABLE)
- subject | derived work |
- parent | |
+ subject | total work |
+ parent | |
TABLE
end
end
end
end
- describe "remaining_hours propagation" do
+ describe "remaining work propagation" do
shared_let(:parent) { create(:work_package, subject: "parent") }
+ shared_let(:child) { create(:work_package, subject: "child", parent:) }
- context "when setting remaining hours of a work package" do
+ context "when setting remaining work of a work package having children without any remaining work value" do
before do
parent.remaining_hours = 2.0
end
@@ -966,7 +968,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.call(%i(remaining_hours))
end
- it "sets its derived value to the same value" do
+ it "sets its total remaining work to the same value" do
expect { call_result }
.to change(parent, :derived_remaining_hours).from(nil).to(2.0)
expect(call_result).to be_success
@@ -975,7 +977,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
context "for the new ancestors chain" do
- context "with parent having no remaining work" do
+ context "with parent having no remaining work set" do
let_work_packages(<<~TABLE)
hierarchy | remaining work |
parent | |
@@ -989,17 +991,17 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.call(%i(remaining_hours))
end
- it "sets parent derived remaining work to the sum of children remaining work" do
+ it "sets parent total remaining work to the sum of children remaining work" do
expect(call_result).to be_success
updated_work_packages = call_result.dependent_results.map(&:result)
expect_work_packages(updated_work_packages, <<~TABLE)
- subject | derived remaining work
- parent | 2.5h
+ subject | total remaining work
+ parent | 2.5h
TABLE
end
end
- context "with parent having some remaining work" do
+ context "with parent having remaining work set" do
let_work_packages(<<~TABLE)
hierarchy | remaining work |
parent | 5.25h |
@@ -1013,12 +1015,12 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.call(%i(remaining_hours))
end
- it "sets parent derived remaining work to the sum of itself and children remaining work" do
+ it "sets parent total remaining work to the sum of itself and children remaining work" do
expect(call_result).to be_success
updated_work_packages = call_result.dependent_results.map(&:result)
expect_work_packages(updated_work_packages, <<~TABLE)
- subject | derived remaining work
- parent | 7.75h
+ subject | total remaining work
+ parent | 7.75h
TABLE
end
end
@@ -1036,11 +1038,11 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
.call(%i(remaining_hours))
end
- it "does not update the parent derived remaining work" do
+ it "does not update the parent total remaining work" do
expect(call_result).to be_success
expect(call_result.dependent_results).to be_empty
expect_work_packages([parent.reload], <<~TABLE)
- subject | derived remaining work
+ subject | total remaining work
parent |
TABLE
end
@@ -1055,7 +1057,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
let_work_packages(<<~TABLE)
hierarchy | work | total work | remaining work | total remaining work
parent | | 10h | 7h |
- child1 | 10h | 10h | |
+ child1 | 10h | | |
TABLE
subject(:call_result) do
@@ -1077,7 +1079,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
let_work_packages(<<~TABLE)
hierarchy | work | total work | remaining work | total remaining work
parent | 10h | | | 7h
- child1 | | | 7h | 7h
+ child1 | | | 7h |
TABLE
subject(:call_result) do