Class: Barbeque::MonitorsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/barbeque/monitors_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/barbeque/monitors_controller.rb', line 2

def index
  now = Time.zone.now
  from = 6.hours.ago(now.beginning_of_hour)
  rows = Barbeque::JobExecution.find_by_sql([<<SQL.strip_heredoc, from, now]).map(&:attributes)
  select
    t.date_hour
    , app.id as app_id
    , app.name as app_name
    , def.id as job_id
    , def.job as job_name
    , t.cnt
  from
    (
      select
        date_format(e.created_at, '%Y-%m-%d %H:00:00') as date_hour
        , e.job_definition_id
        , count(1) as cnt
      from #{Barbeque::JobExecution.table_name} e
      where
        e.created_at between ? and ?
      group by
        date_hour
        , e.job_definition_id
    ) t
    inner join #{Barbeque::JobDefinition.table_name} def on def.id = t.job_definition_id
    inner join #{Barbeque::App.table_name} app on app.id = def.app_id
SQL

  jobs = {}
  rows.each do |row|
    job_id = row.fetch('job_id')
    job = {
      app_id: row.fetch('app_id'),
      app_name: row.fetch('app_name'),
      job_id: job_id,
      job_name: row.fetch('job_name'),
    }
    jobs[job_id] = job
  end

  @recently_processed_jobs = {}
  t = from
  while t < now
    @recently_processed_jobs[t] = {}
    jobs.each do |job_id, job|
      @recently_processed_jobs[t][job_id] = job.merge(count: 0)
    end
    t += 1.hour
  end

  rows.each do |row|
    date_hour = Time.zone.parse("#{row.fetch('date_hour')} UTC")
    job_id = row.fetch('job_id')
    @recently_processed_jobs[date_hour][job_id] = jobs[job_id].merge(count: row.fetch('cnt'))
  end
end