class Sidekiq::Statistic::Base

Constants

KEY_SEPARATOR

Public Class Methods

new(days_previous, start_date = nil) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 8
def initialize(days_previous, start_date = nil)
  @start_date = start_date || Time.now.utc.to_date
  @end_date = @start_date - days_previous
end

Public Instance Methods

statistic_for(worker) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 13
def statistic_for(worker)
  statistic_hash.map{ |h| h.values.first[worker] || {} }
end
statistic_hash() click to toggle source
# File lib/sidekiq/statistic/base.rb, line 21
def statistic_hash
  @redis_hash ||= Sidekiq.redis do |conn|
    redis_hash = {}
    get_statistic_hash(conn, redis_hash)
    update_time_values(conn, redis_hash)
    desired_dates.map { |key| result_hash(redis_hash, key) }
  end
end
worker_names() click to toggle source
# File lib/sidekiq/statistic/base.rb, line 17
def worker_names
  @worker_names ||= statistic_hash.flat_map{ |h| h.values.first.keys }.uniq.sort
end

Private Instance Methods

desired_dates() click to toggle source
# File lib/sidekiq/statistic/base.rb, line 45
def desired_dates
  (@end_date..@start_date).map { |date| date.strftime "%Y-%m-%d" }
end
get_statistic_hash(conn, redis_hash) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 32
def get_statistic_hash(conn, redis_hash)
  conn
    .hgetall(REDIS_HASH)
    .each do |keys, value|
      *keys, last = keys.split(KEY_SEPARATOR)
      keys.inject(redis_hash, &key_or_empty_hash)[last.to_sym] = to_number(value)
    end
end
key_or_empty_hash() click to toggle source
# File lib/sidekiq/statistic/base.rb, line 41
def key_or_empty_hash
  ->(h, k) { h[k] || h[k] = {} }
end
result_hash(redis_hash, key) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 49
def result_hash(redis_hash, key)
  redis_hash.fetch(key, {}).each { |_, v| update_hash_statments v }
  { key => (redis_hash[key] || {}) }
end
time_hash(timeslist, worker_key) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 83
def time_hash(timeslist, worker_key)
  return {} if timeslist.empty?
  statistics = time_statistics(timeslist)

  Sidekiq.redis do |redis|
    redis.hmset REDIS_HASH,
      statistics.flat_map{ |(k, v)| ["#{worker_key}:#{k}", v] }
  end

  statistics
end
time_statistics(timeslist) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 95
def time_statistics(timeslist)
  total = timeslist.inject(:+)

  {
    average_time: total / timeslist.count,
    min_time: timeslist.min,
    max_time: timeslist.max,
    total_time: total
  }
end
to_number(value) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 59
def to_number(value)
  case value
  when /\A-?\d+\.\d+\z/ then value.to_f
  when /\A-?\d+\z/ then value.to_i
  else value
  end
end
update_hash_statments(hash) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 54
def update_hash_statments(hash)
  hash[:passed] ||= 0
  hash[:failed] ||= 0
end
update_time_values(conn, redis_hash) click to toggle source
# File lib/sidekiq/statistic/base.rb, line 67
def update_time_values(conn, redis_hash)
  redis_hash.each do |time, workers|
    workers.each do |worker, _|
      worker_key = "#{time}:#{worker}"

      timeslist, _ = conn.multi do |multi|
        multi.lrange("#{worker_key}:timeslist", 0, -1)
        multi.del("#{worker_key}:timeslist")
      end

      timeslist.map!(&:to_f)
      redis_hash[time][worker].merge! time_hash(timeslist, worker_key)
    end
  end
end