lib/resque_cleaner/server.rb in resque-cleaner-0.2.12 vs lib/resque_cleaner/server.rb in resque-cleaner-0.3.0

- old
+ new

@@ -101,53 +101,60 @@ end def show_job_args(args) Array(args).map { |a| a.inspect }.join("\n") end + + def text_filter(id, name, value) + html = "<input id=\"#{id}\" type=\"text\" name=\"#{name}\" value=\"#{value}\">" + html += "</input>" + end end mime_type :json, 'application/json' get "/cleaner" do load_library load_cleaner_filter @jobs = cleaner.select - @stats, @total = {}, {"total" => 0, "1h" => 0, "3h" => 0, "1d" => 0, "3d" => 0, "7d" => 0} + @stats = { :klass => {}, :exception => {} } + @total = Hash.new(0) @jobs.each do |job| - klass = if job["payload"] && job["payload"]["class"] - job["payload"]["class"] - else - "UNKNOWN" - end + klass = job["payload"]["class"] || 'UNKNOWN' + exception = job["exception"] || 'UNKNOWN' failed_at = Time.parse job["failed_at"] + @stats[:klass][klass] ||= Hash.new(0) + @stats[:exception][exception] ||= Hash.new(0) - @stats[klass] ||= {"total" => 0, "1h" => 0, "3h" => 0, "1d" => 0, "3d" => 0, "7d" => 0} - items = [@stats[klass],@total] - - items.each{|a| a["total"] += 1} - items.each{|a| a["1h"] += 1} if failed_at >= hours_ago(1) - items.each{|a| a["3h"] += 1} if failed_at >= hours_ago(3) - items.each{|a| a["1d"] += 1} if failed_at >= hours_ago(24) - items.each{|a| a["3d"] += 1} if failed_at >= hours_ago(24*3) - items.each{|a| a["7d"] += 1} if failed_at >= hours_ago(24*7) + [ + @stats[:klass][klass], + @stats[:exception][exception], + @total + ].each do |stat| + stat[:total] += 1 + stat[:h1] += 1 if failed_at >= hours_ago(1) + stat[:h3] += 1 if failed_at >= hours_ago(3) + stat[:d1] += 1 if failed_at >= hours_ago(24) + stat[:d3] += 1 if failed_at >= hours_ago(24*3) + stat[:d7] += 1 if failed_at >= hours_ago(24*7) + end end erb File.read(ResqueCleaner::Server.erb_path('cleaner.erb')) end get "/cleaner_list" do load_library load_cleaner_filter + build_urls block = filter_block @failed = cleaner.select(&block).reverse - url = "cleaner_list?c=#{@klass}&ex=#{@exception}&f=#{@from}&t=#{@to}" - @dump_url = "cleaner_dump?c=#{@klass}&ex=#{@exception}&f=#{@from}&t=#{@to}" - @paginate = Paginate.new(@failed, url, params[:p].to_i) + @paginate = Paginate.new(@failed, @list_url, params[:p].to_i) @klasses = cleaner.stats_by_class.keys @exceptions = cleaner.stats_by_exception.keys @count = cleaner.select(&block).size @@ -155,10 +162,11 @@ end post "/cleaner_exec" do load_library load_cleaner_filter + build_urls if params[:select_all_pages]!="1" @sha1 = {} params[:sha1].split(",").each {|s| @sha1[s] = true } end @@ -170,11 +178,10 @@ when "clear" then cleaner.clear(&block) when "retry_and_clear" then cleaner.requeue(true,&block) when "retry" then cleaner.requeue(false,{},&block) end - @url = "cleaner_list?c=#{@klass}&ex=#{@exception}&f=#{@from}&t=#{@to}" erb File.read(ResqueCleaner::Server.erb_path('cleaner_exec.erb')) end get "/cleaner_dump" do load_library @@ -217,18 +224,33 @@ def load_cleaner_filter @from = params[:f]=="" ? nil : params[:f] @to = params[:t]=="" ? nil : params[:t] @klass = params[:c]=="" ? nil : params[:c] @exception = params[:ex]=="" ? nil : params[:ex] + @regex = params[:regex]=="" ? nil : params[:regex] end + def build_urls + params = { + c: @klass, + ex: @exception, + f: @from, + t: @to, + regex: @regex + }.map {|key,value| "#{key}=#{URI.encode(value.to_s)}"}.join("&") + + @list_url = "cleaner_list?#{params}" + @dump_url = "cleaner_dump?#{params}" + end + def filter_block block = lambda{|j| (!@from || j.after?(hours_ago(@from))) && (!@to || j.before?(hours_ago(@to))) && (!@klass || j.klass?(@klass)) && (!@exception || j.exception?(@exception)) && - (!@sha1 || @sha1[Digest::SHA1.hexdigest(j.to_json)]) + (!@sha1 || @sha1[Digest::SHA1.hexdigest(j.to_json)]) && + (!@regex || j.to_s =~ /#{@regex}/) } end def hours_ago(h) Time.now - h.to_i*60*60