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

[#113] Filter workers by their last job status #173

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## HEAD

## v2.1.0
* 31.05.2023: Add a new endpoint (/sidekiq/api/statistic_by_last_job_status.json) *Rhian Moraes*
* 31.05.2023: /sidekiq/statistic: add two checkboxes to show/hide workers based on their last job status (#113) *Rhian Moraes*

## v2.0.0

#### BREAK
Expand Down
9 changes: 9 additions & 0 deletions lib/sidekiq/statistic/statistic/workers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ def display
end
end

def display_by_last_status
filtered_workers = display.group_by { |worker| worker[:last_job_status] }

filtered_workers['passed'] ||= []
filtered_workers['failed'] ||= []

filtered_workers
end

def display_per_day(worker_name)
statistic_hash.flat_map do |day|
day.reject{ |_, workers| workers.empty? }.map do |date, workers|
Expand Down
7 changes: 6 additions & 1 deletion lib/sidekiq/statistic/views/statistic.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</div>

<h2><%= t('WorkersTable') %></h2>
<div id="status_filters_checkboxes">
<p><strong><%= t('LastJobStatus') %>:</strong></p>
<p><input type="checkbox" id="show-passed" checked> <%= t('Passed') %></p>
<p><input type="checkbox" id="show-failed" checked> <%= t('Failed') %></p>
</div>
<table class="statistic__table table table-hover table-bordered table-striped table-white live-reload">
<thead>
<th><%= t('Worker') %></th>
Expand All @@ -56,7 +61,7 @@
</thead>
<tbody>
<% @all_workers.each do |worker| %>
<tr>
<tr class="worker-<%= worker[:last_job_status] %>">
<td>
<a href="<%= root_path %>statistic/<%= worker[:name] %>"><%= worker[:name] %></a>
</td>
Expand Down
13 changes: 13 additions & 0 deletions lib/sidekiq/statistic/views/statistic.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lib/sidekiq/statistic/views/styles/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
float: right;
}

/* === INPUT LIST === */

#status_filters_checkboxes {
display: flex;
gap: 2em;
}

#status_filters_checkboxes p {
display: inline;
}


/* === TABLES === */

.statistic__table > tbody > tr > td {
Expand Down
5 changes: 5 additions & 0 deletions lib/sidekiq/statistic/web_api_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def self.registered(app)
Sidekiq.dump_json(workers: statistic.display)
end

app.get '/api/statistic_by_last_job_status.json' do
statistic = Sidekiq::Statistic::Workers.new(*calculate_date_range(params))
Sidekiq.dump_json(status: statistic.display_by_last_status)
end

app.get '/api/statistic/:worker.json' do
worker_statistic =
Sidekiq::Statistic::Workers
Expand Down
19 changes: 19 additions & 0 deletions test/test_sidekiq/statistic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ module Statistic
end
end

describe '#display_by_last_status' do
it 'return workers separated by their last status' do
middlewared {}

subject = statistic.display_by_last_status

_(subject).must_be_instance_of Hash
assert_equal subject.keys.sort,
%w[passed failed].sort

_(subject['passed']).must_be_instance_of Array
_(subject['failed']).must_be_instance_of Array
assert_equal subject['passed'][0].keys.sort,
%i[name last_job_status number_of_calls queue runtime].sort

assert_equal worker, subject['passed'][0][:name]
end
end

describe '#display_per_day' do
it 'return workers job per day' do
middlewared {}
Expand Down
56 changes: 56 additions & 0 deletions test/test_sidekiq/web_api_extension_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,62 @@ def app
end
end

describe 'GET /api/statistic_by_last_job_status.json' do
describe 'without jobs' do
it 'returns empty workers statistic' do
get '/api/statistic_by_last_job_status.json'

response = Sidekiq.load_json(last_response.body)
_(response['status']).must_be_instance_of Hash
_(response['status']['passed']).must_equal []
_(response['status']['failed']).must_equal []
end
end

describe 'for perfomed jobs' do
it 'returns workers statistic filtered by state' do
middlewared {}
get '/api/statistic_by_last_job_status.json'

response = Sidekiq.load_json(last_response.body)
_(response['status']).must_be_instance_of Hash
_(response['status']['passed']).wont_equal []
_(response['status']['failed']).must_equal []

_(response['status']['passed'].first.keys).must_equal %w[name last_job_status number_of_calls queue runtime]
end
end

describe 'for any range' do
before do
middlewared {}
end

describe 'for date range with empty statistic' do
it 'returns empty statistic' do
get '/api/statistic_by_last_job_status.json?dateFrom=2015-07-28&dateTo=2015-07-29'

response = Sidekiq.load_json(last_response.body)
_(response['status']).must_be_instance_of Hash
_(response['status']['passed']).must_equal []
_(response['status']['failed']).must_equal []
end
end

describe 'for any date range with existed statistic' do
it 'returns workers statistic filtered by state' do
get "/api/statistic_by_last_job_status.json?dateFrom=2015-07-28&dateTo=#{Date.today}"

response = Sidekiq.load_json(last_response.body)
_(response['status']).must_be_instance_of Hash
_(response['status']['passed']).wont_equal []
_(response['status']['passed'].count).must_equal 1
_(response['status']['failed']).must_equal []
end
end
end
end

describe 'GET /api/statistic/:worker.json' do
describe 'without jobs' do
it 'returns empty workers statistic' do
Expand Down