Project

General

Profile

Feature #24277 » 03_calculate_done_ratio_using_time.patch

Marius BĂLTEANU, 2016-11-12 19:53

View differences:

app/models/issue.rb
57 57
  acts_as_activity_provider :scope => preload(:project, :author, :tracker, :status),
58 58
                            :author_key => :author_id
59 59

  
60
  DONE_RATIO_OPTIONS = %w(issue_field issue_status)
60
  DONE_RATIO_OPTIONS = %w(issue_field issue_status issue_time)
61 61

  
62 62
  attr_reader :current_journal
63 63
  delegate :notes, :notes=, :private_notes, :private_notes=, :to => :current_journal, :allow_nil => true
......
675 675
  def done_ratio
676 676
    if Issue.use_status_for_done_ratio? && status && status.default_done_ratio
677 677
      status.default_done_ratio
678
    elsif Issue.use_time_for_done_ratio?
679
      if done_ratio_derived?
680
        calculate_done_ratio_from_time(total_estimated_hours, total_remaining_hours)
681
      else
682
        calculate_done_ratio_from_time(estimated_hours, remaining_hours)
683
      end
678 684
    else
679 685
      read_attribute(:done_ratio)
680 686
    end
......
688 694
    Setting.issue_done_ratio == 'issue_field'
689 695
  end
690 696

  
697
  def self.use_time_for_done_ratio?
698
    Setting.issue_done_ratio == 'issue_time'
699
  end
700

  
691 701
  def validate_issue
692 702
    if due_date && start_date && (start_date_changed? || due_date_changed?) && due_date < start_date
693 703
      errors.add :due_date, :greater_than_start_date
......
1751 1761
      self.done_ratio ||= 0
1752 1762
    end
1753 1763
  end
1764

  
1765
  def calculate_done_ratio_from_time(estimated, remaining)
1766
    if remaining.to_f == 0
1767
      100
1768
    elsif remaining.to_f > 0
1769
      ratio = (estimated.to_f - remaining.to_f) / estimated.to_f * 100
1770
      [ratio, 0].max.to_i
1771
    else
1772
      0.0
1773
    end
1774
  end
1754 1775
end
config/locales/en.yml
421 421
  setting_issue_done_ratio: Calculate the issue done ratio with
422 422
  setting_issue_done_ratio_issue_field: Use the issue field
423 423
  setting_issue_done_ratio_issue_status: Use the issue status
424
  setting_issue_done_ratio_issue_time: Use the issue remaining and estimated time
424 425
  setting_start_of_week: Start calendars on
425 426
  setting_rest_api_enabled: Enable REST web service
426 427
  setting_cache_formatted_text: Cache formatted text
test/unit/issue_subtasking_test.rb
218 218
    end
219 219
  end
220 220

  
221
  def test_done_ratio_should_be_calculated_from_total_estimated_and_total_remaining_time
222
    with_settings :parent_issue_done_ratio => 'derived', :issue_done_ratio => 'issue_time' do
223
      parent = Issue.generate!
224
      parent.generate_child!(:estimated_hours => 40, :remaining_hours => 20)
225
      parent.generate_child!(:estimated_hours => 40, :remaining_hours => 10)
226
      parent.generate_child!(:estimated_hours => 20, :remaining_hours => 20)
227
      parent.generate_child!(:estimated_hours => 0, :remaining_hours => 0)
228

  
229
      assert_equal 50, parent.reload.done_ratio
230
    end
231
  end
232

  
221 233
  def test_done_ratio_of_parent_should_reflect_children
222 234
    root = Issue.generate!
223 235
    child1 = root.generate_child!
test/unit/issue_test.rb
2590 2590
    end
2591 2591
  end
2592 2592

  
2593
  def test_done_ratio_should_be_calculated_from_estimated_and_remaining_time
2594
    @issue = Issue.find(1)
2595
    @issue.update_attribute(:estimated_hours, 50)
2596
    @issue.update_attribute(:remaining_hours, 25)
2597

  
2598
    @issue2 = Issue.find(2)
2599
    @issue2.update_attribute(:estimated_hours, 50)
2600
    @issue2.update_attribute(:remaining_hours, 50)
2601

  
2602
    @issue3 = Issue.find(3)
2603
    @issue3.update_attribute(:estimated_hours, 50)
2604
    @issue3.update_attribute(:remaining_hours, 0)
2605

  
2606
    with_settings :issue_done_ratio => 'issue_time' do
2607
      assert_equal 50, @issue.done_ratio
2608
      assert_equal 0, @issue2.done_ratio
2609
      assert_equal 100, @issue3.done_ratio
2610
    end
2611
  end
2612

  
2593 2613
  test "#by_tracker" do
2594 2614
    User.current = User.anonymous
2595 2615
    groups = Issue.by_tracker(Project.find(1))
(6-6/14)