Class: SidekiqJobStats::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_job_stats/duration.rb

Constant Summary collapse

JOB_DURATION_PERIOD_EXPIRE =
{ :day => 60 * 60 * 24, :month => 60 * 60 * 24 * 30 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_class) ⇒ Duration

Returns a new instance of Duration.



7
8
9
# File 'lib/sidekiq_job_stats/duration.rb', line 7

def initialize(job_class)
  @job_class = job_class
end

Instance Attribute Details

#job_classObject

Returns the value of attribute job_class.



5
6
7
# File 'lib/sidekiq_job_stats/duration.rb', line 5

def job_class
  @job_class
end

Instance Method Details

#job_rolling_avg_dayObject



42
43
44
45
# File 'lib/sidekiq_job_stats/duration.rb', line 42

def job_rolling_avg_day
  _total, _count, _peak, avg = jobs_duration_per(:day)
  avg
end

#job_rolling_avg_monthObject



47
48
49
50
# File 'lib/sidekiq_job_stats/duration.rb', line 47

def job_rolling_avg_month
  _total, _count, _peak, avg = jobs_duration_per(:month)
  avg
end

#jobs_duration_keyObject



68
69
70
# File 'lib/sidekiq_job_stats/duration.rb', line 68

def jobs_duration_key
  "stats:jobs:#{job_class}:duration"
end

#jobs_duration_per(period) ⇒ Object



62
63
64
65
66
# File 'lib/sidekiq_job_stats/duration.rb', line 62

def jobs_duration_per(period)
  Sidekiq.redis do |conn|
    JSON.parse(conn.get(jobs_duration_period_key(period)) || "null")
  end
end

#jobs_duration_period_key(period) ⇒ Object



72
73
74
# File 'lib/sidekiq_job_stats/duration.rb', line 72

def jobs_duration_period_key(period)
  "#{jobs_duration_key}:#{period}:#{Time.now.send(period)}"
end

#jobs_performed_dayObject



32
33
34
35
# File 'lib/sidekiq_job_stats/duration.rb', line 32

def jobs_performed_day
  _total, count, _peak, _avg = jobs_duration_per(:day)
  count
end

#jobs_performed_monthObject



37
38
39
40
# File 'lib/sidekiq_job_stats/duration.rb', line 37

def jobs_performed_month
  _total, count, _peak, _avg = jobs_duration_per(:month)
  count
end

#jobs_track_duration(duration, period) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sidekiq_job_stats/duration.rb', line 16

def jobs_track_duration(duration, period)
  total, count, peak, _avg = jobs_duration_per(period)

  Sidekiq.redis do |conn|
    conn.set(
      jobs_duration_period_key(period),
      [
        total.to_f + duration,
        count.to_i + 1,
        [peak.to_f, duration].max,
        (total.to_f + duration)/(count.to_i + 1)
      ].to_json)
    conn.expire(jobs_duration_period_key(period), JOB_DURATION_PERIOD_EXPIRE[period])
  end
end

#longest_job_dayObject



52
53
54
55
# File 'lib/sidekiq_job_stats/duration.rb', line 52

def longest_job_day
  _total, _count, peak, _avg = jobs_duration_per(:day)
  peak
end

#longest_job_monthObject



57
58
59
60
# File 'lib/sidekiq_job_stats/duration.rb', line 57

def longest_job_month
  _total, _count, peak, _avg = jobs_duration_per(:month)
  peak
end

#track(duration) ⇒ Object



11
12
13
14
# File 'lib/sidekiq_job_stats/duration.rb', line 11

def track(duration)
  jobs_track_duration(duration, :day)
  jobs_track_duration(duration, :month)
end