Skip to content

Commit

Permalink
Open source Web UI filtering (#6052)
Browse files Browse the repository at this point in the history
* Move filtering into OSS so we can use it with Metrics too

* extract english strings

* changes

* wording

* minor bump

* Modify inline testing's client override so as to not conflict with batch's client override, fixes #6057

* Previous commit fixes #6054

* tweaks
  • Loading branch information
mperham authored Sep 27, 2023
1 parent 1757a96 commit 8874e68
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 13 deletions.
10 changes: 6 additions & 4 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
HEAD
----------

- **BREAKING CHANGE** Error handlers now take three arguments `->(ex, context, config)`.
The previous calling convention will work until Sidekiq 8.0 but will print out a
deprecation warning.
- **FEATURE**: Job filtering within the Web UI. This feature has been open
sourced from Sidekiq Pro. [#6052]
- **API CHANGE** Error handlers now take three arguments `->(ex, context, config)`.
The previous calling convention will work until Sidekiq 8.0 but will print
out a deprecation warning. [#6051]
- Fix issue with the `batch_size` and `at` options in `S::Client.push_bulk` [#6040]
- Fix inline testing firing batch callbacks early [#6057]
- Fix log broadcast with Rails 7.1 [#6054]
- Use new log broadcast API in Rails 7.1 [#6054]

7.1.4
----------
Expand Down
50 changes: 50 additions & 0 deletions lib/sidekiq/web/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,56 @@ def self.set(key, val)
json Sidekiq::Stats.new.queues
end

########
# Filtering
get "/filter/retries" do
x = params[:substr]
return redirect "#{root_path}retries" unless x && x != ""

@retries = search(Sidekiq::RetrySet.new, params[:substr])
erb :retries
end

post "/filter/retries" do
x = params[:substr]
return redirect "#{root_path}retries" unless x && x != ""

@retries = search(Sidekiq::RetrySet.new, params[:substr])
erb :retries
end

get "/filter/scheduled" do
x = params[:substr]
return redirect "#{root_path}scheduled" unless x && x != ""

@scheduled = search(Sidekiq::ScheduledSet.new, params[:substr])
erb :scheduled
end

post "/filter/scheduled" do
x = params[:substr]
return redirect "#{root_path}scheduled" unless x && x != ""

@scheduled = search(Sidekiq::ScheduledSet.new, params[:substr])
erb :scheduled
end

get "/filter/dead" do
x = params[:substr]
return redirect "#{root_path}morgue" unless x && x != ""

@dead = search(Sidekiq::DeadSet.new, params[:substr])
erb :morgue
end

post "/filter/dead" do
x = params[:substr]
return redirect "#{root_path}morgue" unless x && x != ""

@dead = search(Sidekiq::DeadSet.new, params[:substr])
erb :morgue
end

def call(env)
action = self.class.match(env)
return [404, {Rack::CONTENT_TYPE => "text/plain", Web::X_CASCADE => "pass"}, ["Not Found"]] unless action
Expand Down
32 changes: 23 additions & 9 deletions lib/sidekiq/web/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,29 @@ def find_locale_files(lang)
locale_files.select { |file| file =~ /\/#{lang}\.yml$/ }
end

# This is a hook for a Sidekiq Pro feature. Please don't touch.
def filtering(*)
def search(jobset, substr)
resultset = jobset.scan(substr).to_a
@current_page = 1
@count = @total_size = resultset.size
resultset
end

def filtering(which)
erb(:filtering, locals: {which: which})
end

def filter_link(jid, within = "retries")
if within.nil?
::Rack::Utils.escape_html(jid)
else
"<a href='#{root_path}filter/#{within}?substr=#{jid}'>#{::Rack::Utils.escape_html(jid)}</a>"
end
end

def display_tags(job, within = "retries")
job.tags.map { |tag|
"<span class='label label-info jobtag'>#{filter_link(tag, within)}</span>"
}.join(" ")
end

# This view helper provide ability display you html code in
Expand Down Expand Up @@ -111,13 +132,6 @@ def locale
end
end

# within is used by Sidekiq Pro
def display_tags(job, within = nil)
job.tags.map { |tag|
"<span class='label label-info jobtag'>#{::Rack::Utils.escape_html(tag)}</span>"
}.join(" ")
end

# sidekiq/sidekiq#3243
def unfiltered?
yield unless env["PATH_INFO"].start_with?("/filter/")
Expand Down
47 changes: 47 additions & 0 deletions test/filtering_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative "helper"
require "sidekiq/web"
require "rack/test"

class WebWorker
include Sidekiq::Worker

def perform(a, b)
a + b
end
end

describe "filtering" do
before do
@config = reset!
end

include Rack::Test::Methods

def app
Sidekiq::Web
end

it "can filter jobs" do
jid1 = WebWorker.perform_in(5, "bob", "tammy")
jid2 = WebWorker.perform_in(5, "mike", "jim")

get "/scheduled"
assert_equal 200, last_response.status
assert_match(/#{jid1}/, last_response.body)
assert_match(/#{jid2}/, last_response.body)

post "/filter/scheduled", substr: "tammy"
assert_equal 200, last_response.status
assert_match(/#{jid1}/, last_response.body)
refute_match(/#{jid2}/, last_response.body)

post "/filter/scheduled", substr: ""
assert_equal 302, last_response.status
get "/filter/scheduled"
assert_equal 302, last_response.status
get "/filter/retries"
assert_equal 302, last_response.status
get "/filter/dead"
assert_equal 302, last_response.status
end
end
2 changes: 2 additions & 0 deletions web/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,5 @@ en:
Context: Context
Bucket: Bucket
NoJobMetricsFound: No recent job metrics were found
Filter: Filter
AnyJobContent: Any job content
7 changes: 7 additions & 0 deletions web/views/filtering.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="sm-col-3 pull-right" style="display: inline; margin: 25px 15px 0 0;">
<%= t('Filter') %>:
<form method="POST" action='<%= root_path %>filter/<%= which %>' style="display: inline-block">
<%= csrf_tag %>
<input class="search" type="search" name="substr" value="<%= h params[:substr] %>" placeholder="<%= t('AnyJobContent') %>"/>
</form>
</div>

0 comments on commit 8874e68

Please sign in to comment.