Class: Barbeque::JobDefinition

Inherits:
ApplicationRecord show all
Defined in:
app/models/barbeque/job_definition.rb

Constant Summary collapse

DATE_HOUR_SQL =
'date_format(created_at, "%Y-%m-%d %H:00:00")'

Instance Method Summary collapse

Instance Method Details

#execution_stats(from, to) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/barbeque/job_definition.rb', line 20

def execution_stats(from, to)
  from = from.beginning_of_hour
  to = to.beginning_of_hour
  stats = Hash.new { |h, k| h[k] = { count: 0, avg_time: 0 } }
  job_executions.where(created_at: from .. to).group(DATE_HOUR_SQL).order(Arel.sql(DATE_HOUR_SQL)).pluck(Arel.sql("#{DATE_HOUR_SQL}, count(1), avg(timestampdiff(second, created_at, finished_at))")).each do |date_hour, count, avg_time|
    time = Time.zone.parse("#{date_hour} UTC")
    stats[time] = {
      count: count,
      avg_time: avg_time,
    }
  end
  (from.to_i ... to.to_i).step(1.hour.to_i).map do |t|
    time = Time.at(t)
    stats[time].merge(date_hour: time)
  end
end