Class: GoodJob::PerformanceShowChart

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

Constant Summary collapse

BUCKET_INTERVALS =

These numbers are lifted from Sidekiq

[
  0.02, 0.03, 0.045, 0.065, 0.1,
  0.15, 0.225, 0.335, 0.5, 0.75,
  1.1, 1.7, 2.5, 3.8, 5.75,
  8.5, 13, 20, 30, 45,
  65, 100, 150, 225, 335,
  10**8 # About 3 years
].freeze

Instance Method Summary collapse

Methods inherited from BaseChart

#start_end_binds, #string_to_hsl

Constructor Details

#initialize(job_class) ⇒ PerformanceShowChart

Returns a new instance of PerformanceShowChart.



15
16
17
18
# File 'app/charts/good_job/performance_show_chart.rb', line 15

def initialize(job_class)
  super()
  @job_class = job_class
end

Instance Method Details

#dataObject



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
# File 'app/charts/good_job/performance_show_chart.rb', line 20

def data
  table_name = GoodJob::Execution.table_name

  interval_entries = BUCKET_INTERVALS.map { "interval '#{_1}'" }.join(",")
  sum_query = Arel.sql(GoodJob::Job.pg_or_jdbc_query("    SELECT\n      WIDTH_BUCKET(duration, ARRAY[\#{interval_entries}]) as bucket_index,\n      COUNT(WIDTH_BUCKET(duration, ARRAY[\#{interval_entries}])) AS count\n    FROM \#{table_name} sources\n    WHERE\n      scheduled_at > $1::timestamp\n      AND scheduled_at < $2::timestamp\n      AND job_class = $3\n      AND duration IS NOT NULL\n    GROUP BY bucket_index\n  SQL\n\n  binds = [\n    *start_end_binds,\n    @job_class,\n  ]\n  labels = BUCKET_INTERVALS.map { |interval| GoodJob::ApplicationController.helpers.format_duration(interval) }\n  labels[-1] = I18n.t(\"good_job.performance.show.slow\")\n  executions_data = GoodJob::Job.connection.exec_query(GoodJob::Job.pg_or_jdbc_query(sum_query), \"GoodJob Performance Job Chart\", binds)\n  executions_data = executions_data.to_a.index_by { |data| data[\"bucket_index\"] }\n\n  bucket_data = 0.upto(BUCKET_INTERVALS.count).map do |bucket_index|\n    executions_data.dig(bucket_index, \"count\") || 0\n  end\n\n  {\n    type: \"bar\",\n    data: {\n      labels: labels,\n      datasets: [{\n        label: @job_class,\n        data: bucket_data,\n        backgroundColor: string_to_hsl(@job_class),\n        borderColor: string_to_hsl(@job_class),\n      }],\n    },\n    options: {\n      scales: {\n        y: {\n          beginAtZero: true,\n        },\n      },\n    },\n  }\nend\n".squish))