##// END OF EJS Templates
Preload total spent time on the issue list with 1 query (#11253)....
Jean-Philippe Lang -
r14025:5e1a6e1040f2
parent child
Show More
@@ -918,11 +918,10 class Issue < ActiveRecord::Base
918
918
919 # Returns the total number of hours spent on this issue and its descendants
919 # Returns the total number of hours spent on this issue and its descendants
920 def total_spent_hours
920 def total_spent_hours
921 if leaf?
921 @total_spent_hours ||= if leaf?
922 spent_hours
922 spent_hours
923 else
923 else
924 @total_spent_hours ||=
924 self_and_descendants.joins(:time_entries).sum("#{TimeEntry.table_name}.hours").to_f || 0.0
925 self_and_descendants.joins(:time_entries).sum("#{TimeEntry.table_name}.hours").to_f || 0.0
926 end
925 end
927 end
926 end
928
927
@@ -948,7 +947,7 class Issue < ActiveRecord::Base
948 end
947 end
949 end
948 end
950
949
951 # Preloads visible spent time for a collection of issues
950 # Preloads visible total spent time for a collection of issues
952 def self.load_visible_spent_hours(issues, user=User.current)
951 def self.load_visible_spent_hours(issues, user=User.current)
953 if issues.any?
952 if issues.any?
954 hours_by_issue_id = TimeEntry.visible(user).group(:issue_id).sum(:hours)
953 hours_by_issue_id = TimeEntry.visible(user).group(:issue_id).sum(:hours)
@@ -958,6 +957,16 class Issue < ActiveRecord::Base
958 end
957 end
959 end
958 end
960
959
960 def self.load_visible_total_spent_hours(issues, user=User.current)
961 if issues.any?
962 hours_by_issue_id = TimeEntry.visible(user).joins(:issue).joins("JOIN #{Issue.table_name} parent ON parent.root_id = #{Issue.table_name}.root_id" +
963 " AND parent.lft <= #{Issue.table_name}.lft AND parent.rgt >= #{Issue.table_name}.rgt").group("parent.id").sum(:hours)
964 issues.each do |issue|
965 issue.instance_variable_set "@total_spent_hours", (hours_by_issue_id[issue.id] || 0)
966 end
967 end
968 end
969
961 # Preloads visible relations for a collection of issues
970 # Preloads visible relations for a collection of issues
962 def self.load_visible_relations(issues, user=User.current)
971 def self.load_visible_relations(issues, user=User.current)
963 if issues.any?
972 if issues.any?
@@ -353,6 +353,9 class IssueQuery < Query
353 if has_column?(:spent_hours)
353 if has_column?(:spent_hours)
354 Issue.load_visible_spent_hours(issues)
354 Issue.load_visible_spent_hours(issues)
355 end
355 end
356 if has_column?(:total_spent_hours)
357 Issue.load_visible_total_spent_hours(issues)
358 end
356 if has_column?(:relations)
359 if has_column?(:relations)
357 Issue.load_visible_relations(issues)
360 Issue.load_visible_relations(issues)
358 end
361 end
General Comments 0
You need to be logged in to leave comments. Login now