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.



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

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



100
101
102
# File 'lib/sidekiq/metrics/query.rb', line 100

def hist
  @hist
end

#seriesObject

Returns the value of attribute series

Returns:

  • (Object)

    the current value of series



100
101
102
# File 'lib/sidekiq/metrics/query.rb', line 100

def series
  @series
end

#totalsObject

Returns the value of attribute totals

Returns:

  • (Object)

    the current value of totals



100
101
102
# File 'lib/sidekiq/metrics/query.rb', line 100

def totals
  @totals
end

Instance Method Details

#add_hist(time, hist_result) ⇒ Object



116
117
118
# File 'lib/sidekiq/metrics/query.rb', line 116

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

#add_metric(metric, time, value) ⇒ Object



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

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



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

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



120
121
122
123
124
# File 'lib/sidekiq/metrics/query.rb', line 120

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