Commit 5a8cf044493f7ae7a5096e0935332ccee7d245d3

Authored by Nathan Broadbent
1 parent 047df46a
Exists in master and in 1 other branch production

Reduce hits to the database for unresolved err count by caching values.

app/controllers/apps_controller.rb
... ... @@ -45,9 +45,12 @@ class AppsController < InheritedResources::Base
45 45  
46 46 protected
47 47 def collection
48   - # Sort apps by number of unresolved errs (highest number first)
  48 + # Sort apps by number of unresolved errs, descending.
  49 + # Caches the unresolved err counts while performing the sort.
  50 + @unresolved_counts = {}
49 51 @apps ||= end_of_association_chain.all.sort{|a,b|
50   - b.errs.unresolved.count <=> a.errs.unresolved.count
  52 + [a,b].each{|app| @unresolved_counts[app.id] ||= app.errs.unresolved.count }
  53 + @unresolved_counts[b.id] <=> @unresolved_counts[a.id]
51 54 }
52 55 end
53 56  
... ...
app/views/apps/index.html.haml
... ... @@ -15,7 +15,7 @@
15 15 %td.deploy= app.last_deploy_at ? link_to( app.last_deploy_at.to_s(:micro) << " (#{app.deploys.last.short_revision})", app_deploys_path(app)) : 'n/a'
16 16 %td.count
17 17 - if app.errs.count > 0
18   - - unresolved = app.errs.unresolved.count
  18 + - unresolved = @unresolved_counts[app.id]
19 19 = link_to unresolved, app_path(app), :class => (unresolved == 0 ? "resolved" : nil)
20 20 - else
21 21 \-
... ...