From 2c9e5cc74405850e692c5bb08c3bc06dd853a9a8 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Mon, 20 Nov 2017 14:06:41 +0100 Subject: [PATCH 3/4] Performance opt - cache AR Proxy for Version#fixed_issues.visible @version.fixed_issues.visible@ is used quite often in the versions templates, but it's creating a new proxy instance every time. I.e. calculations, which are cached inside that instance will be performed each time. Instead we want to keep the instance, so that the caching of the calculations works as expected. --- app/helpers/versions_helper.rb | 4 ++-- app/models/version.rb | 5 +++++ app/views/versions/_overview.html.erb | 20 ++++++++++---------- app/views/versions/show.html.erb | 4 ++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/helpers/versions_helper.rb b/app/helpers/versions_helper.rb index 9d088a9d9..9ade12a75 100644 --- a/app/helpers/versions_helper.rb +++ b/app/helpers/versions_helper.rb @@ -57,9 +57,9 @@ module VersionsHelper h = Hash.new {|k,v| k[v] = [0, 0]} begin # Total issue count - version.fixed_issues.visible.group(criteria).count.each {|c,s| h[c][0] = s} + version.visible_fixed_issues.group(criteria).count.each {|c,s| h[c][0] = s} # Open issues count - version.fixed_issues.visible.open.group(criteria).count.each {|c,s| h[c][1] = s} + version.visible_fixed_issues.open.group(criteria).count.each {|c,s| h[c][1] = s} rescue ActiveRecord::RecordNotFound # When grouping by an association, Rails throws this exception if there's no result (bug) end diff --git a/app/models/version.rb b/app/models/version.rb index 12a7be903..74c3309e9 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -176,6 +176,7 @@ class Version < ActiveRecord::Base alias :base_reload :reload def reload(*args) @default_project_version = nil + @visible_fixed_issues = nil base_reload(*args) end @@ -257,6 +258,10 @@ class Version < ActiveRecord::Base fixed_issues.closed_count end + def visible_fixed_issues + @visible_fixed_issues ||= fixed_issues.visible + end + def wiki_page if project.wiki && !wiki_page_title.blank? @wiki_page ||= project.wiki.find_page(wiki_page_title) diff --git a/app/views/versions/_overview.html.erb b/app/views/versions/_overview.html.erb index ec7a18a6f..21224df43 100644 --- a/app/views/versions/_overview.html.erb +++ b/app/views/versions/_overview.html.erb @@ -14,22 +14,22 @@ <% end %> -<% if version.fixed_issues.visible.count > 0 %> - <%= progress_bar([version.fixed_issues.visible.closed_percent, version.fixed_issues.visible.completed_percent], +<% if version.visible_fixed_issues.count > 0 %> + <%= progress_bar([version.visible_fixed_issues.closed_percent, version.visible_fixed_issues.completed_percent], :titles => - ["%s: %0.0f%%" % [l(:label_closed_issues_plural), version.fixed_issues.visible.closed_percent], - "%s: %0.0f%%" % [l(:field_done_ratio), version.fixed_issues.visible.completed_percent]], - :legend => ('%0.0f%%' % version.fixed_issues.visible.completed_percent)) %> + ["%s: %0.0f%%" % [l(:label_closed_issues_plural), version.visible_fixed_issues.closed_percent], + "%s: %0.0f%%" % [l(:field_done_ratio), version.visible_fixed_issues.completed_percent]], + :legend => ('%0.0f%%' % version.visible_fixed_issues.completed_percent)) %>

- <%= link_to(l(:label_x_issues, :count => version.fixed_issues.visible.count), + <%= link_to(l(:label_x_issues, :count => version.visible_fixed_issues.count), version_filtered_issues_path(version, :status_id => '*')) %>   - (<%= link_to_if(version.fixed_issues.visible.closed_count > 0, - l(:label_x_closed_issues_abbr, :count => version.fixed_issues.visible.closed_count), + (<%= link_to_if(version.visible_fixed_issues.closed_count > 0, + l(:label_x_closed_issues_abbr, :count => version.visible_fixed_issues.closed_count), version_filtered_issues_path(version, :status_id => 'c')) %> — - <%= link_to_if(version.fixed_issues.visible.open_count > 0, - l(:label_x_open_issues_abbr, :count => version.fixed_issues.visible.open_count), + <%= link_to_if(version.visible_fixed_issues.open_count > 0, + l(:label_x_open_issues_abbr, :count => version.visible_fixed_issues.open_count), version_filtered_issues_path(version, :status_id => 'o')) %>)

<% else %> diff --git a/app/views/versions/show.html.erb b/app/views/versions/show.html.erb index 83953cce0..a62b0a1d0 100644 --- a/app/views/versions/show.html.erb +++ b/app/views/versions/show.html.erb @@ -12,12 +12,12 @@ <%= render(:partial => "wiki/content", :locals => {:content => @version.wiki_page.content}) if @version.wiki_page %>
-<% if @version.fixed_issues.visible.estimated_hours > 0 || User.current.allowed_to?(:view_time_entries, @project) %> +<% if @version.visible_fixed_issues.estimated_hours > 0 || User.current.allowed_to?(:view_time_entries, @project) %>
<%= l(:label_time_tracking) %> - <% if User.current.allowed_to_view_all_time_entries?(@project) %> -- 2.14.1
<%= l(:field_estimated_hours) %><%= link_to html_hours(l_hours(@version.fixed_issues.visible.estimated_hours)), + <%= link_to html_hours(l_hours(@version.visible_fixed_issues.estimated_hours)), project_issues_path(@version.project, :set_filter => 1, :status_id => '*', :fixed_version_id => @version.id, :c => [:tracker, :status, :subject, :estimated_hours], :t => [:estimated_hours]) %>