]> source.dussan.org Git - redmine.git/commitdiff
Optimize Version model by reducing redundant SQL count queries (#40798).
authorGo MAEDA <maeda@farend.jp>
Mon, 17 Jun 2024 06:55:36 +0000 (06:55 +0000)
committerGo MAEDA <maeda@farend.jp>
Mon, 17 Jun 2024 06:55:36 +0000 (06:55 +0000)
Patch by Go MAEDA (@maeda).

git-svn-id: https://svn.redmine.org/redmine/trunk@22880 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/version.rb

index 7434f0c32a1059f3268a49296b941803842e41fd..c12420c3fab7e533b4b2fa86599f87a8addae40a 100644 (file)
@@ -45,22 +45,17 @@ module FixedIssuesExtension
   # Returns the completion percentage of this version based on the amount of open/closed issues
   # and the time spent on the open issues.
   def completed_percent
-    if count == 0
-      0
-    elsif open_count == 0
-      100
-    else
-      issues_progress(false) + issues_progress(true)
-    end
+    return 0 if open_count + closed_count == 0
+    return 100 if open_count == 0
+
+    issues_progress(false) + issues_progress(true)
   end
 
   # Returns the percentage of issues that have been marked as 'closed'.
   def closed_percent
-    if count == 0
-      0
-    else
-      issues_progress(false)
-    end
+    return 0 if open_count + closed_count == 0
+
+    issues_progress(false)
   end
 
   private
@@ -105,14 +100,15 @@ module FixedIssuesExtension
     @issues_progress ||= {}
     @issues_progress[open] ||= begin
       progress = 0
-      if count > 0
+      issues_count = open_count + closed_count
+      if issues_count > 0
         done = self.open(open).sum do |c|
           estimated = c.total_estimated_hours.to_f
           estimated = estimated_average unless estimated > 0.0
           ratio = c.closed? ? 100 : (c.done_ratio || 0)
           estimated * ratio
         end
-        progress = done / (estimated_average * count)
+        progress = done / (estimated_average * issues_count)
       end
       progress
     end