mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Add History to Stats API.
This commit is contained in:
parent
b2826161c2
commit
095331792d
4 changed files with 134 additions and 1 deletions
|
@ -1,7 +1,6 @@
|
|||
require 'sidekiq'
|
||||
|
||||
module Sidekiq
|
||||
|
||||
class Stats
|
||||
def processed
|
||||
count = Sidekiq.redis do |conn|
|
||||
|
@ -33,6 +32,41 @@ module Sidekiq
|
|||
def enqueued
|
||||
queues.values.inject(&:+) || 0
|
||||
end
|
||||
|
||||
class History
|
||||
def initialize(days_previous, start_date = nil)
|
||||
@days_previous = days_previous
|
||||
@start_date = start_date || Time.now.utc.to_date
|
||||
end
|
||||
|
||||
def processed
|
||||
date_stat_hash("processed")
|
||||
end
|
||||
|
||||
def failed
|
||||
date_stat_hash("failed")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def date_stat_hash(stat)
|
||||
i = 0
|
||||
stat_hash = {}
|
||||
|
||||
Sidekiq.redis do |conn|
|
||||
while i < @days_previous
|
||||
date = @start_date - i
|
||||
value = conn.get("stat:#{stat}:#{date}")
|
||||
|
||||
stat_hash[date.to_s] = value ? value.to_i : 0
|
||||
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
|
||||
stat_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
|
|
|
@ -80,6 +80,7 @@ module Sidekiq
|
|||
redis do |conn|
|
||||
conn.multi do
|
||||
conn.incrby("stat:failed", 1)
|
||||
conn.incrby("stat:failed:#{Time.now.utc.to_date}", 1)
|
||||
end
|
||||
end
|
||||
raise
|
||||
|
@ -90,6 +91,7 @@ module Sidekiq
|
|||
conn.del("worker:#{self}")
|
||||
conn.del("worker:#{self}:started")
|
||||
conn.incrby("stat:processed", 1)
|
||||
conn.incrby("stat:processed:#{Time.now.utc.to_date}", 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -72,6 +72,50 @@ class TestApi < MiniTest::Unit::TestCase
|
|||
assert_equal 4, s.enqueued
|
||||
end
|
||||
end
|
||||
|
||||
describe "over time" do
|
||||
describe "processed" do
|
||||
it 'retrieves hash of dates' do
|
||||
Sidekiq.redis do |c|
|
||||
c.incrby("stat:processed:2012-12-24", 4)
|
||||
c.incrby("stat:processed:2012-12-25", 1)
|
||||
c.incrby("stat:processed:2012-12-26", 6)
|
||||
c.incrby("stat:processed:2012-12-27", 2)
|
||||
end
|
||||
Time.stub(:now, Time.parse("2012-12-26 1:00:00 -0500")) do
|
||||
s = Sidekiq::Stats::History.new(2)
|
||||
assert_equal ({ "2012-12-26" => 6, "2012-12-25" => 1 }), s.processed
|
||||
|
||||
s = Sidekiq::Stats::History.new(3)
|
||||
assert_equal ({ "2012-12-26" => 6, "2012-12-25" => 1, "2012-12-24" => 4 }), s.processed
|
||||
|
||||
s = Sidekiq::Stats::History.new(2, Date.parse("2012-12-25"))
|
||||
assert_equal ({ "2012-12-25" => 1, "2012-12-24" => 4 }), s.processed
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "failed" do
|
||||
it 'retrieves hash of dates' do
|
||||
Sidekiq.redis do |c|
|
||||
c.incrby("stat:failed:2012-12-24", 4)
|
||||
c.incrby("stat:failed:2012-12-25", 1)
|
||||
c.incrby("stat:failed:2012-12-26", 6)
|
||||
c.incrby("stat:failed:2012-12-27", 2)
|
||||
end
|
||||
Time.stub(:now, Time.parse("2012-12-26 1:00:00 -0500")) do
|
||||
s = Sidekiq::Stats::History.new(2)
|
||||
assert_equal ({ "2012-12-26" => 6, "2012-12-25" => 1 }), s.failed
|
||||
|
||||
s = Sidekiq::Stats::History.new(3)
|
||||
assert_equal ({ "2012-12-26" => 6, "2012-12-25" => 1, "2012-12-24" => 4 }), s.failed
|
||||
|
||||
s = Sidekiq::Stats::History.new(2, Date.parse("2012-12-25"))
|
||||
assert_equal ({ "2012-12-25" => 1, "2012-12-24" => 4 }), s.failed
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with an empty database' do
|
||||
|
|
|
@ -67,5 +67,58 @@ class TestProcessor < MiniTest::Unit::TestCase
|
|||
processor.process(msgstr, 'default')
|
||||
assert_equal [['myarg']], msg['args']
|
||||
end
|
||||
|
||||
describe 'stats' do
|
||||
before do
|
||||
Sidekiq.redis {|c| c.flushdb }
|
||||
end
|
||||
|
||||
describe 'when successful' do
|
||||
def successful_job
|
||||
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
|
||||
actor = MiniTest::Mock.new
|
||||
actor.expect(:processor_done, nil, [@processor])
|
||||
@boss.expect(:async, actor, [])
|
||||
@processor.process(msg, 'default')
|
||||
end
|
||||
|
||||
it 'increments processed stat' do
|
||||
successful_job
|
||||
assert_equal 1, Sidekiq::Stats.new.processed
|
||||
end
|
||||
|
||||
it 'increments date processed stat' do
|
||||
Time.stub(:now, Time.parse("2012-12-25 1:00:00 -0500")) do
|
||||
successful_job
|
||||
date_processed = Sidekiq.redis { |conn| conn.get("stat:processed:2012-12-25") }.to_i
|
||||
assert_equal 1, date_processed
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'when failed' do
|
||||
def failed_job
|
||||
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['boom'] })
|
||||
begin
|
||||
@processor.process(msg, 'default')
|
||||
rescue TestException
|
||||
end
|
||||
end
|
||||
|
||||
it 'increments failed stat' do
|
||||
failed_job
|
||||
assert_equal 1, Sidekiq::Stats.new.failed
|
||||
end
|
||||
|
||||
it 'increments date failed stat' do
|
||||
Time.stub(:now, Time.parse("2012-12-25 1:00:00 -0500")) do
|
||||
failed_job
|
||||
date_failed = Sidekiq.redis { |conn| conn.get("stat:failed:2012-12-25") }.to_i
|
||||
assert_equal 1, date_failed
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue