Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add queues for stats and as a filter option #34

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/resque_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ def stats_by_date(&block)
stats
end

# Stats by queue.
def stats_by_queue(&block)
jobs, stats = select(&block), {}
jobs.each do |job|
queue = job["queue"] || "UNKNOWN"
stats[queue] ||= 0
stats[queue] += 1
end

print_stats(stats) if print?
stats
end

# Stats by class.
def stats_by_class(&block)
jobs, stats = select(&block), {}
Expand Down
18 changes: 17 additions & 1 deletion lib/resque_cleaner/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ def time_filter(id, name, value)
html += "</select>"
end

def queue_filter(id, name, queues, value)
html = "<select id=\"#{id}\" name=\"#{name}\">"
html += "<option value=\"\">-</option>"
queues.each do |q|
selected = q == value ? 'selected="selected"' : ''
html += "<option #{selected} value=\"#{q}\">#{q}</option>"
end
html += "</select>"
end

def class_filter(id, name, klasses, value)
html = "<select id=\"#{id}\" name=\"#{name}\">"
html += "<option value=\"\">-</option>"
Expand Down Expand Up @@ -117,18 +127,21 @@ def text_filter(id, name, value)
load_cleaner_filter

@jobs = cleaner.select
@stats = { :klass => {}, :exception => {} }
@stats = { klass: {}, exception: {}, queue: {} }
@total = Hash.new(0)
@jobs.each do |job|
klass = job["payload"]["class"] || 'UNKNOWN'
exception = job["exception"] || 'UNKNOWN'
queue = job["queue"] || 'UNKNOWN'
failed_at = Time.parse job["failed_at"]
@stats[:klass][klass] ||= Hash.new(0)
@stats[:exception][exception] ||= Hash.new(0)
@stats[:queue][queue] ||= Hash.new(0)

[
@stats[:klass][klass],
@stats[:exception][exception],
@stats[:queue][queue],
@total
].each do |stat|
stat[:total] += 1
Expand Down Expand Up @@ -156,6 +169,7 @@ def text_filter(id, name, value)

@klasses = cleaner.stats_by_class.keys
@exceptions = cleaner.stats_by_exception.keys
@queues = cleaner.stats_by_queue.keys
@count = cleaner.select(&block).size

erb File.read(ResqueCleaner::Server.erb_path('cleaner_list.erb'))
Expand Down Expand Up @@ -226,6 +240,7 @@ def load_cleaner_filter
@to = params[:t]=="" ? nil : params[:t]
@klass = params[:c]=="" ? nil : params[:c]
@exception = params[:ex]=="" ? nil : params[:ex]
@queue = params[:q]=="" ? nil : params[:q]
@regex = params[:regex]=="" ? nil : params[:regex]
end

Expand All @@ -247,6 +262,7 @@ def filter_block
(!@from || j.after?(hours_ago(@from))) &&
(!@to || j.before?(hours_ago(@to))) &&
(!@klass || j.klass?(@klass)) &&
(!@queue || j.queue?(@queue)) &&
(!@exception || j.exception?(@exception)) &&
(!@sha1 || @sha1[Digest::SHA1.hexdigest(j.to_json)]) &&
(!@regex || j.to_s =~ /#{@regex}/)
Expand Down
9 changes: 9 additions & 0 deletions lib/resque_cleaner/server/views/cleaner.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
locals: {q: 'ex', type: 'exception'}
) %>

<div class="title clearfix">
<h1>Group by queue</h1>
</div>

<%= erb(
File.read(ResqueCleaner::Server.erb_path("_stats.erb")),
locals: {q: 'q', type: 'queue'}
) %>

<% if @cleaner.limiter.on? %>
<%= erb File.read(ResqueCleaner::Server.erb_path("_limiter.erb")) %>
<% end %>
Expand Down
3 changes: 3 additions & 0 deletions lib/resque_cleaner/server/views/cleaner_list.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<span class="exception_filter">
Exception: <%= exception_filter("filter_class","ex",@exceptions,@exception)%>
</span>
<span class="queue_filter">
Queue: <%= queue_filter("filter_class","q",@queues,@queue)%>
</span>
<span class="time_filter">
From: <%= time_filter("filter_from","f",@from)%>
</span>
Expand Down
6 changes: 6 additions & 0 deletions test/resque_cleaner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@
assert_equal 7, ret['SyntaxError']
end

it "#stats_by_queue returns stats grouped by queue" do
ret = @cleaner.stats_by_queue
assert_equal 22, ret['jobs']
assert_equal 20, ret['jobs2']
end

it "#lock ensures that a new failure job doesn't affect in a limit mode" do
@cleaner.limiter.maximum = 23
@cleaner.limiter.lock do
Expand Down
2 changes: 2 additions & 0 deletions test/resque_web_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def setup_some_failed_jobs
get "/cleaner"
assert last_response.body.include?('BadJob')
assert last_response.body =~ /\bException\b/
assert last_response.body =~ /\bQueue\b/
end

it "#cleaner_list should respond with success" do
Expand All @@ -44,6 +45,7 @@ def setup_some_failed_jobs

it '#cleaner_list shows the failed jobs' do
get "/cleaner_list"
File.open(File.join(File.dirname(__FILE__),'..','tmp','response.html'), 'w') { |f| f.write last_response.body }
assert last_response.body.include?('BadJob')
end

Expand Down