Commit 470e39d6 authored by James Lopez's avatar James Lopez

WIP - refactored some arel queries

parent ec7db295
...@@ -11,14 +11,21 @@ module Gitlab ...@@ -11,14 +11,21 @@ module Gitlab
def fetch_issues def fetch_issues
cte_table = Arel::Table.new("cte_table_for_issue") cte_table = Arel::Table.new("cte_table_for_issue")
interval_query = Arel::Nodes::As.new( # Build a `SELECT` query. We find the first of the `end_time_attrs` that isn't `NULL` (call this end_time).
cte_table, # Next, we find the first of the start_time_attrs that isn't `NULL` (call this start_time).
subtract_datetimes(base_query_for(:issue), *attributes, 'issue')) # We compute the (end_time - start_time) interval, and give it an alias based on the current
# cycle analytics stage.
#TODO ActiveRecord::Base.connection.execute(interval_query) base_query = base_query_for(:issue)
diff_fn = subtract_datetimes_diff(base_query, issue_table[:created_at], metric_attributes)
query = base_query.project(diff_fn.as('issue_diff'))
ActiveRecord::Base.connection.execute(query.to_sql)
end end
def attributes def metric_attributes
[issue_metrics_table[:first_associated_with_milestone_at], [issue_metrics_table[:first_associated_with_milestone_at],
issue_metrics_table[:first_added_to_board_at]] issue_metrics_table[:first_added_to_board_at]]
end end
......
...@@ -28,7 +28,7 @@ module Gitlab ...@@ -28,7 +28,7 @@ module Gitlab
# automatically excluded. # automatically excluded.
def base_query_for(name) def base_query_for(name)
# Load issues # Load issues
query = issue_metrics_table.join(issue_table).on(issue_table[:id].eq(mr_closing_issues_table[:issue_id])). query = mr_closing_issues_table.join(issue_table).on(issue_table[:id].eq(mr_closing_issues_table[:issue_id])).
join(issue_metrics_table).on(issue_table[:id].eq(issue_metrics_table[:issue_id])). join(issue_metrics_table).on(issue_table[:id].eq(issue_metrics_table[:issue_id])).
where(issue_table[:project_id].eq(@project.id)). where(issue_table[:project_id].eq(@project.id)).
where(issue_table[:deleted_at].eq(nil)). where(issue_table[:deleted_at].eq(nil)).
......
...@@ -8,20 +8,24 @@ module Gitlab ...@@ -8,20 +8,24 @@ module Gitlab
# Note: For MySQL, the interval is returned in seconds. # Note: For MySQL, the interval is returned in seconds.
# For PostgreSQL, the interval is returned as an INTERVAL type. # For PostgreSQL, the interval is returned as an INTERVAL type.
def subtract_datetimes(query_so_far, end_time_attrs, start_time_attrs, as) def subtract_datetimes(query_so_far, end_time_attrs, start_time_attrs, as)
diff_fn = if Gitlab::Database.postgresql? diff_fn = subtract_datetimes_diff(query_so_far, end_time_attrs, start_time_attrs)
Arel::Nodes::Subtraction.new(
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs)),
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(start_time_attrs)))
elsif Gitlab::Database.mysql?
Arel::Nodes::NamedFunction.new(
"TIMESTAMPDIFF",
[Arel.sql('second'),
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(start_time_attrs)),
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs))])
end
query_so_far.project(diff_fn.as(as)) query_so_far.project(diff_fn.as(as))
end end
def subtract_datetimes_diff(query_so_far, end_time_attrs, start_time_attrs)
if Gitlab::Database.postgresql?
Arel::Nodes::Subtraction.new(
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs)),
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(start_time_attrs)))
elsif Gitlab::Database.mysql?
Arel::Nodes::NamedFunction.new(
"TIMESTAMPDIFF",
[Arel.sql('second'),
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(start_time_attrs)),
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs))])
end
end
end end
end end
end end
...@@ -15,12 +15,13 @@ describe Gitlab::CycleAnalytics::Events do ...@@ -15,12 +15,13 @@ describe Gitlab::CycleAnalytics::Events do
let!(:context) { create(:issue, project: project) } let!(:context) { create(:issue, project: project) }
xit 'does something' do xit 'does something' do
expect(subject.issue_events).to eq([]) expect(subject.issue_events).to eq([context])
end end
end end
def setup(context) def setup(context)
create(:milestone, project: project) milestone = create(:milestone, project: project)
context.update(milestone: milestone)
create_merge_request_closing_issue(context) create_merge_request_closing_issue(context)
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment