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

Compute totals during progress values migration

Christophe Bliard4874bd3f3f5a
diff --git a/app/workers/work_packages/update_progress_job.rb b/app/workers/work_packages/update_progress_job.rb
index 664e62009bc..39dbb1b39df 100644
--- a/app/workers/work_packages/update_progress_job.rb
+++ b/app/workers/work_packages/update_progress_job.rb
@@ -32,10 +32,8 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
def perform(current_mode:, previous_mode:)
with_temporary_progress_table do
- if previous_mode == "disabled"
- unset_all_percent_complete_values
- end
if current_mode == "field"
+ unset_all_percent_complete_values if previous_mode == "disabled"
fix_remaining_work_set_with_100p_complete
fix_remaining_work_exceeding_work
fix_only_work_being_set
@@ -51,8 +49,9 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
derive_remaining_work_from_work_and_p_complete
end
- updated_work_package_ids = copy_progress_values_to_work_packages
- create_journals_for_updated_work_packages(updated_work_package_ids)
+ update_totals
+
+ copy_progress_values_to_work_packages_and_update_journals
end
end
@@ -73,7 +72,10 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
status_id,
estimated_hours,
remaining_hours,
- done_ratio
+ done_ratio,
+ NULL::double precision AS total_work,
+ NULL::double precision AS total_remaining_work,
+ NULL::integer AS total_p_complete
FROM work_packages
SQL
end
@@ -184,12 +186,41 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
SQL
end
+ def update_totals
+ execute(<<~SQL)
+ UPDATE temp_wp_progress_values
+ SET total_work = totals.total_work,
+ total_remaining_work = totals.total_remaining_work,
+ total_p_complete = CASE
+ WHEN totals.total_work = 0 THEN NULL
+ ELSE (1 - (totals.total_remaining_work / totals.total_work)) * 100
+ END
+ FROM (
+ SELECT wp_tree.ancestor_id AS id,
+ SUM(estimated_hours) AS total_work,
+ SUM(remaining_hours) AS total_remaining_work
+ FROM work_package_hierarchies wp_tree
+ LEFT JOIN temp_wp_progress_values wp_progress ON wp_tree.descendant_id = wp_progress.id
+ GROUP BY wp_tree.ancestor_id
+ ) totals
+ WHERE temp_wp_progress_values.id = totals.id
+ SQL
+ end
+
+ def copy_progress_values_to_work_packages_and_update_journals
+ updated_work_package_ids = copy_progress_values_to_work_packages
+ create_journals_for_updated_work_packages(updated_work_package_ids)
+ end
+
def copy_progress_values_to_work_packages
results = execute(<<~SQL)
UPDATE work_packages
SET estimated_hours = temp_wp_progress_values.estimated_hours,
remaining_hours = temp_wp_progress_values.remaining_hours,
done_ratio = temp_wp_progress_values.done_ratio,
+ derived_estimated_hours = temp_wp_progress_values.total_work,
+ derived_remaining_hours = temp_wp_progress_values.total_remaining_work,
+ derived_done_ratio = temp_wp_progress_values.total_p_complete,
lock_version = lock_version + 1,
updated_at = NOW()
FROM temp_wp_progress_values
@@ -198,6 +229,9 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
work_packages.estimated_hours IS DISTINCT FROM temp_wp_progress_values.estimated_hours
OR work_packages.remaining_hours IS DISTINCT FROM temp_wp_progress_values.remaining_hours
OR work_packages.done_ratio IS DISTINCT FROM temp_wp_progress_values.done_ratio
+ OR work_packages.derived_estimated_hours IS DISTINCT FROM temp_wp_progress_values.total_work
+ OR work_packages.derived_remaining_hours IS DISTINCT FROM temp_wp_progress_values.total_remaining_work
+ OR work_packages.derived_done_ratio IS DISTINCT FROM temp_wp_progress_values.total_p_complete
)
RETURNING work_packages.id
SQL
diff --git a/spec/migrations/update_progress_calculation_spec.rb b/spec/migrations/update_progress_calculation_spec.rb
index 4fe07015c18..7a2d52ce115 100644
--- a/spec/migrations/update_progress_calculation_spec.rb
+++ b/spec/migrations/update_progress_calculation_spec.rb
@@ -80,10 +80,10 @@ 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
- wp all unset | | |
- wp only pc set | | | 60%
- wp all set consistent | 10h | 4h | 60%
+ subject | 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%
TABLE
before do
@@ -648,4 +648,58 @@ RSpec.describe UpdateProgressCalculation, type: :model do
end
end
end
+
+ describe "totals computation" do
+ context "when totals are not up-to-date" do
+ # rubocop:disable RSpec/ExampleLength
+ it "computes them" do
+ expect_migrates(
+ from: <<~TABLE,
+ hierarchy | work | remaining work | ∑ work | ∑ remaining work | ∑ % complete
+ grandparent | 10h | 1h |
+ parent 1 | 10h | 2h |
+ child 11 | 10h | 3h |
+ child 12 | 10h | 4h |
+ child 13 | 10h | 5h |
+ parent 2 | 10h | 6h |
+ child 21 | 10h | 7h |
+ child 22 | 10h | 8h |
+ child 23 | 10h | 9h |
+ child 24 | 10h | 10h |
+ TABLE
+ to: <<~TABLE
+ 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%
+ 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%
+ TABLE
+ )
+ end
+ # rubocop:enable RSpec/ExampleLength
+ end
+
+ context "when total work and total remaining work are 0h" do
+ it "unset total % complete" do
+ expect_migrates(
+ from: <<~TABLE,
+ hierarchy | work | remaining work |
+ parent | 0h | 0h |
+ child | 0h | 0h |
+ TABLE
+ to: <<~TABLE
+ subject | work | remaining work | ∑ work | ∑ remaining work | ∑ % complete
+ parent | 0h | 0h | 0h | 0h |
+ child | 0h | 0h | 0h | 0h |
+ TABLE
+ )
+ end
+ end
+ end
end
diff --git a/spec/support/table_helpers/column.rb b/spec/support/table_helpers/column.rb
index d24a1255b78..f2938c2bbbd 100644
--- a/spec/support/table_helpers/column.rb
+++ b/spec/support/table_helpers/column.rb
@@ -46,15 +46,15 @@ module TableHelpers
raise ArgumentError, 'Please use "remaining work" instead of "remaining hours"'
when /\A\s*work/i
Duration.new(header:, attribute: :estimated_hours)
- when /derived work|total work/i
+ when /(∑|derived|total) work/i
Duration.new(header:, attribute: :derived_estimated_hours)
when /\A\s*remaining work/i
Duration.new(header:, attribute: :remaining_hours)
- when /derived remaining work|total remaining work/i
+ when /(∑|derived|total) remaining work/i
Duration.new(header:, attribute: :derived_remaining_hours)
when /\A\s*% complete/i
Percentage.new(header:, attribute: :done_ratio)
- when /total % complete/i
+ when /(∑|derived|total) % complete/i
Percentage.new(header:, attribute: :derived_done_ratio)
when /end date/i
Generic.new(header:, attribute: :due_date)