From 8243c55f2523c9225149bccf95f547b771c8fa99 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Sun, 8 Sep 2019 00:49:28 +0300 Subject: [PATCH] Add ability to sort 'Enqueued' page on Web UI by enqueued_at time --- Changes.md | 1 + lib/sidekiq/paginator.rb | 9 +++++++-- lib/sidekiq/web/application.rb | 2 +- lib/sidekiq/web/helpers.rb | 6 +++++- test/test_web.rb | 24 ++++++++++++++++++++---- web/locales/en.yml | 1 + web/locales/ru.yml | 1 + web/locales/uk.yml | 1 + web/views/queue.erb | 5 +++++ 9 files changed, 42 insertions(+), 8 deletions(-) diff --git a/Changes.md b/Changes.md index d01dbb54..4b6239b9 100644 --- a/Changes.md +++ b/Changes.md @@ -5,6 +5,7 @@ HEAD --------- +- Add ability to sort 'Enqueued' page on Web UI by enqueued_at time [#4248] - Support `Client.push_bulk` with different delays [#4243] ```ruby Sidekiq::Client.push_bulk("class" => FooJob, "args" => [[1], [2]], "at" => [1.minute.from_now.to_f, 5.minutes.from_now.to_f]) diff --git a/lib/sidekiq/paginator.rb b/lib/sidekiq/paginator.rb index cfb5035b..c1eacb43 100644 --- a/lib/sidekiq/paginator.rb +++ b/lib/sidekiq/paginator.rb @@ -12,10 +12,10 @@ module Sidekiq Sidekiq.redis do |conn| type = conn.type(key) + rev = opts && opts[:reverse] case type when "zset" - rev = opts && opts[:reverse] total_size, items = conn.multi { conn.zcard(key) if rev @@ -28,8 +28,13 @@ module Sidekiq when "list" total_size, items = conn.multi { conn.llen(key) - conn.lrange(key, starting, ending) + if rev + conn.lrange(key, -ending - 1, -starting - 1) + else + conn.lrange(key, starting, ending) + end } + items.reverse! if rev [current_page, total_size, items] when "none" [1, 0, []] diff --git a/lib/sidekiq/web/application.rb b/lib/sidekiq/web/application.rb index 435157d2..8326e775 100644 --- a/lib/sidekiq/web/application.rb +++ b/lib/sidekiq/web/application.rb @@ -84,7 +84,7 @@ module Sidekiq @count = (params["count"] || 25).to_i @queue = Sidekiq::Queue.new(@name) - (@current_page, @total_size, @messages) = page("queue:#{@name}", params["page"], @count) + (@current_page, @total_size, @messages) = page("queue:#{@name}", params["page"], @count, reverse: params["direction"] == "asc") @messages = @messages.map { |msg| Sidekiq::Job.new(msg, @name) } erb(:queue) diff --git a/lib/sidekiq/web/helpers.rb b/lib/sidekiq/web/helpers.rb index cdb8b831..7c22fded 100644 --- a/lib/sidekiq/web/helpers.rb +++ b/lib/sidekiq/web/helpers.rb @@ -130,6 +130,10 @@ module Sidekiq end end + def sort_direction_label + params[:direction] == "asc" ? "↑" : "↓" + end + def workers @workers ||= Sidekiq::Workers.new end @@ -189,7 +193,7 @@ module Sidekiq [score.to_f, jid] end - SAFE_QPARAMS = %w[page poll] + SAFE_QPARAMS = %w[page poll direction] # Merge options with current params, filter safe params, and stringify to query string def qparams(options) diff --git a/test/test_web.rb b/test/test_web.rb index f70429fd..e8fe85c0 100644 --- a/test/test_web.rb +++ b/test/test_web.rb @@ -134,9 +134,25 @@ describe Sidekiq::Web do assert_equal 200, last_response.status end + it 'can sort on enqueued_at column' do + Sidekiq.redis do |conn| + (1000..1005).each do |i| + conn.lpush('queue:default', Sidekiq.dump_json(args: [i], enqueued_at: Time.now.to_i + i)) + end + end + + get '/queues/default?count=3' # direction is 'desc' by default + assert_match(/1005/, last_response.body) + refute_match(/1002/, last_response.body) + + get '/queues/default?count=3&direction=asc' + assert_match(/1000/, last_response.body) + refute_match(/1003/, last_response.body) + end + it 'can delete a queue' do Sidekiq.redis do |conn| - conn.rpush('queue:foo', '{\"args\":[]}') + conn.rpush('queue:foo', "{\"args\":[],\"enqueued_at\":1567894960}") conn.sadd('queues', 'foo') end @@ -154,9 +170,9 @@ describe Sidekiq::Web do it 'can delete a job' do Sidekiq.redis do |conn| - conn.rpush('queue:foo', "{\"args\":[]}") - conn.rpush('queue:foo', "{\"foo\":\"bar\",\"args\":[]}") - conn.rpush('queue:foo', "{\"foo2\":\"bar2\",\"args\":[]}") + conn.rpush('queue:foo', '{"args":[],"enqueued_at":1567894960}') + conn.rpush('queue:foo', '{"foo":"bar","args":[],"enqueued_at":1567894960}') + conn.rpush('queue:foo', '{"foo2":"bar2","args":[],"enqueued_at":1567894960}') end get '/queues/foo' diff --git a/web/locales/en.yml b/web/locales/en.yml index 115e6cc5..35224cf0 100644 --- a/web/locales/en.yml +++ b/web/locales/en.yml @@ -77,5 +77,6 @@ en: # <---- change this to your locale code Plugins: Plugins NotYetEnqueued: Not yet enqueued CreatedAt: Created At + EnqueuedAt: Enqueued At BackToApp: Back to App Latency: Latency diff --git a/web/locales/ru.yml b/web/locales/ru.yml index 842df07a..c8efa8fd 100644 --- a/web/locales/ru.yml +++ b/web/locales/ru.yml @@ -75,4 +75,5 @@ ru: Plugins: Плагины NotYetEnqueued: Пока не в очереди CreatedAt: Создан + EnqueuedAt: Поставлен в очередь BackToApp: Назад diff --git a/web/locales/uk.yml b/web/locales/uk.yml index 3bb14abe..d8b84226 100644 --- a/web/locales/uk.yml +++ b/web/locales/uk.yml @@ -74,3 +74,4 @@ uk: PollingInterval: Інтервал опитування Plugins: Плагіни NotYetEnqueued: Ще не в черзі + EnqueuedAt: Відправлено до черги diff --git a/web/views/queue.erb b/web/views/queue.erb index f451bf75..3dfe2879 100644 --- a/web/views/queue.erb +++ b/web/views/queue.erb @@ -17,6 +17,8 @@ <%= t('Job') %> <%= t('Arguments') %> + + <%= t('EnqueuedAt') %> <%= sort_direction_label %> <% @messages.each_with_index do |msg, index| %> @@ -32,6 +34,9 @@ <%= display_args(msg.display_args) %> <% end %> + + <%= relative_time(msg.enqueued_at) %> +
<%= csrf_tag %>