加快我的rails应用程序

时间:2013-06-19 08:14:52

标签: mysql ruby-on-rails json performance

我有一个非常大的rails应用程序,它运行得很好,但是有一个非常慢的页面!完成装载需要2分钟。 该页面包含一个日历,我在此方法中使用Json请求插入大量约会。

def get_ticket

## get days between event and -10 days and + 10 days

cstart = ((Time.at(params[:start].to_i).to_datetime)-10).to_i  # assign to cstart the start date converted into integer
cend = ((Time.at(params[:end].to_i).to_datetime)+10).to_i  # assign to cend the end date converted into integer
name = params[:search] # assign to name the param search
option = params[:type] # assign to option the param type


unless current_user.roles[0].name == "admin"   
  if(name == nil && option == nil)
    option = "default_user_search"
    name = current_user.district.name
  end
end


@data = [] # @data is an empty array

@calendar = Calendar.where("startdate <= #{cstart} AND enddate < #{cend}")
@calendar.each do |i|                                                     
  if(i.tickets == [])
    if(current_user.roles[0].name == "cse")# || current_user.roles[0].name == "ase")
      if(i.user == current_user)
        @data <<  {type: "user" , data: UserJson.to_h(i.id,(i.startdate-7200),(i.enddate-7200),i.user,i.status)}
      end
    else
      if(option != nil)
        if(i.user.district == District.find_by_name(name))
          @data <<  {type: "user" , data: UserJson.to_h(i.id,(i.startdate-7200),(i.enddate-7200),i.user,i.status)}
        end
      else
        @data <<  {type: "user" , data: UserJson.to_h(i.id,(i.startdate-7200),(i.enddate-7200),i.user,i.status)}
      end
    end
  else
    if(current_user.roles[0].name == "cse")# || current_user.roles[0].name == "ase")
      if(i.tickets[0].workers.include? current_user)
    p i.tickets[0].percentage
        @data <<  {type: "ticket", data: TicketJson.to_h((i.startdate-7200),(i.enddate-7200),i.tickets,i.tickets[0].percentage.to_i,i.tickets[0].recruiter.surname)}         
      end

    else
      if(option != nil)
        i.tickets[0].workers.each do |worker|

          if(worker.district.name ==  District.find_by_name(name).name)
    p i.tickets[0].percentage
            @data <<  {type: "ticket", data: TicketJson.to_h((i.startdate-7200),(i.enddate-7200),i.tickets,i.tickets[0].percentage.to_i, i.tickets[0].recruiter.surname)}         
            break
          end
        end
      else
    p i.tickets[0].percentage
        @data <<  {type: "ticket", data: TicketJson.to_h((i.startdate-7200),(i.enddate-7200),i.tickets,i.tickets[0].percentage.to_i, i.tickets[0].recruiter.surname)}         
      end
    end
  end
end   
respond_to do |ff|                            # The request can be either:
  ff.html { render :json => @data}            # html, with a json render of @data   
  ff.json { render :json => @data}            # json, with a json render of @data
end
end

我真的不知道如何加快这个动作,如果你需要其他一些代码就问我。

希望有人可以帮助我,谢谢。

0 个答案:

没有答案