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

WIP - refactored some arel queries

parent ec7db295
......@@ -11,14 +11,21 @@ module Gitlab
def fetch_issues
cte_table = Arel::Table.new("cte_table_for_issue")
interval_query = Arel::Nodes::As.new(
cte_table,
subtract_datetimes(base_query_for(:issue), *attributes, 'issue'))
# Build a `SELECT` query. We find the first of the `end_time_attrs` that isn't `NULL` (call this end_time).
# Next, we find the first of the start_time_attrs that isn't `NULL` (call this start_time).
# 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
def attributes
def metric_attributes
[issue_metrics_table[:first_associated_with_milestone_at],
issue_metrics_table[:first_added_to_board_at]]
end
......
......@@ -28,7 +28,7 @@ module Gitlab
# automatically excluded.
def base_query_for(name)
# 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])).
where(issue_table[:project_id].eq(@project.id)).
where(issue_table[:deleted_at].eq(nil)).
......
......@@ -8,7 +8,13 @@ module Gitlab
# Note: For MySQL, the interval is returned in seconds.
# For PostgreSQL, the interval is returned as an INTERVAL type.
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)
query_so_far.project(diff_fn.as(as))
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)))
......@@ -19,8 +25,6 @@ module Gitlab
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))
end
end
end
......
......@@ -15,12 +15,13 @@ describe Gitlab::CycleAnalytics::Events do
let!(:context) { create(:issue, project: project) }
xit 'does something' do
expect(subject.issue_events).to eq([])
expect(subject.issue_events).to eq([context])
end
end
def setup(context)
create(:milestone, project: project)
milestone = create(:milestone, project: project)
context.update(milestone: milestone)
create_merge_request_closing_issue(context)
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