Would anyone here happen to know if the following code is capable of causing an awful drain on the processor behind running rake test ?
def calc_yesterday_count
self.jobs.select{|a| a.completed_on > Time.zone.now.yesterday.beginning_of_day && a.completed_on < Time.zone.now.yesterday.end_of_day if a.completed_on != nil}.length
end
def calc_yesterday_sum
self.jobs.select{|a| a.completed_on > Time.zone.now.yesterday.beginning_of_day && a.completed_on < Time.zone.now.yesterday.end_of_day if a.completed_on != nil}.map(&:total).sum
end
def calc_today_count
self.jobs.select{|a| a.completed_on > Time.zone.now.beginning_of_day && a.completed_on < Time.zone.now.end_of_day if a.completed_on != nil}.length
end
def calc_today_sum
self.jobs.select{|a| a.completed_on > Time.zone.now.beginning_of_day && a.completed_on < Time.zone.now.end_of_day if a.completed_on != nil}.map(&:total).sum
end
def calc_week_count
self.jobs.select{|a| a.completed_on > Time.zone.now.beginning_of_week && a.completed_on < Time.zone.now.end_of_week if a.completed_on != nil}.length
end
def calc_week_sum
self.jobs.select{|a| a.completed_on > Time.zone.now.beginning_of_week && a.completed_on < Time.zone.now.end_of_week if a.completed_on != nil}.map(&:total).sum
end
def calc_month_count
self.jobs.select{|a| a.completed_on > Time.zone.now.beginning_of_month && a.completed_on < Time.zone.now.end_of_month if a.completed_on != nil}.length
end
def calc_month_sum
self.jobs.select{|a| a.completed_on > Time.zone.now.beginning_of_month && a.completed_on < Time.zone.now.end_of_month if a.completed_on != nil}.map(&:total).sum
end
def calc_year_count
self.jobs.select{|a| a.completed_on > Time.zone.now.beginning_of_year && a.completed_on < Time.zone.now.end_of_year if a.completed_on != nil}.length
end
def calc_year_sum
self.jobs.select{|a| a.completed_on > Time.zone.now.beginning_of_year && a.completed_on < Time.zone.now.end_of_year if a.completed_on != nil}.map(&:total).sum
end
def calc_open_estimates_sum
self.jobs.uncompleted.select{|a|a.which != 'job'}.map(&:total).sum
end
def calc_jobs_in_progress_sum
self.jobs.non_estimate.uncompleted.select{|a| a.scheduled_on < Time.zone.now if a.scheduled_on != nil}.map(&:total).sum
end
alias :account_stats_method :account_stats
#lazy build account stats
def build_account_stats
@estimates_in_progress = self.jobs.completed.select{|a|a.which != 'job'}.map(&:total).sum
@jobs_in_progress = self.jobs.non_estimate.uncompleted.select{|a| a.scheduled_on < Time.zone.now if a.scheduled_on != nil}.map(&:total).sum
@account_stats = {
:account_id => self.id,
:yesterday_count => self.calc_yesterday_count,
:yesterday_total => self.calc_yesterday_sum,
:today_count => self.calc_today_count,
:today_total => self.calc_today_sum,
:week_count => self.calc_week_count,
:week_total => self.calc_week_sum,
:month_count => self.calc_month_count,
:month_total => self.calc_month_sum,
:year_count => self.calc_year_count,
:year_total => self.calc_year_sum
}
self.create_account_stats(@account_stats)
end
def account_stats
if account_stats_method.nil?
build_account_stats
else
account_stats_method
end
end
It’s still a little hard to see what is going on, but I’m assuming self.jobs is based on an ActiveRecord association. When you use
self.jobs.select { ... }it’s loading all the jobs from the database and then iterating over all of them to figure out which ones to pull out. You’d be better off using database queries there to select the records you want. Changingselecttowhereshould be the right direction to go, assuming you’re using Rails 3. if you’re on Rails 2, you will need to change this to:conditions.