From 5a8cf044493f7ae7a5096e0935332ccee7d245d3 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Sat, 3 Sep 2011 23:53:05 +0800 Subject: [PATCH] Reduce hits to the database for unresolved err count by caching values. --- app/controllers/apps_controller.rb | 7 +++++-- app/views/apps/index.html.haml | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/controllers/apps_controller.rb b/app/controllers/apps_controller.rb index 3454cbc..89490aa 100644 --- a/app/controllers/apps_controller.rb +++ b/app/controllers/apps_controller.rb @@ -45,9 +45,12 @@ class AppsController < InheritedResources::Base protected def collection - # Sort apps by number of unresolved errs (highest number first) + # Sort apps by number of unresolved errs, descending. + # Caches the unresolved err counts while performing the sort. + @unresolved_counts = {} @apps ||= end_of_association_chain.all.sort{|a,b| - b.errs.unresolved.count <=> a.errs.unresolved.count + [a,b].each{|app| @unresolved_counts[app.id] ||= app.errs.unresolved.count } + @unresolved_counts[b.id] <=> @unresolved_counts[a.id] } end diff --git a/app/views/apps/index.html.haml b/app/views/apps/index.html.haml index 0bf6688..f5d476f 100644 --- a/app/views/apps/index.html.haml +++ b/app/views/apps/index.html.haml @@ -15,7 +15,7 @@ %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' %td.count - if app.errs.count > 0 - - unresolved = app.errs.unresolved.count + - unresolved = @unresolved_counts[app.id] = link_to unresolved, app_path(app), :class => (unresolved == 0 ? "resolved" : nil) - else \- -- libgit2 0.21.2