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

Extract common operations to sql_commands

Aaron Contrerasde862213616c
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.rb
index a2c614497d6..d14fd19fcea 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
@@ -63,104 +63,14 @@ class WorkPackages::Progress::ApplyTotalPercentCompleteModeChangeJob < WorkPacka
def update_total_percent_complete
case mode
when "work_weighted_average"
- update_to_work_weighted_average
+ update_total_percent_complete_in_work_weighted_average_mode
when "simple_average"
- update_to_simple_average
+ update_total_percent_complete_in_simple_average_mode
else
raise ArgumentError, "Invalid total percent complete mode: #{mode}"
end
end
- def update_to_work_weighted_average
- execute(<<~SQL.squish)
- UPDATE temp_wp_progress_values
- SET total_p_complete = CASE
- WHEN total_work IS NULL OR total_remaining_work IS NULL THEN NULL
- WHEN total_work = 0 THEN NULL
- ELSE ROUND(
- ((total_work - total_remaining_work)::float / total_work) * 100
- )
- END
- WHERE id IN (
- SELECT ancestor_id
- FROM work_package_hierarchies
- GROUP BY ancestor_id
- HAVING MAX(generations) > 0
- )
- SQL
- end
-
- def update_to_simple_average
- execute(<<~SQL.squish)
- DO $$
- DECLARE
- min_depth INTEGER := 0;
- max_depth INTEGER := (SELECT MAX(depth) FROM temp_work_package_depth);
- current_depth INTEGER := min_depth;
- BEGIN
- /* Navigate work packages and perform updates bottom-up */
- while current_depth <= max_depth loop
- UPDATE temp_wp_progress_values wp
- SET
- total_p_complete = CASE
- WHEN current_depth = min_depth THEN NULL
- ELSE ROUND(
- (
- /* 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 + (
- SELECT
- SUM(
- COALESCE(child_wp.total_p_complete, child_wp.p_complete, 0)
- )
- FROM
- temp_wp_progress_values child_wp
- WHERE
- child_wp.parent_id = wp.id
- /* Exclude children with a status excluded from totals */
- AND NOT child_wp.status_excluded_from_totals
- )
- ) / (
- /* 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 + (
- SELECT
- COUNT(1)
- FROM
- temp_wp_progress_values child_wp
- WHERE
- child_wp.parent_id = wp.id
- /* Exclude children with a status excluded from totals */
- AND NOT child_wp.status_excluded_from_totals
- )
- )
- )
- END
- /* Select only work packages at the curren depth */
- WHERE
- wp.id IN (
- SELECT
- id
- FROM
- temp_work_package_depth
- WHERE
- depth = current_depth
- );
-
- /* Go up a level from a child to a parent*/
- current_depth := current_depth + 1;
-
- END loop;
- END $$;
- SQL
- end
-
def journal_cause
assert_valid_cause_type!
diff --git a/app/workers/work_packages/progress/sql_commands.rb b/app/workers/work_packages/progress/sql_commands.rb
index 29f1a39948a..6d367d0f7f9 100644
--- a/app/workers/work_packages/progress/sql_commands.rb
+++ b/app/workers/work_packages/progress/sql_commands.rb
@@ -103,7 +103,7 @@ module WorkPackages::Progress::SqlCommands
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,
+ done_ratio,
NULL::INTEGER AS total_p_complete
FROM work_packages
LEFT JOIN statuses ON work_packages.status_id = statuses.id
@@ -221,13 +221,16 @@ module WorkPackages::Progress::SqlCommands
execute(<<~SQL.squish)
UPDATE temp_wp_progress_values
SET total_p_complete = CASE
+ WHEN total_work IS NULL OR total_remaining_work IS NULL THEN NULL
WHEN total_work = 0 THEN NULL
- ELSE (1 - (total_remaining_work / total_work)) * 100
+ ELSE ROUND(
+ ((total_work - total_remaining_work)::float / total_work) * 100
+ )
END
WHERE id IN (
- SELECT ancestor_id AS id
+ SELECT ancestor_id
FROM work_package_hierarchies
- GROUP BY id
+ GROUP BY ancestor_id
HAVING MAX(generations) > 0
)
SQL