Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Arel::Nodes::NamedFunction to fix COALESCE timestamp generation #1306

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion app/charts/good_job/scheduled_by_queue_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ def data
start_time = end_time - 1.day
table_name = GoodJob::Job.table_name

scheduled_query = @filter.filtered_query
.except(:select, :order)
.select(
:queue_name,
Arel::Nodes::NamedFunction.new(
'CAST',
[
Arel::Nodes::NamedFunction.new(
'COALESCE',
[Arel::Nodes::SqlLiteral.new("#{table_name}.scheduled_at"), Arel::Nodes::SqlLiteral.new("#{table_name}.created_at")]
).as('timestamp'),
]
).as('scheduled_at')
).to_sql

count_query = Arel.sql(GoodJob::Execution.pg_or_jdbc_query(<<~SQL.squish))
SELECT *
FROM generate_series(
Expand All @@ -24,7 +39,7 @@ def data
queue_name,
count(*) AS count
FROM (
#{@filter.filtered_query.except(:select, :order).select('queue_name', "COALESCE(#{table_name}.scheduled_at, #{table_name}.created_at)::timestamp AS scheduled_at").to_sql}
#{scheduled_query}
) sources
GROUP BY date_trunc('hour', scheduled_at), queue_name
) sources ON sources.scheduled_at = timestamp
Expand Down