Class: Sidekiq::Metrics::Query::JobResult

Inherits:
Struct
  • Object
show all
Defined in:
lib/sidekiq/metrics/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJobResult

Returns a new instance of JobResult.



103
104
105
106
107
108
# File 'lib/sidekiq/metrics/query.rb', line 103

def initialize
  super
  self.series = Hash.new { |h, k| h[k] = Hash.new(0) }
  self.hist = Hash.new { |h, k| h[k] = [] }
  self.totals = Hash.new(0)
end

Instance Attribute Details

#histObject

Returns the value of attribute hist

Returns:

  • (Object)

    the current value of hist



102
103
104
# File 'lib/sidekiq/metrics/query.rb', line 102

def hist
  @hist
end

#seriesObject

Returns the value of attribute series

Returns:

  • (Object)

    the current value of series



102
103
104
# File 'lib/sidekiq/metrics/query.rb', line 102

def series
  @series
end

#totalsObject

Returns the value of attribute totals

Returns:

  • (Object)

    the current value of totals



102
103
104
# File 'lib/sidekiq/metrics/query.rb', line 102

def totals
  @totals
end

Instance Method Details

#add_hist(time, hist_result) ⇒ Object



118
119
120
# File 'lib/sidekiq/metrics/query.rb', line 118

def add_hist(time, hist_result)
  hist[time.strftime("%H:%M")] = hist_result
end

#add_metric(metric, time, value) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/sidekiq/metrics/query.rb', line 110

def add_metric(metric, time, value)
  totals[metric] += value
  series[metric][time.strftime("%H:%M")] += value

  # Include timing measurements in seconds for convenience
  add_metric("s", time, value / 1000.0) if metric == "ms"
end

#series_avg(metric = "ms") ⇒ Object



128
129
130
131
132
133
# File 'lib/sidekiq/metrics/query.rb', line 128

def series_avg(metric = "ms")
  series[metric].each_with_object(Hash.new(0)) do |(bucket, value), result|
    completed = series.dig("p", bucket) - series.dig("f", bucket)
    result[bucket] = (completed == 0) ? 0 : value.to_f / completed
  end
end

#total_avg(metric = "ms") ⇒ Object



122
123
124
125
126
# File 'lib/sidekiq/metrics/query.rb', line 122

def total_avg(metric = "ms")
  completed = totals["p"] - totals["f"]
  return 0 if completed.zero?
  totals[metric].to_f / completed
end