Class: GoodJob::PerformanceIndexChart

Inherits:
BaseChart
  • Object
show all
Defined in:
app/charts/good_job/performance_index_chart.rb

Instance Method Summary collapse

Methods inherited from BaseChart

#start_end_binds, #string_to_hsl

Instance Method Details

#dataObject



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
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/charts/good_job/performance_index_chart.rb', line 5

def data
  table_name = GoodJob::Execution.table_name

  sum_query = "    SELECT *\n    FROM generate_series(\n      date_trunc('hour', $1::timestamp),\n      date_trunc('hour', $2::timestamp),\n      '1 hour'\n    ) timestamp\n    LEFT JOIN (\n      SELECT\n          date_trunc('hour', scheduled_at) AS scheduled_at,\n          job_class,\n          SUM(duration) AS sum\n        FROM \#{table_name} sources\n        GROUP BY date_trunc('hour', scheduled_at), job_class\n    ) sources ON sources.scheduled_at = timestamp\n    ORDER BY timestamp ASC\n  SQL\n\n  executions_data = GoodJob::Job.connection.exec_query(GoodJob::Job.pg_or_jdbc_query(sum_query), \"GoodJob Performance Chart\", start_end_binds)\n\n  job_names = executions_data.reject { |d| d['sum'].nil? }.map { |d| d['job_class'] || BaseFilter::EMPTY }.uniq\n  labels = []\n  jobs_data = executions_data.to_a.group_by { |d| d['timestamp'] }.each_with_object({}) do |(timestamp, values), hash|\n    labels << timestamp.in_time_zone.strftime('%H:%M')\n    job_names.each do |job_class|\n      sum = values.find { |d| d['job_class'] == job_class }&.[]('sum')\n      duration = sum ? ActiveSupport::Duration.parse(sum).to_f : 0\n      (hash[job_class] ||= []) << duration\n    end\n  end\n\n  {\n    type: \"line\",\n    data: {\n      labels: labels,\n      datasets: jobs_data.map do |job_class, data|\n        label = job_class || '(none)'\n        {\n          label: label,\n          data: data,\n          backgroundColor: string_to_hsl(label),\n          borderColor: string_to_hsl(label),\n        }\n      end,\n    },\n    options: {\n      plugins: {\n        title: {\n          display: true,\n          text: I18n.t(\"good_job.performance.index.chart_title\"),\n        },\n        legend: {\n          vertical: true,\n        },\n      },\n      scales: {\n        y: {\n          beginAtZero: true,\n        },\n      },\n    },\n  }\nend\n".squish