Module: Sidekiq::WebHelpers
- Defined in:
- lib/sidekiq/web/helpers.rb
Overview
This is not a public API
Constant Summary collapse
- SAFE_QPARAMS =
%w[page poll 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 ])
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
- #clear_caches ⇒ 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 = nil) ⇒ Object
within is used by Sidekiq Pro.
- #environment_title_prefix ⇒ Object
-
#filtering ⇒ Object
This is a hook for a Sidekiq Pro feature.
- #find_locale_files(lang) ⇒ 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
- #namespace ⇒ Object
- #number_with_delimiter(number) ⇒ Object
- #parse_params(params) ⇒ Object
- #poll_path ⇒ Object
- #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_connection ⇒ Object
- #redis_connection_and_namespace ⇒ Object
- #redis_info ⇒ Object
- #relative_time(time) ⇒ Object
- #retry_extra_items(retry_job) ⇒ Object
- #retry_or_delete_or_kill(job, params) ⇒ Object
- #root_path ⇒ Object
- #rtl? ⇒ Boolean
- #server_utc_time ⇒ Object
- #sort_direction_label ⇒ Object
- #stats ⇒ Object
- #strings(lang) ⇒ Object
- #t(msg, options = {}) ⇒ Object
- #text_direction ⇒ Object
- #to_display(arg) ⇒ Object
- #to_query_string(params) ⇒ Object
- #truncate(text, truncate_after_chars = 2000) ⇒ Object
-
#unfiltered? ⇒ Boolean
mperham/sidekiq#3243.
- #user_preferred_languages ⇒ Object
- #workers ⇒ 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 %>
57 58 59 60 |
# File 'lib/sidekiq/web/helpers.rb', line 57 def add_to_head @head_html ||= [] @head_html << yield.dup if block_given? end |
#available_locales ⇒ Object
37 38 39 |
# File 'lib/sidekiq/web/helpers.rb', line 37 def available_locales @available_locales ||= locale_files.map { |path| File.basename(path, ".yml") }.uniq end |
#clear_caches ⇒ Object
25 26 27 28 29 |
# File 'lib/sidekiq/web/helpers.rb', line 25 def clear_caches @strings = nil @locale_files = nil @available_locales = nil end |
#csrf_tag ⇒ Object
235 236 237 |
# File 'lib/sidekiq/web/helpers.rb', line 235 def csrf_tag "<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>" end |
#current_path ⇒ Object
178 179 180 |
# File 'lib/sidekiq/web/helpers.rb', line 178 def current_path @current_path ||= request.path_info.gsub(/^\//, "") end |
#current_status ⇒ Object
182 183 184 |
# File 'lib/sidekiq/web/helpers.rb', line 182 def current_status workers.size == 0 ? "idle" : "active" end |
#delete_or_add_queue(job, params) ⇒ Object
328 329 330 331 332 333 334 |
# File 'lib/sidekiq/web/helpers.rb', line 328 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
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/sidekiq/web/helpers.rb', line 222 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
62 63 64 |
# File 'lib/sidekiq/web/helpers.rb', line 62 def display_custom_head @head_html.join if defined?(@head_html) end |
#display_tags(job, within = nil) ⇒ Object
within is used by Sidekiq Pro
119 120 121 122 123 |
# File 'lib/sidekiq/web/helpers.rb', line 119 def (job, within = nil) job..map { |tag| "<span class='jobtag label label-info'>#{::Rack::Utils.escape_html(tag)}</span>" }.join(" ") end |
#environment_title_prefix ⇒ Object
297 298 299 300 301 |
# File 'lib/sidekiq/web/helpers.rb', line 297 def environment_title_prefix environment = Sidekiq.[:environment] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" "[#{environment.upcase}] " unless environment == "production" end |
#filtering ⇒ Object
This is a hook for a Sidekiq Pro feature. Please don’t touch.
46 47 |
# File 'lib/sidekiq/web/helpers.rb', line 46 def filtering(*) end |
#find_locale_files(lang) ⇒ Object
41 42 43 |
# File 'lib/sidekiq/web/helpers.rb', line 41 def find_locale_files(lang) locale_files.select { |file| file =~ /\/#{lang}\.yml$/ } end |
#get_locale ⇒ Object
130 131 132 |
# File 'lib/sidekiq/web/helpers.rb', line 130 def get_locale strings(locale) end |
#h(text) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/sidekiq/web/helpers.rb', line 277 def h(text) ::Rack::Utils.escape_html(text) 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
191 192 193 |
# File 'lib/sidekiq/web/helpers.rb', line 191 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/sidekiq/web/helpers.rb', line 102 def locale @locale ||= begin 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
31 32 33 34 35 |
# File 'lib/sidekiq/web/helpers.rb', line 31 def locale_files @locale_files ||= settings.locales.flat_map { |path| Dir["#{path}/*.yml"] } end |
#namespace ⇒ Object
166 167 168 |
# File 'lib/sidekiq/web/helpers.rb', line 166 def namespace @ns ||= Sidekiq.redis { |conn| conn.respond_to?(:namespace) ? conn.namespace : nil } end |
#number_with_delimiter(number) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/sidekiq/web/helpers.rb', line 264 def number_with_delimiter(number) begin Float(number) rescue ArgumentError, TypeError return number end = {delimiter: ",", separator: "."} parts = number.to_s.to_str.split(".") parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{[:delimiter]}") parts.join([:separator]) end |
#parse_params(params) ⇒ Object
195 196 197 198 |
# File 'lib/sidekiq/web/helpers.rb', line 195 def parse_params(params) score, jid = params.split("-", 2) [score.to_f, jid] end |
#poll_path ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sidekiq/web/helpers.rb', line 66 def poll_path if current_path != "" && params["poll"] path = root_path + current_path query_string = to_query_string(params.slice(*params.keys - %w[page poll])) path += "?#{query_string}" unless query_string.empty? path else "" end end |
#processes ⇒ Object
151 152 153 |
# File 'lib/sidekiq/web/helpers.rb', line 151 def processes @processes ||= Sidekiq::ProcessSet.new end |
#product_version ⇒ Object
303 304 305 |
# File 'lib/sidekiq/web/helpers.rb', line 303 def product_version "Sidekiq v#{Sidekiq::VERSION}" end |
#qparams(options) ⇒ Object
Merge options with current params, filter safe params, and stringify to query string
203 204 205 206 207 208 209 210 |
# File 'lib/sidekiq/web/helpers.rb', line 203 def qparams() # stringify .keys.each do |key| [key.to_s] = .delete(key) end 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.
287 288 289 290 291 292 293 294 295 |
# File 'lib/sidekiq/web/helpers.rb', line 287 def redirect_with_query(url) r = request.referer if r && r =~ /\?/ ref = URI(r) redirect("#{url}?#{ref.query}") else redirect url end end |
#redis_connection ⇒ Object
159 160 161 162 163 164 |
# File 'lib/sidekiq/web/helpers.rb', line 159 def redis_connection Sidekiq.redis do |conn| c = conn.connection "redis://#{c[:location]}/#{c[:db]}" end end |
#redis_connection_and_namespace ⇒ Object
311 312 313 314 315 316 |
# File 'lib/sidekiq/web/helpers.rb', line 311 def redis_connection_and_namespace @redis_connection_and_namespace ||= begin namespace_suffix = namespace.nil? ? "" : "##{namespace}" "#{redis_connection}#{namespace_suffix}" end end |
#redis_info ⇒ Object
170 171 172 |
# File 'lib/sidekiq/web/helpers.rb', line 170 def redis_info Sidekiq.redis_info end |
#relative_time(time) ⇒ Object
186 187 188 189 |
# File 'lib/sidekiq/web/helpers.rb', line 186 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
256 257 258 259 260 261 262 |
# File 'lib/sidekiq/web/helpers.rb', line 256 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
318 319 320 321 322 323 324 325 326 |
# File 'lib/sidekiq/web/helpers.rb', line 318 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
174 175 176 |
# File 'lib/sidekiq/web/helpers.rb', line 174 def root_path "#{env["SCRIPT_NAME"]}/" end |
#rtl? ⇒ Boolean
81 82 83 |
# File 'lib/sidekiq/web/helpers.rb', line 81 def rtl? text_direction == "rtl" end |
#server_utc_time ⇒ Object
307 308 309 |
# File 'lib/sidekiq/web/helpers.rb', line 307 def server_utc_time Time.now.utc.strftime("%H:%M:%S UTC") end |
#sort_direction_label ⇒ Object
143 144 145 |
# File 'lib/sidekiq/web/helpers.rb', line 143 def sort_direction_label params[:direction] == "asc" ? "↑" : "↓" end |
#stats ⇒ Object
155 156 157 |
# File 'lib/sidekiq/web/helpers.rb', line 155 def stats @stats ||= Sidekiq::Stats.new end |
#strings(lang) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sidekiq/web/helpers.rb', line 11 def strings(lang) @strings ||= {} @strings[lang] ||= begin # Allow sidekiq-web extensions to add locale paths # so extensions can be localized settings.locales.each_with_object({}) do |path, global| find_locale_files(lang).each do |file| strs = YAML.load(File.open(file)) global.merge!(strs[lang]) end end end end |
#t(msg, options = {}) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/sidekiq/web/helpers.rb', line 134 def t(msg, = {}) string = get_locale[msg] || strings("en")[msg] || msg if .empty? string else string % end end |
#text_direction ⇒ Object
77 78 79 |
# File 'lib/sidekiq/web/helpers.rb', line 77 def text_direction get_locale["TextDirection"] || "ltr" end |
#to_display(arg) ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/sidekiq/web/helpers.rb', line 239 def to_display(arg) arg.inspect rescue begin arg.to_s rescue => ex "Cannot display argument: [#{ex.class.name}] #{ex.}" end end |
#to_query_string(params) ⇒ Object
212 213 214 215 216 |
# File 'lib/sidekiq/web/helpers.rb', line 212 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
218 219 220 |
# File 'lib/sidekiq/web/helpers.rb', line 218 def truncate(text, truncate_after_chars = 2000) truncate_after_chars && text.size > truncate_after_chars ? "#{text[0..truncate_after_chars]}..." : text end |
#unfiltered? ⇒ Boolean
mperham/sidekiq#3243
126 127 128 |
# File 'lib/sidekiq/web/helpers.rb', line 126 def unfiltered? yield unless env["PATH_INFO"].start_with?("/filter/") end |
#user_preferred_languages ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/sidekiq/web/helpers.rb', line 86 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 |