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
Account for status exclusion from total calculations in simple average mode
Aaron Contreras1f1239388026
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rbindex 7e44b238740..e70189edeaf 100644--- a/app/services/work_packages/update_ancestors_service.rb+++ b/app/services/work_packages/update_ancestors_service.rb@@ -140,11 +140,9 @@ class WorkPackages::UpdateAncestorsServiceenddef calculate_simple_average_percent_complete(work_package, loader)- all_done_ratios = loader- .children_of(work_package)- .map { |child| child.derived_done_ratio || child.done_ratio || 0 }+ all_done_ratios = children_done_ratio_values(work_package, loader)- if work_package.done_ratio.present?+ if work_package.done_ratio.present? && !work_package.status.excluded_from_totalsall_done_ratios << work_package.done_ratioend@@ -152,6 +150,13 @@ class WorkPackages::UpdateAncestorsServiceprogress.roundend+ def children_done_ratio_values(work_package, loader)+ loader+ .children_of(work_package)+ .reject(&:status_excluded_from_totals)+ .map { |child| child.derived_done_ratio || child.done_ratio || 0 }+ end+# Sets the ignore_non_working_days to true if any descendant has its value set to true.# If there is no value returned from the descendants, that means that the work package in# question no longer has a descendant. But since we are in the service going up the ancestor chain,diff --git a/app/workers/work_packages/progress/apply_total_percent_complete_mode_change_job.rb b/app/workers/work_packages/progress/apply_total_percent_complete_mode_change_job.rbindex 7feb4fc7e2c..a2c614497d6 100644--- a/app/workers/work_packages/progress/apply_total_percent_complete_mode_change_job.rb+++ b/app/workers/work_packages/progress/apply_total_percent_complete_mode_change_job.rb@@ -106,7 +106,12 @@ class WorkPackages::Progress::ApplyTotalPercentCompleteModeChangeJob < WorkPackaWHEN current_depth = min_depth THEN NULLELSE ROUND((- COALESCE(wp.p_complete, 0) + (+ /* Exclude the current work package if it has a status excluded from totals */+ CASE WHEN wp.status_excluded_from_totals+ THEN 0+ /* Otherwise, use the current work package's % complete value or 0 if unset */+ ELSE COALESCE(wp.p_complete, 0)+ END + (SELECTSUM(COALESCE(child_wp.total_p_complete, child_wp.p_complete, 0)@@ -115,18 +120,24 @@ class WorkPackages::Progress::ApplyTotalPercentCompleteModeChangeJob < WorkPackatemp_wp_progress_values child_wpWHEREchild_wp.parent_id = wp.id+ /* Exclude children with a status excluded from totals */+ AND NOT child_wp.status_excluded_from_totals)- ) / (- CASE- WHEN wp.p_complete IS NOT NULL THEN 1- ELSE 0- END + (+ ) / (+ /* Exclude the current work package if it has a status excluded from totals */+ CASE WHEN wp.status_excluded_from_totals+ THEN 0+ /* Otherwise, count the current work package if it has a % complete value set */+ ELSE(CASE WHEN wp.p_complete IS NOT NULL THEN 1 ELSE 0 END)+ END + (SELECTCOUNT(1)FROMtemp_wp_progress_values child_wpWHEREchild_wp.parent_id = wp.id+ /* Exclude children with a status excluded from totals */+ AND NOT child_wp.status_excluded_from_totals)))diff --git a/app/workers/work_packages/progress/sql_commands.rb b/app/workers/work_packages/progress/sql_commands.rbindex ce4ff402aea..4f2c7777120 100644--- a/app/workers/work_packages/progress/sql_commands.rb+++ b/app/workers/work_packages/progress/sql_commands.rb@@ -95,12 +95,13 @@ module WorkPackages::Progress::SqlCommandsexecute(<<~SQL.squish)CREATE UNLOGGED TABLE temp_wp_progress_values ASSELECT- id,- parent_id,- status_id,+ work_packages.id as id,+ work_packages.parent_id as parent_id,+ statuses.excluded_from_totals AS status_excluded_from_totals,done_ratio AS p_complete,NULL::INTEGER AS total_p_completeFROM work_packages+ LEFT JOIN statuses ON work_packages.status_id = statuses.idSQLenddiff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex 57e5cfeddea..dac34b0aabc 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -975,6 +975,32 @@ RSpec.describe WorkPackages::UpdateAncestorsService,TABLEendend++ context "with parent having % complete set " \+ "and a multi-level children hierarchy with some % complete set " \+ "and some children having status excluded from totals" do+ let_work_packages(<<~TABLE)+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete+ parent | Open | | 44h | | 21h | 10% |+ child1 | Open | 15h | 23h | 10h | 14h | 33% | 43%+ grandchild1 | Open | 5h | | 3h | | 40% |+ grandchild2 | Open | 3h | | 1h | | 67% |+ child2 | Rejected | 5h | 7h | 2.5h | 3.5h | 50% | 75%+ grandchild3 | Open | 2h | | 1h | | 100% |+ child3 | Open | 10h | 14h | 2.5h | 3.5h | 75% |+ grandchild4 | Rejected | 4h | | 1h | | 75% |+ child4 | Open | | | | | 60% |+ TABLE++ it "sets the total % complete solely based on % complete values of direct children" do+ expect(call_result).to be_success+ updated_work_packages = call_result.all_results+ expect_work_packages(updated_work_packages, <<~TABLE)+ subject | total % complete+ parent | 47%+ TABLE+ end+ endenddescribe "ignore_non_working_days propagation" dodiff --git a/spec/workers/work_packages/progress/apply_total_percent_complete_mode_change_job_spec.rb b/spec/workers/work_packages/progress/apply_total_percent_complete_mode_change_job_spec.rbindex 4ad6cae4709..5e393754f54 100644--- a/spec/workers/work_packages/progress/apply_total_percent_complete_mode_change_job_spec.rb+++ b/spec/workers/work_packages/progress/apply_total_percent_complete_mode_change_job_spec.rb@@ -185,6 +185,33 @@ RSpec.describe WorkPackages::Progress::ApplyTotalPercentCompleteModeChangeJob doendend+ context "on a multi-level hierarchy with work and remaining work values set " \+ "and a child with a status excluded from totals" do+ it "updates the total % complete of parent work packages excluding the child" do+ expect_performing_job_changes(+ mode: "work_weighted_average",+ from: <<~TABLE,+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete+ parent | New | 10h | 30h | 6h | 6h | 40% | 63%+ child1 | New | 15h | | 0h | | 100% |+ child2 | New | | | | | 40% |+ child3 | New | 5h | 5h | 0h | 0h | 100% | 70%+ grandchild1 | New | | | | | 40% |+ grandchild2 | Excluded | 20h | | 0h | | 100% |+ TABLE+ to: <<~TABLE+ subject | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete+ parent | New | 10h | 30h | 6h | 6h | 40% | 80%+ child1 | New | 15h | | 0h | | 100% |+ child2 | New | | | | | 40% |+ child3 | New | 5h | 5h | 0h | 0h | 100% | 100%+ grandchild1 | New | | | | | 40% |+ grandchild2 | Excluded | 20h | | 0h | | 100% |+ TABLE+ )+ end+ end+describe "journal entries" do# rubocop:disable RSpec/ExampleLengthit "creates journal entries for the modified work packages" do@@ -341,6 +368,34 @@ RSpec.describe WorkPackages::Progress::ApplyTotalPercentCompleteModeChangeJob doendend+ context "on a multi-level hierarchy with work and remaining work values set " \+ "and a child having a status excluded from totals" do+ it "updates the total % complete of parent work packages irrelevant of work values " \+ "excluding the child with the excluded status" do+ expect_performing_job_changes(+ mode: "simple_average",+ from: <<~TABLE,+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete+ parent | New | 10h | 50h | 6h | 6h | 40% | 88%+ child1 | New | 15h | | 0h | | 100% |+ child2 | New | | | | | 40% |+ child3 | Excluded | 5h | 25h | 0h | 0h | 100% | 100%+ grandchild1 | New | | | | | 40% |+ grandchild2 | New | 20h | | 0h | | 100% |+ TABLE+ to: <<~TABLE+ subject | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete+ parent | New | 10h | 50h | 6h | 6h | 40% | 60%+ child1 | New | 15h | | 0h | | 100% |+ child2 | New | | | | | 40% |+ child3 | Excluded | 5h | 25h | 0h | 0h | 100% | 70%+ grandchild1 | New | | | | | 40% |+ grandchild2 | New | 20h | | 0h | | 100% |+ TABLE+ )+ end+ end+describe "journal entries" do# rubocop:disable RSpec/ExampleLengthit "creates journal entries for the modified work packages" do