Module: Sidekiq::WebHelpers
- Defined in:
- lib/sidekiq/web/helpers.rb
Overview
These methods are available to pages within the Web UI and UI extensions. They are not public APIs for applications to use.
Constant Summary collapse
- SAFE_QPARAMS =
%w[page direction]
- RETRY_JOB_KEYS =
Set.new(%w[ queue class args retry_count retried_at failed_at jid error_message error_class backtrace error_backtrace enqueued_at retry wrapped created_at tags display_class ])
Instance Method Summary collapse
-
#add_to_head ⇒ Object
This view helper provide ability display you html code in to head of page.
- #available_locales ⇒ Object
- #busy_weights(capsule_weights) ⇒ Object
- #clear_caches ⇒ Object
- #csp_nonce ⇒ Object
- #csrf_tag ⇒ Object
- #current_path ⇒ Object
- #current_status ⇒ Object
- #delete_or_add_queue(job, params) ⇒ Object
- #display_args(args, truncate_after_chars = 2000) ⇒ Object
- #display_custom_head ⇒ Object
- #display_tags(job, within = "retries") ⇒ Object
- #environment_title_prefix ⇒ Object
- #filter_link(jid, within = "retries") ⇒ Object
- #filtering(which) ⇒ Object
- #find_locale_files(lang) ⇒ Object
- #format_memory(rss_kb) ⇒ Object
- #get_locale ⇒ Object
- #h(text) ⇒ Object
- #job_params(job, score) ⇒ Object
-
#locale ⇒ Object
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.
- #locale_files ⇒ Object
- #number_with_delimiter(number, options = {}) ⇒ Object
- #parse_params(params) ⇒ Object
- #pollable? ⇒ Boolean
- #processes ⇒ Object
- #product_version ⇒ Object
-
#qparams(options) ⇒ Object
Merge options with current params, filter safe params, and stringify to query string.
-
#redirect_with_query(url) ⇒ Object
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
- #redis_info ⇒ Object
- #redis_url ⇒ Object
- #relative_time(time) ⇒ Object
- #retry_extra_items(retry_job) ⇒ Object
- #retry_or_delete_or_kill(job, params) ⇒ Object
- #root_path ⇒ Object
- #rtl? ⇒ Boolean
- #script_tag(location, **kwargs) ⇒ Object
- #search(jobset, substr) ⇒ Object
- #server_utc_time ⇒ Object
- #singularize(str, count) ⇒ Object
- #sort_direction_label ⇒ Object
-
#sorted_processes ⇒ Object
Sorts processes by hostname following the natural sort order.
- #stats ⇒ Object
- #strings(lang) ⇒ Object
- #style_tag(location, **kwargs) ⇒ Object
- #t(msg, options = {}) ⇒ Object
- #text_direction ⇒ Object
- #to_display(arg) ⇒ Object
- #to_json(x) ⇒ Object
- #to_query_string(params) ⇒ Object
- #truncate(text, truncate_after_chars = 2000) ⇒ Object
-
#unfiltered? ⇒ Boolean
sidekiq/sidekiq#3243.
- #user_preferred_languages ⇒ Object
- #workset ⇒ Object
Instance Method Details
#add_to_head ⇒ Object
This view helper provide ability display you html code in to head of page. Example:
<% add_to_head do %>
<link rel="stylesheet" .../>
<meta .../>
<% end %>
132 133 134 135 |
# File 'lib/sidekiq/web/helpers.rb', line 132 def add_to_head @head_html ||= [] @head_html << yield.dup if block_given? end |
#available_locales ⇒ Object
91 92 93 |
# File 'lib/sidekiq/web/helpers.rb', line 91 def available_locales @available_locales ||= Set.new(locale_files.map { |path| File.basename(path, ".yml") }) end |
#busy_weights(capsule_weights) ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/sidekiq/web/helpers.rb', line 229 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 |
#clear_caches ⇒ Object
79 80 81 82 83 |
# File 'lib/sidekiq/web/helpers.rb', line 79 def clear_caches @strings = nil @locale_files = nil @available_locales = nil end |
#csp_nonce ⇒ Object
313 314 315 |
# File 'lib/sidekiq/web/helpers.rb', line 313 def csp_nonce env[:csp_nonce] end |
#csrf_tag ⇒ Object
309 310 311 |
# File 'lib/sidekiq/web/helpers.rb', line 309 def csrf_tag "<input type='hidden' name='authenticity_token' value='#{env[:csrf_token]}'/>" end |
#current_path ⇒ Object
255 256 257 |
# File 'lib/sidekiq/web/helpers.rb', line 255 def current_path @current_path ||= request.path_info.gsub(/^\//, "") end |
#current_status ⇒ Object
259 260 261 |
# File 'lib/sidekiq/web/helpers.rb', line 259 def current_status (workset.size == 0) ? "idle" : "active" end |
#delete_or_add_queue(job, params) ⇒ Object
408 409 410 411 412 413 414 |
# File 'lib/sidekiq/web/helpers.rb', line 408 def delete_or_add_queue(job, params) if params["delete"] job.delete elsif params["add_to_queue"] job.add_to_queue end end |
#display_args(args, truncate_after_chars = 2000) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/sidekiq/web/helpers.rb', line 296 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 |
#display_custom_head ⇒ Object
137 138 139 |
# File 'lib/sidekiq/web/helpers.rb', line 137 def display_custom_head @head_html.join if defined?(@head_html) end |
#display_tags(job, within = "retries") ⇒ Object
118 119 120 121 122 |
# File 'lib/sidekiq/web/helpers.rb', line 118 def (job, within = "retries") job..map { |tag| "<span class='label label-info jobtag'>#{filter_link(tag, within)}</span>" }.join(" ") end |
#environment_title_prefix ⇒ Object
379 380 381 382 383 |
# File 'lib/sidekiq/web/helpers.rb', line 379 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 |
#filter_link(jid, within = "retries") ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/sidekiq/web/helpers.rb', line 110 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 |
#filtering(which) ⇒ Object
106 107 108 |
# File 'lib/sidekiq/web/helpers.rb', line 106 def filtering(which) erb(:filtering, locals: {which: which}) end |
#find_locale_files(lang) ⇒ Object
95 96 97 |
# File 'lib/sidekiq/web/helpers.rb', line 95 def find_locale_files(lang) locale_files.select { |file| file =~ /\/#{lang}\.yml$/ } end |
#format_memory(rss_kb) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/sidekiq/web/helpers.rb', line 342 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 |
#get_locale ⇒ Object
191 192 193 |
# File 'lib/sidekiq/web/helpers.rb', line 191 def get_locale strings(locale) end |
#h(text) ⇒ Object
359 360 361 362 363 364 365 |
# File 'lib/sidekiq/web/helpers.rb', line 359 def h(text) ::Rack::Utils.escape_html(text.to_s) rescue ArgumentError => e raise unless e..eql?("invalid byte sequence in UTF-8") text.encode!("UTF-16", "UTF-8", invalid: :replace, replace: "").encode!("UTF-8", "UTF-16") retry end |
#job_params(job, score) ⇒ Object
268 269 270 |
# File 'lib/sidekiq/web/helpers.rb', line 268 def job_params(job, score) "#{score}-#{job["jid"]}" end |
#locale ⇒ Object
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.
Inspiration taken from github.com/iain/http_accept_language/blob/master/lib/http_accept_language/parser.rb
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/sidekiq/web/helpers.rb', line 166 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 = user_preferred_languages.map { |preferred| preferred_language = preferred.split("-", 2).first lang_group = available_locales.select { |available| preferred_language == available.split("-", 2).first } lang_group.find { |lang| lang == preferred } || lang_group.min_by(&:length) }.compact.first matched_locale || "en" end end |
#locale_files ⇒ Object
85 86 87 88 89 |
# File 'lib/sidekiq/web/helpers.rb', line 85 def locale_files @locale_files ||= settings.locales.flat_map { |path| Dir["#{path}/*.yml"] } end |
#number_with_delimiter(number, options = {}) ⇒ Object
354 355 356 357 |
# File 'lib/sidekiq/web/helpers.rb', line 354 def number_with_delimiter(number, = {}) precision = [:precision] || 0 %(<span data-nwp="#{precision}">#{number.round(precision)}</span>) end |
#parse_params(params) ⇒ Object
272 273 274 275 |
# File 'lib/sidekiq/web/helpers.rb', line 272 def parse_params(params) score, jid = params.split("-", 2) [score.to_f, jid] end |
#pollable? ⇒ Boolean
393 394 395 396 |
# File 'lib/sidekiq/web/helpers.rb', line 393 def pollable? # there's no point to refreshing the metrics pages every N seconds !(current_path == "" || current_path.index("metrics")) end |
#processes ⇒ Object
212 213 214 |
# File 'lib/sidekiq/web/helpers.rb', line 212 def processes @processes ||= Sidekiq::ProcessSet.new end |
#product_version ⇒ Object
385 386 387 |
# File 'lib/sidekiq/web/helpers.rb', line 385 def product_version "Sidekiq v#{Sidekiq::VERSION}" end |
#qparams(options) ⇒ Object
Merge options with current params, filter safe params, and stringify to query string
280 281 282 283 284 |
# File 'lib/sidekiq/web/helpers.rb', line 280 def qparams() = .transform_keys(&:to_s) to_query_string(params.merge()) end |
#redirect_with_query(url) ⇒ Object
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
369 370 371 372 373 374 375 376 377 |
# File 'lib/sidekiq/web/helpers.rb', line 369 def redirect_with_query(url) r = request.referer if r && r =~ /\?/ ref = URI(r) redirect("#{url}?#{ref.query}") else redirect url end end |
#redis_info ⇒ Object
247 248 249 |
# File 'lib/sidekiq/web/helpers.rb', line 247 def redis_info Sidekiq.default_configuration.redis_info end |
#redis_url ⇒ Object
241 242 243 244 245 |
# File 'lib/sidekiq/web/helpers.rb', line 241 def redis_url Sidekiq.redis do |conn| conn.config.server_url end end |
#relative_time(time) ⇒ Object
263 264 265 266 |
# File 'lib/sidekiq/web/helpers.rb', line 263 def relative_time(time) stamp = time.getutc.iso8601 %(<time class="ltr" dir="ltr" title="#{stamp}" datetime="#{stamp}">#{time}</time>) end |
#retry_extra_items(retry_job) ⇒ Object
334 335 336 337 338 339 340 |
# File 'lib/sidekiq/web/helpers.rb', line 334 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 |
#retry_or_delete_or_kill(job, params) ⇒ Object
398 399 400 401 402 403 404 405 406 |
# File 'lib/sidekiq/web/helpers.rb', line 398 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 |
#root_path ⇒ Object
251 252 253 |
# File 'lib/sidekiq/web/helpers.rb', line 251 def root_path "#{env["SCRIPT_NAME"]}/" end |
#rtl? ⇒ Boolean
145 146 147 |
# File 'lib/sidekiq/web/helpers.rb', line 145 def rtl? text_direction == "rtl" end |
#script_tag(location, **kwargs) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sidekiq/web/helpers.rb', line 25 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 |
#search(jobset, substr) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/sidekiq/web/helpers.rb', line 99 def search(jobset, substr) resultset = jobset.scan(substr).to_a @current_page = 1 @count = @total_size = resultset.size resultset end |
#server_utc_time ⇒ Object
389 390 391 |
# File 'lib/sidekiq/web/helpers.rb', line 389 def server_utc_time Time.now.utc.strftime("%H:%M:%S UTC") end |
#singularize(str, count) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/sidekiq/web/helpers.rb', line 71 def singularize(str, count) if count == 1 && str.respond_to?(:singularize) # rails str.singularize else str end end |
#sort_direction_label ⇒ Object
204 205 206 |
# File 'lib/sidekiq/web/helpers.rb', line 204 def sort_direction_label (params[:direction] == "asc") ? "↑" : "↓" end |
#sorted_processes ⇒ Object
Sorts processes by hostname following the natural sort order
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/sidekiq/web/helpers.rb', line 217 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 |
#stats ⇒ Object
237 238 239 |
# File 'lib/sidekiq/web/helpers.rb', line 237 def stats @stats ||= Sidekiq::Stats.new end |
#strings(lang) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sidekiq/web/helpers.rb', line 54 def strings(lang) @strings ||= {} # Allow sidekiq-web extensions to add locale paths # so extensions can be localized @strings[lang] ||= settings.locales.each_with_object({}) do |path, global| find_locale_files(lang).each do |file| strs = YAML.safe_load(File.read(file)) global.merge!(strs[lang]) end end end |
#style_tag(location, **kwargs) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sidekiq/web/helpers.rb', line 12 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 } html_tag(:link, attrs.merge(kwargs)) end |
#t(msg, options = {}) ⇒ Object
195 196 197 198 199 200 201 202 |
# File 'lib/sidekiq/web/helpers.rb', line 195 def t(msg, = {}) string = get_locale[msg] || strings("en")[msg] || msg if .empty? string else string % end end |
#text_direction ⇒ Object
141 142 143 |
# File 'lib/sidekiq/web/helpers.rb', line 141 def text_direction get_locale["TextDirection"] || "ltr" end |
#to_display(arg) ⇒ Object
317 318 319 320 321 322 323 324 325 |
# File 'lib/sidekiq/web/helpers.rb', line 317 def to_display(arg) arg.inspect rescue begin arg.to_s rescue => ex "Cannot display argument: [#{ex.class.name}] #{ex.}" end end |
#to_json(x) ⇒ Object
67 68 69 |
# File 'lib/sidekiq/web/helpers.rb', line 67 def to_json(x) Sidekiq.dump_json(x) end |
#to_query_string(params) ⇒ Object
286 287 288 289 290 |
# File 'lib/sidekiq/web/helpers.rb', line 286 def to_query_string(params) params.map { |key, value| SAFE_QPARAMS.include?(key) ? "#{key}=#{CGI.escape(value.to_s)}" : next }.compact.join("&") end |
#truncate(text, truncate_after_chars = 2000) ⇒ Object
292 293 294 |
# File 'lib/sidekiq/web/helpers.rb', line 292 def truncate(text, truncate_after_chars = 2000) (truncate_after_chars && text.size > truncate_after_chars) ? "#{text[0..truncate_after_chars]}..." : text end |
#unfiltered? ⇒ Boolean
sidekiq/sidekiq#3243
186 187 188 189 |
# File 'lib/sidekiq/web/helpers.rb', line 186 def unfiltered? s = url_params("substr") yield unless s && s.size > 0 end |
#user_preferred_languages ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/sidekiq/web/helpers.rb', line 150 def user_preferred_languages languages = env["HTTP_ACCEPT_LANGUAGE"] languages.to_s.downcase.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 |