module Sidekiq::WebHelpers
These methods are available to pages within the Web
UI and UI extensions. They are not public APIs for applications to use.
Constants
- RETRY_JOB_KEYS
- SAFE_QPARAMS
Public Instance Methods
Source
# File lib/sidekiq/web/helpers.rb, line 151 def add_to_head @head_html ||= [] @head_html << yield.dup if block_given? end
This view helper provide ability display you html code in to head of page. Example:
<% add_to_head do %> <link rel="stylesheet" .../> <meta .../> <% end %>
Source
# File lib/sidekiq/web/helpers.rb, line 106 def available_locales @@available_locales ||= Set.new(locale_files.map { |path| File.basename(path, ".yml") }) end
Source
# File lib/sidekiq/web/helpers.rb, line 259 def busy_weights(capsule_weights) # backwards compat with 7.0.0, remove in 7.1 cw = [capsule_weights].flatten cw.map { |hash| hash.map { |name, weight| (weight > 0) ? +name << ": " << weight.to_s : name }.join(", ") }.join("; ") end
Source
# File lib/sidekiq/web/helpers.rb, line 94 def clear_caches @@strings = nil @@locale_files = nil @@available_locales = nil end
Source
# File lib/sidekiq/web/helpers.rb, line 339 def csrf_tag "<input type='hidden' name='authenticity_token' value='#{env[:csrf_token]}'/>" end
Source
# File lib/sidekiq/web/helpers.rb, line 285 def current_path @current_path ||= request.path_info.gsub(/^\//, "") end
Source
# File lib/sidekiq/web/helpers.rb, line 289 def current_status (workset.size == 0) ? "idle" : "active" end
Source
# File lib/sidekiq/web/helpers.rb, line 438 def delete_or_add_queue(job, params) if params["delete"] job.delete elsif params["add_to_queue"] job.add_to_queue end end
Source
# File lib/sidekiq/web/helpers.rb, line 326 def display_args(args, truncate_after_chars = 2000) return "Invalid job payload, args is nil" if args.nil? return "Invalid job payload, args must be an Array, not #{args.class.name}" unless args.is_a?(Array) begin args.map { |arg| h(truncate(to_display(arg), truncate_after_chars)) }.join(", ") rescue "Illegal job arguments: #{h args.inspect}" end end
Source
# File lib/sidekiq/web/helpers.rb, line 156 def display_custom_head @head_html.join if defined?(@head_html) end
Source
# File lib/sidekiq/web/helpers.rb, line 409 def environment_title_prefix environment = Sidekiq.default_configuration[:environment] || ENV["APP_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" "[#{environment.upcase}] " unless environment == "production" end
Source
# File lib/sidekiq/web/helpers.rb, line 129 def filter_link(jid, within = "retries") if within.nil? ::Rack::Utils.escape_html(jid) else "<a href='#{root_path}#{within}?substr=#{jid}'>#{::Rack::Utils.escape_html(jid)}</a>" end end
Source
# File lib/sidekiq/web/helpers.rb, line 125 def filtering(which) erb(:filtering, locals: {which: which}) end
Source
# File lib/sidekiq/web/helpers.rb, line 110 def find_locale_files(lang) locale_files.select { |file| file =~ /\/#{lang}\.yml$/ } end
Source
# File lib/sidekiq/web/helpers.rb, line 372 def format_memory(rss_kb) return "0" if rss_kb.nil? || rss_kb == 0 if rss_kb < 100_000 "#{number_with_delimiter(rss_kb)} KB" elsif rss_kb < 10_000_000 "#{number_with_delimiter((rss_kb / 1024.0).to_i)} MB" else "#{number_with_delimiter(rss_kb / (1024.0 * 1024.0), precision: 1)} GB" end end
Source
# File lib/sidekiq/web/helpers.rb, line 389 def h(text) ::Rack::Utils.escape_html(text.to_s) rescue ArgumentError => e raise unless e.message.eql?("invalid byte sequence in UTF-8") text.encode!("UTF-16", "UTF-8", invalid: :replace, replace: "").encode!("UTF-8", "UTF-16") retry end
Source
# File lib/sidekiq/web/helpers.rb, line 298 def job_params(job, score) "#{score}-#{job["jid"]}" end
Source
# File lib/sidekiq/web/helpers.rb, line 114 def language_name(locale) strings(locale).fetch("LanguageName", locale) end
Source
# File lib/sidekiq/web/helpers.rb, line 186 def locale # session[:locale] is set via the locale selector from the footer @locale ||= if (l = session&.fetch(:locale, nil)) && available_locales.include?(l) l else matched_locale = nil # Attempt to find a case-insensitive exact match first user_preferred_languages.each do |preferred| # We only care about the language and primary subtag # "en-GB-oxendict" becomes "en-GB" language_tag = preferred.split("-")[0..1].join("-") matched_locale = available_locales.find { |available_locale| available_locale.casecmp?(language_tag) } break if matched_locale end return matched_locale if matched_locale # Find the first base language match # "en-US,es-MX;q=0.9" matches "en" user_preferred_languages.each do |preferred| base_language = preferred.split("-", 2).first matched_locale = available_locales.find { |available_locale| available_locale.casecmp?(base_language) } break if matched_locale end matched_locale || "en" end end
Given an Accept-Language header like “fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2” this method will try to best match the available locales to the user’s preferred languages.
Source
# File lib/sidekiq/web/helpers.rb, line 100 def locale_files @@locale_files ||= config.locales.flat_map { |path| Dir["#{path}/*.yml"] } end
Source
# File lib/sidekiq/web/helpers.rb, line 384 def number_with_delimiter(number, options = {}) precision = options[:precision] || 0 %(<span data-nwp="#{precision}">#{number.round(precision)}</span>) end
Source
# File lib/sidekiq/web/helpers.rb, line 302 def parse_key(key) score, jid = key.split("-", 2) [score.to_f, jid] end
Source
# File lib/sidekiq/web/helpers.rb, line 423 def pollable? # there's no point to refreshing the metrics pages every N seconds !(current_path == "" || current_path.index("metrics")) end
Source
# File lib/sidekiq/web/helpers.rb, line 242 def processes @processes ||= Sidekiq::ProcessSet.new end
Source
# File lib/sidekiq/web/helpers.rb, line 415 def product_version "Sidekiq v#{Sidekiq::VERSION}" end
Source
# File lib/sidekiq/web/helpers.rb, line 310 def qparams(options) stringified_options = options.transform_keys(&:to_s) to_query_string(request.params.merge(stringified_options)) end
Merge options with current params, filter safe params, and stringify to query string
Source
# File lib/sidekiq/web/helpers.rb, line 399 def redirect_with_query(url) r = request.referer if r && r =~ /\?/ ref = URI(r) redirect("#{url}?#{ref.query}") else redirect url end end
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
Source
# File lib/sidekiq/web/helpers.rb, line 277 def redis_info @info ||= Sidekiq.default_configuration.redis_info end
Source
# File lib/sidekiq/web/helpers.rb, line 271 def redis_url Sidekiq.redis do |conn| conn.config.server_url end end
Source
# File lib/sidekiq/web/helpers.rb, line 293 def relative_time(time) stamp = time.getutc.iso8601 %(<time class="ltr" dir="ltr" title="#{stamp}" datetime="#{stamp}">#{time}</time>) end
Source
# File lib/sidekiq/web/helpers.rb, line 364 def retry_extra_items(retry_job) @retry_extra_items ||= {}.tap do |extra| retry_job.item.each do |key, value| extra[key] = value unless RETRY_JOB_KEYS.include?(key) end end end
Source
# File lib/sidekiq/web/helpers.rb, line 428 def retry_or_delete_or_kill(job, params) if params["retry"] job.retry elsif params["delete"] job.delete elsif params["kill"] job.kill end end
Source
# File lib/sidekiq/web/helpers.rb, line 281 def root_path "#{env["SCRIPT_NAME"]}/" end
Source
# File lib/sidekiq/web/helpers.rb, line 40 def script_tag(location, **kwargs) global = location.match?(/:\/\//) location = root_path + location if !global && !location.start_with?(root_path) attrs = { type: "text/javascript", nonce: csp_nonce, src: location } html_tag(:script, attrs.merge(kwargs)) {} end
Source
# File lib/sidekiq/web/helpers.rb, line 118 def search(jobset, substr) resultset = jobset.scan(substr).to_a @current_page = 1 @count = @total_size = resultset.size resultset end
Source
# File lib/sidekiq/web/helpers.rb, line 419 def server_utc_time Time.now.utc.strftime("%H:%M:%S UTC") end
Source
# File lib/sidekiq/web/helpers.rb, line 86 def singularize(str, count) if count == 1 && str.respond_to?(:singularize) # rails str.singularize else str end end
Source
# File lib/sidekiq/web/helpers.rb, line 234 def sort_direction_label (url_params("direction") == "asc") ? "↑" : "↓" end
Source
# File lib/sidekiq/web/helpers.rb, line 247 def sorted_processes @sorted_processes ||= begin return processes unless processes.all? { |p| p["hostname"] } processes.to_a.sort_by do |process| # Kudos to `shurikk` on StackOverflow # https://stackoverflow.com/a/15170063/575547 process["hostname"].split(/(\d+)/).map { |a| /\d+/.match?(a) ? a.to_i : a } end end end
Sorts processes by hostname following the natural sort order
Source
# File lib/sidekiq/web/helpers.rb, line 267 def stats @stats ||= Sidekiq::Stats.new end
Source
# File lib/sidekiq/web/helpers.rb, line 11 def store_name hash = redis_info return "Dragonfly" if hash.has_key?("dragonfly_version") return "Valkey" if hash.has_key?("valkey_version") "Redis" end
Source
# File lib/sidekiq/web/helpers.rb, line 18 def store_version hash = redis_info return hash["dragonfly_version"] if hash.has_key?("dragonfly_version") return hash["valkey_version"] if hash.has_key?("valkey_version") hash["redis_version"] end
Source
# File lib/sidekiq/web/helpers.rb, line 69 def strings(lang) @@strings ||= {} # Allow sidekiq-web extensions to add locale paths # so extensions can be localized @@strings[lang] ||= config.locales.each_with_object({}) do |path, global| find_locale_files(lang).each do |file| strs = YAML.safe_load_file(file) global.merge!(strs[lang]) end end end
Source
# File lib/sidekiq/web/helpers.rb, line 25 def style_tag(location, **kwargs) global = location.match?(/:\/\//) location = root_path + location if !global && !location.start_with?(root_path) attrs = { type: "text/css", media: "screen", rel: "stylesheet", nonce: csp_nonce, href: location } add_to_head do html_tag(:link, attrs.merge(kwargs)) end end
Source
# File lib/sidekiq/web/helpers.rb, line 225 def t(msg, options = {}) string = get_locale[msg] || strings("en")[msg] || msg if options.empty? string else string % options end end
Source
# File lib/sidekiq/web/helpers.rb, line 160 def text_direction get_locale["TextDirection"] || "ltr" end
Source
# File lib/sidekiq/web/helpers.rb, line 347 def to_display(arg) arg.inspect rescue begin arg.to_s rescue => ex "Cannot display argument: [#{ex.class.name}] #{ex.message}" end end
Source
# File lib/sidekiq/web/helpers.rb, line 82 def to_json(x) Sidekiq.dump_json(x) end
Source
# File lib/sidekiq/web/helpers.rb, line 316 def to_query_string(hash) hash.map { |key, value| SAFE_QPARAMS.include?(key) ? "#{key}=#{CGI.escape(value.to_s)}" : next }.compact.join("&") end
Source
# File lib/sidekiq/web/helpers.rb, line 322 def truncate(text, truncate_after_chars = 2000) (truncate_after_chars && text.size > truncate_after_chars) ? "#{text[0..truncate_after_chars]}..." : text end
Source
# File lib/sidekiq/web/helpers.rb, line 216 def unfiltered? s = url_params("substr") yield unless s && s.size > 0 end
sidekiq/sidekiq#3243
Source
# File lib/sidekiq/web/helpers.rb, line 172 def user_preferred_languages languages = env["HTTP_ACCEPT_LANGUAGE"] languages.to_s.gsub(/\s+/, "").split(",").map { |language| locale, quality = language.split(";q=", 2) locale = nil if locale == "*" # Ignore wildcards quality = quality ? quality.to_f : 1.0 [locale, quality] }.sort { |(_, left), (_, right)| right <=> left }.map(&:first).compact end
See www.rfc-editor.org/rfc/rfc9110.html#section-12.5.4 Returns an array of language tags ordered by their quality value
Inspiration taken from github.com/iain/http_accept_language/blob/master/lib/http_accept_language/parser.rb
Source
# File lib/sidekiq/web/helpers.rb, line 238 def workset @work ||= Sidekiq::WorkSet.new end
Private Instance Methods
Source
# File lib/sidekiq/web/helpers.rb, line 53 def html_tag(tagname, attrs) s = "<#{tagname}" attrs.each_pair do |k, v| next unless v s << " #{k}=\"#{v}\"" end if block_given? s << ">" yield s s << "</#{tagname}>" else s << " />" end s end
NB: keys and values are not escaped; do not allow user input in the attributes