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
[54646] Do not set totals of non-parent work packages on status changes
Christophe Bliard376dbb4e0389
diff --git a/app/workers/work_packages/apply_statuses_p_complete_job.rb b/app/workers/work_packages/apply_statuses_p_complete_job.rbindex 530a3bb3f2f..6182c167757 100644--- a/app/workers/work_packages/apply_statuses_p_complete_job.rb+++ b/app/workers/work_packages/apply_statuses_p_complete_job.rb@@ -28,10 +28,11 @@# See COPYRIGHT and LICENSE files for more details.#++-# rubocop:disable Rails/SquishedSQLHeredocsclass WorkPackages::ApplyStatusesPCompleteJob < ApplicationJobqueue_with_priority :default+ include WorkPackages::ProgressSqlCommands+VALID_CAUSE_TYPES = %w[progress_mode_changed_to_status_basedstatus_p_complete_changed@@ -86,118 +87,4 @@ class WorkPackages::ApplyStatusesPCompleteJob < ApplicationJobraise "Unable to handle cause type #{cause_type.inspect}"endend-- def with_temporary_progress_table- WorkPackage.transaction do- create_temporary_progress_table- yield- ensure- drop_temporary_progress_table- end- end-- def create_temporary_progress_table- execute(<<~SQL)- CREATE UNLOGGED TABLE temp_wp_progress_values- AS SELECT- id,- status_id,- estimated_hours,- remaining_hours,- 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-- def drop_temporary_progress_table- execute(<<~SQL)- DROP TABLE temp_wp_progress_values- SQL- end-- def set_p_complete_from_status- execute(<<~SQL.squish)- UPDATE temp_wp_progress_values- SET done_ratio = statuses.default_done_ratio- FROM statuses- WHERE temp_wp_progress_values.status_id = statuses.id- SQL- end-- def derive_remaining_work_from_work_and_p_complete- execute(<<~SQL.squish)- UPDATE temp_wp_progress_values- SET remaining_hours = ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)- WHERE estimated_hours IS NOT NULL- AND done_ratio IS NOT NULL- 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(cause)- updated_work_package_ids = copy_progress_values_to_work_packages- create_journals_for_updated_work_packages(updated_work_package_ids, cause:)- end-- def copy_progress_values_to_work_packages- results = execute(<<~SQL)- UPDATE work_packages- SET remaining_hours = temp_wp_progress_values.remaining_hours,- done_ratio = temp_wp_progress_values.done_ratio,- 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- WHERE work_packages.id = temp_wp_progress_values.id- AND (- 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_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- results.column_values(0)- end-- def create_journals_for_updated_work_packages(updated_work_package_ids, cause:)- WorkPackage.where(id: updated_work_package_ids).find_each do |work_package|- Journals::CreateService.new(work_package, system_user)- .call(cause:)- end- end-- # Executes an sql statement, shorter.- def execute(sql)- ActiveRecord::Base.connection.execute(sql)- end-- def system_user- @system_user ||= User.system- endend-# rubocop:enable Rails/SquishedSQLHeredocsdiff --git a/app/workers/work_packages/progress_sql_commands.rb b/app/workers/work_packages/progress_sql_commands.rbnew file mode 100644index 00000000000..c7b7ba67f4f--- /dev/null+++ b/app/workers/work_packages/progress_sql_commands.rb@@ -0,0 +1,158 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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.+#++++module WorkPackages::ProgressSqlCommands+ def with_temporary_progress_table+ WorkPackage.transaction do+ create_temporary_progress_table+ yield+ ensure+ drop_temporary_progress_table+ end+ end++ def create_temporary_progress_table+ execute(<<~SQL.squish)+ CREATE UNLOGGED TABLE temp_wp_progress_values+ AS SELECT+ id,+ status_id,+ estimated_hours,+ remaining_hours,+ 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++ def drop_temporary_progress_table+ execute(<<~SQL.squish)+ DROP TABLE temp_wp_progress_values+ SQL+ end++ def derive_remaining_work_from_work_and_p_complete+ execute(<<~SQL.squish)+ UPDATE temp_wp_progress_values+ SET remaining_hours =+ GREATEST(0,+ LEAST(estimated_hours,+ ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)+ )+ )+ WHERE estimated_hours IS NOT NULL+ AND done_ratio IS NOT NULL+ SQL+ end++ def set_p_complete_from_status+ execute(<<~SQL.squish)+ UPDATE temp_wp_progress_values+ SET done_ratio = statuses.default_done_ratio+ FROM statuses+ WHERE temp_wp_progress_values.status_id = statuses.id+ SQL+ end++ # Computes total work, total remaining work and total % complete for all work+ # packages having children.+ def update_totals+ execute(<<~SQL.squish)+ 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,+ MAX(generations) AS generations,+ 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+ AND totals.generations > 0+ SQL+ end++ def copy_progress_values_to_work_packages_and_update_journals(cause)+ updated_work_package_ids = copy_progress_values_to_work_packages+ create_journals_for_updated_work_packages(updated_work_package_ids, cause:)+ end++ def copy_progress_values_to_work_packages+ results = execute(<<~SQL.squish)+ 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+ WHERE work_packages.id = temp_wp_progress_values.id+ AND (+ 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+ results.column_values(0)+ end++ def create_journals_for_updated_work_packages(updated_work_package_ids, cause:)+ WorkPackage.where(id: updated_work_package_ids).find_each do |work_package|+ Journals::CreateService+ .new(work_package, system_user)+ .call(cause:)+ end+ end++ # Executes an sql statement, shorter.+ def execute(sql)+ ActiveRecord::Base.connection.execute(sql)+ end++ def system_user+ @system_user ||= User.system+ end+enddiff --git a/app/workers/work_packages/update_progress_job.rb b/app/workers/work_packages/update_progress_job.rbindex 929f0df263d..053103400e3 100644--- a/app/workers/work_packages/update_progress_job.rb+++ b/app/workers/work_packages/update_progress_job.rb@@ -26,35 +26,29 @@# See COPYRIGHT and LICENSE files for more details.#++-# rubocop:disable Rails/SquishedSQLHeredocsclass WorkPackages::UpdateProgressJob < ApplicationJobqueue_with_priority :default+ include WorkPackages::ProgressSqlCommands+attr_reader :current_mode, :previous_modedef perform(current_mode:, previous_mode:)@current_mode = current_mode@previous_mode = previous_mode+ journal_cause = { type: "system_update", feature: system_update_explanation }+with_temporary_progress_table doadjust_progress_valuesupdate_totalsunset_total_p_complete- copy_progress_values_to_work_packages_and_update_journals+ copy_progress_values_to_work_packages_and_update_journals(journal_cause)endendprivate- def with_temporary_progress_table- WorkPackage.transaction do- create_temporary_progress_table- yield- ensure- drop_temporary_progress_table- end- end-def adjust_progress_valuescase current_modewhen "field"@@ -76,28 +70,6 @@ class WorkPackages::UpdateProgressJob < ApplicationJobendend- def create_temporary_progress_table- execute(<<~SQL)- CREATE UNLOGGED TABLE temp_wp_progress_values- AS SELECT- id,- status_id,- estimated_hours,- remaining_hours,- 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-- def drop_temporary_progress_table- execute(<<~SQL)- DROP TABLE temp_wp_progress_values- SQL- end-def unset_all_percent_complete_valuesexecute(<<~SQL.squish)UPDATE temp_wp_progress_values@@ -163,20 +135,6 @@ class WorkPackages::UpdateProgressJob < ApplicationJobSQLend- def derive_remaining_work_from_work_and_p_complete- execute(<<~SQL.squish)- UPDATE temp_wp_progress_values- SET remaining_hours =- GREATEST(0,- LEAST(estimated_hours,- ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)- )- )- WHERE estimated_hours IS NOT NULL- AND done_ratio IS NOT NULL- SQL- end-def derive_unset_work_from_remaining_work_and_p_completeexecute(<<~SQL.squish)UPDATE temp_wp_progress_values@@ -203,40 +161,6 @@ class WorkPackages::UpdateProgressJob < ApplicationJobSQLend- def set_p_complete_from_status- execute(<<~SQL.squish)- UPDATE temp_wp_progress_values- SET done_ratio = statuses.default_done_ratio- FROM statuses- WHERE temp_wp_progress_values.status_id = statuses.id- 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- 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,- MAX(generations) AS generations,- 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- AND totals.generations > 0- SQL- end-# The value for derived_done_ratio had been calculated wrong in the past. So prior to executing the job# values in the work_packages and work_package_journals table sometimes contained wrong data.# The whole job/migration is now treating the derived_done_ratio as a value newly introduced even if it, under@@ -253,57 +177,17 @@ class WorkPackages::UpdateProgressJob < ApplicationJob# * The value before has been something and is now something. It could have been the same value as before. But# since the job resets the value to null, it will in every case be treated as having changed (set for the first time)def unset_total_p_complete- execute(<<~SQL)+ execute(<<~SQL.squish)UPDATE work_packagesSET derived_done_ratio = NULLSQL- execute(<<~SQL)+ execute(<<~SQL.squish)UPDATE work_package_journalsSET derived_done_ratio = NULLSQLend- 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- WHERE work_packages.id = temp_wp_progress_values.id- AND (- 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- results.column_values(0)- end-- def create_journals_for_updated_work_packages(updated_work_package_ids)- cause = { type: "system_update", feature: system_update_explanation }- WorkPackage.where(id: updated_work_package_ids).find_each do |work_package|- Journals::CreateService- .new(work_package, system_user)- .call(cause:)- end- end-def system_update_explanationif previous_mode == "disabled""progress_calculation_adjusted_from_disabled_mode"@@ -311,14 +195,4 @@ class WorkPackages::UpdateProgressJob < ApplicationJob"progress_calculation_adjusted"endend-- # Executes an sql statement, shorter.- def execute(sql)- ActiveRecord::Base.connection.execute(sql)- end-- def system_user- @system_user ||= User.system- endend-# rubocop:enable Rails/SquishedSQLHeredocsdiff --git a/spec/workers/work_packages/apply_statuses_p_complete_job_spec.rb b/spec/workers/work_packages/apply_statuses_p_complete_job_spec.rbindex 3d962808008..e1649fcdd11 100644--- a/spec/workers/work_packages/apply_statuses_p_complete_job_spec.rb+++ b/spec/workers/work_packages/apply_statuses_p_complete_job_spec.rb@@ -139,17 +139,17 @@ RSpec.describe WorkPackages::ApplyStatusesPCompleteJob dohierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completegrandparent | Doing (40%) | 1h | 0.8h | 20% | 20h | 9.8h | 51%parent | Doing (40%) | | | 20% | 19h | 9h | 53%- child 1 | Done (100%) | 9h | 0h | 100% | 9h | 0h | 100%- child 2 | Doing (40%) | 5h | 4h | 20% | 5h | 4h | 20%- child 3 | To do (0%) | 5h | 5h | 0% | 5h | 5h | 0%+ child 1 | Done (100%) | 9h | 0h | 100% | | |+ child 2 | Doing (40%) | 5h | 4h | 20% | | |+ child 3 | To do (0%) | 5h | 5h | 0% | | |TABLEto: <<~TABLEsubject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completegrandparent | Doing (40%) | 1h | 0.6h | 40% | 20h | 8.6h | 57%parent | Doing (40%) | | | 40% | 19h | 8h | 58%- child 1 | Done (100%) | 9h | 0h | 100% | 9h | 0h | 100%- child 2 | Doing (40%) | 5h | 3h | 40% | 5h | 3h | 40%- child 3 | To do (0%) | 5h | 5h | 0% | 5h | 5h | 0%+ child 1 | Done (100%) | 9h | 0h | 100% | | |+ child 2 | Doing (40%) | 5h | 3h | 40% | | |+ child 3 | To do (0%) | 5h | 5h | 0% | | |TABLE)end@@ -162,14 +162,14 @@ RSpec.describe WorkPackages::ApplyStatusesPCompleteJob dofrom: <<~TABLE,hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completeparent | To do (0%) | | | 0% | 20h | 8h | 60%- child 1 | Doing (40%) | 10h | 8h | 20% | 10h | 8h | 20%- child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%+ child 1 | Doing (40%) | 10h | 8h | 20% | | |+ child 2 | Done (100%) | 10h | 0h | 100% | | |TABLEto: <<~TABLE,subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completeparent | To do (0%) | | | 0% | 20h | 6h | 70%- child 1 | Doing (40%) | 10h | 6h | 40% | 10h | 6h | 40%- child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%+ child 1 | Doing (40%) | 10h | 6h | 40% | | |+ child 2 | Done (100%) | 10h | 0h | 100% | | |TABLEcause_type: "status_p_complete_changed",status_name: status_40p_doing.name,@@ -196,14 +196,14 @@ RSpec.describe WorkPackages::ApplyStatusesPCompleteJob dofrom: <<~TABLE,hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completeparent | To do (0%) | | | 0% | 20h | 8h | 60%- child 1 | Doing (40%) | 10h | 8h | 20% | 10h | 8h | 20%- child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%+ child 1 | Doing (40%) | 10h | 8h | 20% | | |+ child 2 | Done (100%) | 10h | 0h | 100% | | |TABLEto: <<~TABLE,subject | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % completeparent | To do (0%) | | | 0% | 20h | 6h | 70%- child 1 | Doing (40%) | 10h | 6h | 40% | 10h | 6h | 40%- child 2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%+ child 1 | Doing (40%) | 10h | 6h | 40% | | |+ child 2 | Done (100%) | 10h | 0h | 100% | | |TABLEcause_type: "progress_mode_changed_to_status_based")