class Sidekiq::Throttled::Web::Stats

Throttle strategy stats generation helper

Constants

TIME_CONVERSION

Public Class Methods

new(strategy) click to toggle source

@param [Strategy::Concurrency, Strategy::Threshold] strategy

# File lib/sidekiq/throttled/web/stats.rb, line 16
def initialize(strategy)
  raise ArgumentError, "Can't handle dynamic strategies" if strategy&.dynamic?

  @strategy = strategy
end

Public Instance Methods

to_html() click to toggle source

@return [String]

# File lib/sidekiq/throttled/web/stats.rb, line 23
def to_html
  return "" unless @strategy

  html = humanize_integer(@strategy.limit) << " jobs"

  html << " per " << humanize_duration(@strategy.period) if @strategy.respond_to?(:period)

  html << "<br />" << colorize_count(@strategy.count, @strategy.limit)
end

Private Instance Methods

colorize_count(int, max) click to toggle source

@return [String]

# File lib/sidekiq/throttled/web/stats.rb, line 36
def colorize_count(int, max)
  percentile = 100.00 * int / max
  lvl = if    80 <= percentile then "danger"
        elsif 60 <= percentile then "warning"
        else                        "success"
        end

  %(<span class="label label-#{lvl}">#{int}</span>)
end
humanize_duration(int) click to toggle source

@return [String]

# File lib/sidekiq/throttled/web/stats.rb, line 47
def humanize_duration(int)
  arr = []

  TIME_CONVERSION.each do |(dimension, unit, units)|
    count = (int / dimension).to_i

    next unless count.positive?

    int -= count * dimension
    arr << "#{count} #{1 == count ? unit : units}"
  end

  arr.join " "
end
humanize_integer(int) click to toggle source

@return [String]

# File lib/sidekiq/throttled/web/stats.rb, line 63
def humanize_integer(int)
  digits = int.to_s.split ""
  str    = digits.shift(digits.count % 3).join("")

  str << " " << digits.shift(3).join("") while digits.count.positive?

  str.strip
end