0

这里有人会碰巧知道以下代码是否能够在运行后导致处理器严重消耗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
4

1 回答 1

1

仍然有点难以看到发生了什么,但我假设 self.jobs 基于 ActiveRecord 关联。当您使用self.jobs.select { ... }它时,它会从数据库中加载所有作业,然后遍历所有作业以确定要提取哪些作业。您最好使用那里的数据库查询来选择您想要的记录。假设您使用的是 Rails 3,更改selectwhere应该是正确的方向。如果您使用的是 Rails 2,则需要将其更改为:conditions.

self.jobs.where('completed_on > ? AND completed_on < ? AND completed_on IS NOT NULL' , Time.zone.now.beginning_of_day, Time.zone.now.end_of_day).map(&:total).sum
于 2012-04-26T17:59:50.427 回答