Class: Wurk::Metrics::Query::JobResult

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(granularity = :minutely) ⇒ JobResult

Returns a new instance of JobResult.



80
81
82
83
84
85
86
# File 'lib/wurk/metrics/query.rb', line 80

def initialize(granularity = :minutely)
  super
  self.granularity = granularity
  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

#granularity ⇒ Object

Returns the value of attribute granularity

Returns:

  • (Object) —

    the current value of granularity



79
80
81
# File 'lib/wurk/metrics/query.rb', line 79

def granularity
  @granularity
end

#hist ⇒ Object

Returns the value of attribute hist

Returns:

  • (Object) —

    the current value of hist



79
80
81
# File 'lib/wurk/metrics/query.rb', line 79

def hist
  @hist
end

#series ⇒ Object

Returns the value of attribute series

Returns:

  • (Object) —

    the current value of series



79
80
81
# File 'lib/wurk/metrics/query.rb', line 79

def series
  @series
end

#totals ⇒ Object

Returns the value of attribute totals

Returns:

  • (Object) —

    the current value of totals



79
80
81
# File 'lib/wurk/metrics/query.rb', line 79

def totals
  @totals
end

Instance Method Details

#add_hist(time, hist_result) ⇒ Object



95
96
97
# File 'lib/wurk/metrics/query.rb', line 95

def add_hist(time, hist_result)
  hist[Query.bkt_time_s(time, granularity)] = hist_result
end

#add_metric(metric, time, value) ⇒ Object

An ms sample also books the same time in seconds under s.



89
90
91
92
93
# File 'lib/wurk/metrics/query.rb', line 89

def add_metric(metric, time, value)
  totals[metric] += value
  series[metric][Query.bkt_time_s(time, granularity)] += value
  add_metric('s', time, value / 1000.0) if metric == 'ms'
end

#series_avg(metric = 'ms') ⇒ Object



107
108
109
110
111
112
# File 'lib/wurk/metrics/query.rb', line 107

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

#total_avg(metric = 'ms') ⇒ Object

Per successful execution: p counts failures too.



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

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

  totals[metric].to_f / completed
end