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 pagination to worker counts, preserves backwards compatibility #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/qless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def initialize(client)
@client = client
end

def counts
JSON.parse(@client.call('workers'))
def counts(offset = 0, count = 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If backwards compatibility is not an issue, the count should default to 25 like the rest of the paginated methods. When both offset and count are 0, it gets the entire data set

JSON.parse(@client.call('workers', offset, count))
end

def [](name)
Expand Down
5 changes: 3 additions & 2 deletions lib/qless/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def tracked
end

def workers
client.workers.counts
client.workers.counts(0, PAGE_SIZE)
end

def failed
Expand Down Expand Up @@ -242,7 +242,8 @@ def strftime(t)

get '/workers/?' do
erb :workers, layout: true, locals: {
title: 'Workers'
title: 'Workers',
workers: paginated(client.workers, :counts)
}
end

Expand Down
1 change: 1 addition & 0 deletions lib/qless/server/views/workers.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
</div>
</div>
<% end %>
<%= erb :_pagination, :layout => false %>
28 changes: 28 additions & 0 deletions spec/integration/qless_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ module Qless
})
end

it 'paginates access to worker stats' do
expect(client.workers.counts).to eq({})

3.times do |i|
client.worker_name = "worker-#{i}"
queue.put('Foo', {}, jid: "jid-#{i}")
queue.pop
end

worker_names = client.workers.counts().collect {|h| h['name'] }
expect(worker_names).to eq(['worker-2', 'worker-1', 'worker-0'])

worker_names = client.workers.counts(0, 0).collect {|h| h['name'] }
expect(worker_names).to eq(['worker-2', 'worker-1', 'worker-0'])

worker_names = client.workers.counts(0, 1).collect {|h| h['name'] }
expect(worker_names).to eq(['worker-2'])

worker_names = client.workers.counts(1, 1).collect {|h| h['name'] }
expect(worker_names).to eq(['worker-1'])

worker_names = client.workers.counts(2, 1).collect {|h| h['name'] }
expect(worker_names).to eq(['worker-0'])

worker_names = client.workers.counts(1, 2).collect {|h| h['name'] }
expect(worker_names).to eq(['worker-1', 'worker-0'])
end

it 'can deregister workers' do
# Ensure there's a worker listed
queue.put('Foo', {}, jid: 'jid')
Expand Down
31 changes: 22 additions & 9 deletions spec/integration/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ module Qless
end
end

def build_paginated_objects
def build_paginated_objects(pattern = 'jid-%d')
# build 30 since our page size is 25 so we have at least 2 pages
30.times do |i|
yield "jid-#{i + 1}"
yield pattern % (i + 1)
end
end

# We put periods on the end of these jids so that
# an assertion about "jid-1" will not pass if "jid-11"
# is on the page. The jids are formatted as "#{jid}..."
def assert_page(present_jid_num, absent_jid_num)
page.should have_content("jid-#{present_jid_num}.")
page.should_not have_content("jid-#{absent_jid_num}.")
def assert_page(present_id_num, absent_id_num, pattern = 'jid-%d.')
page.should have_content(pattern % present_id_num)
page.should_not have_content(pattern % absent_id_num)
end

def click_pagination_link(text)
Expand All @@ -66,14 +66,14 @@ def click_pagination_link(text)
end
end

def test_pagination(page_1_jid = 1, page_2_jid = 27)
assert_page page_1_jid, page_2_jid
def test_pagination(page_1_id = 1, page_2_id = 27, pattern = 'jid-%d.')
assert_page page_1_id, page_2_id, pattern

click_pagination_link 'Next'
assert_page page_2_jid, page_1_jid
assert_page page_2_id, page_1_id, pattern

click_pagination_link 'Prev'
assert_page page_1_jid, page_2_jid
assert_page page_1_id, page_2_id, pattern
end

it 'can paginate a group of tagged jobs' do
Expand Down Expand Up @@ -122,6 +122,19 @@ def test_pagination(page_1_jid = 1, page_2_jid = 27)
test_pagination
end

it 'can paginate the workers pages' do
build_paginated_objects('worker-%d') do |worker_name|
q.client.worker_name = worker_name
q.put(Qless::Job, {})
q.pop
end

visit '/workers'

# The workers page shows the workers in reverse order
test_pagination 30, 4, 'worker-%d'
end

it 'can see the root-level summary' do
visit '/'

Expand Down