From 1324772355e4212a3e1939cf9a933d3f5aea1ab3 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Thu, 1 Sep 2016 17:03:17 +0000 Subject: [PATCH] Fixed that progress of parent should be calculated with total estimated hours of children (#23511). git-svn-id: http://svn.redmine.org/redmine/trunk@15802 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/issue.rb | 25 +++++++++++++++---------- test/unit/issue_subtasking_test.rb | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/models/issue.rb b/app/models/issue.rb index a8d5e52b7..6cbb18033 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1553,18 +1553,23 @@ class Issue < ActiveRecord::Base end if p.done_ratio_derived? - # done ratio = weighted average ratio of leaves + # done ratio = average ratio of children weighted with their total estimated hours unless Issue.use_status_for_done_ratio? && p.status && p.status.default_done_ratio - child_count = p.children.count - if child_count > 0 - average = p.children.where("estimated_hours > 0").average(:estimated_hours).to_f - if average == 0 - average = 1 + children = p.children.to_a + if children.any? + child_with_total_estimated_hours = children.select {|c| c.total_estimated_hours.to_f > 0.0} + if child_with_total_estimated_hours.any? + average = child_with_total_estimated_hours.map(&:total_estimated_hours).sum.to_f / child_with_total_estimated_hours.count + else + average = 1.0 end - done = p.children.joins(:status). - sum("COALESCE(CASE WHEN estimated_hours > 0 THEN estimated_hours ELSE NULL END, #{average}) " + - "* (CASE WHEN is_closed = #{self.class.connection.quoted_true} THEN 100 ELSE COALESCE(done_ratio, 0) END)").to_f - progress = done / (average * child_count) + done = children.map {|c| + estimated = c.total_estimated_hours.to_f + estimated = average unless estimated > 0.0 + ratio = c.closed? ? 100 : (c.done_ratio || 0) + estimated * ratio + }.sum + progress = done / (average * children.count) p.done_ratio = progress.round end end diff --git a/test/unit/issue_subtasking_test.rb b/test/unit/issue_subtasking_test.rb index 189c6988c..020bcfdfc 100644 --- a/test/unit/issue_subtasking_test.rb +++ b/test/unit/issue_subtasking_test.rb @@ -143,6 +143,26 @@ class IssueSubtaskingTest < ActiveSupport::TestCase end end + def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any_with_grandchildren + # parent + # child 1 (2h estd, 0% done) + # child 2 (no estd) + # child a (2h estd, 50% done) + # child b (2h estd, 50% done) + # + # => parent should have a calculated progress of 33% + # + with_settings :parent_issue_done_ratio => 'derived' do + parent = Issue.generate! + parent.generate_child!(:estimated_hours => 2, :done_ratio => 0) + child = parent.generate_child! + child.generate_child!(:estimated_hours => 2, :done_ratio => 50) + child.generate_child!(:estimated_hours => 2, :done_ratio => 50) + assert_equal 50, child.reload.done_ratio + assert_equal 33, parent.reload.done_ratio + end + end + def test_parent_done_ratio_with_child_estimate_to_0_should_reach_100 with_settings :parent_issue_done_ratio => 'derived' do parent = Issue.generate! -- 2.39.5