class Sidekiq::Stats::History

Public Class Methods

new(days_previous, start_date = nil, pool: nil) click to toggle source
# File lib/sidekiq/api.rb, line 179
def initialize(days_previous, start_date = nil, pool: nil)
  # we only store five years of data in Redis
  raise ArgumentError if days_previous < 1 || days_previous > (5 * 365)
  @days_previous = days_previous
  @start_date = start_date || Time.now.utc.to_date
end

Public Instance Methods

failed() click to toggle source
# File lib/sidekiq/api.rb, line 190
def failed
  @failed ||= date_stat_hash("failed")
end
processed() click to toggle source
# File lib/sidekiq/api.rb, line 186
def processed
  @processed ||= date_stat_hash("processed")
end

Private Instance Methods

date_stat_hash(stat) click to toggle source
# File lib/sidekiq/api.rb, line 196
def date_stat_hash(stat)
  stat_hash = {}
  dates = @start_date.downto(@start_date - @days_previous + 1).map { |date|
    date.strftime("%Y-%m-%d")
  }

  keys = dates.map { |datestr| "stat:#{stat}:#{datestr}" }

  Sidekiq.redis do |conn|
    conn.mget(keys).each_with_index do |value, idx|
      stat_hash[dates[idx]] = value ? value.to_i : 0
    end
  end

  stat_hash
end