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([" select\n t.date_hour\n , app.id as app_id\n , app.name as app_name\n , def.id as job_id\n , def.job as job_name\n , t.cnt\n from\n (\n select\n date_format(e.created_at, '%Y-%m-%d %H:00:00') as date_hour\n , e.job_definition_id\n , count(1) as cnt\n from \#{Barbeque::JobExecution.table_name} e\n where\n e.created_at between ? and ?\n group by\n date_hour\n , e.job_definition_id\n ) t\n inner join \#{Barbeque::JobDefinition.table_name} def on def.id = t.job_definition_id\n inner join \#{Barbeque::App.table_name} app on app.id = def.app_id\n".strip_heredoc, from, now]).map(&:attributes)
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
|