Commit ec7db295 authored by James Lopez's avatar James Lopez

Started refactoring stuff to add events to cycle analytics

- Refactored cycle analytics class to extract DB logic
- Reuse logic in new events fetcher
- Started adding cycle analytics events class and spec (still not functional)
parent 60306053
No related merge requests found
class CycleAnalytics class CycleAnalytics
include Gitlab::Database::Median include Gitlab::CycleAnalytics::MetricsFetcher
include Gitlab::Database::DateTime
DEPLOYMENT_METRIC_STAGES = %i[production staging]
def initialize(project, from:) def initialize(project, from:)
@project = project @project = project
...@@ -56,48 +53,4 @@ class CycleAnalytics ...@@ -56,48 +53,4 @@ class CycleAnalytics
Issue.arel_table[:created_at], Issue.arel_table[:created_at],
MergeRequest::Metrics.arel_table[:first_deployed_to_production_at]) MergeRequest::Metrics.arel_table[:first_deployed_to_production_at])
end end
private
def calculate_metric(name, start_time_attrs, end_time_attrs)
cte_table = Arel::Table.new("cte_table_for_#{name}")
# 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.
interval_query = Arel::Nodes::As.new(
cte_table,
subtract_datetimes(base_query_for(name), end_time_attrs, start_time_attrs, name.to_s))
median_datetime(cte_table, interval_query, name)
end
# Join table with a row for every <issue,merge_request> pair (where the merge request
# closes the given issue) with issue and merge request metrics included. The metrics
# are loaded with an inner join, so issues / merge requests without metrics are
# automatically excluded.
def base_query_for(name)
arel_table = MergeRequestsClosingIssues.arel_table
# Load issues
query = arel_table.join(Issue.arel_table).on(Issue.arel_table[:id].eq(arel_table[:issue_id])).
join(Issue::Metrics.arel_table).on(Issue.arel_table[:id].eq(Issue::Metrics.arel_table[:issue_id])).
where(Issue.arel_table[:project_id].eq(@project.id)).
where(Issue.arel_table[:deleted_at].eq(nil)).
where(Issue.arel_table[:created_at].gteq(@from))
# Load merge_requests
query = query.join(MergeRequest.arel_table, Arel::Nodes::OuterJoin).
on(MergeRequest.arel_table[:id].eq(arel_table[:merge_request_id])).
join(MergeRequest::Metrics.arel_table).
on(MergeRequest.arel_table[:id].eq(MergeRequest::Metrics.arel_table[:merge_request_id]))
if DEPLOYMENT_METRIC_STAGES.include?(name)
# Limit to merge requests that have been deployed to production after `@from`
query.where(MergeRequest::Metrics.arel_table[:first_deployed_to_production_at].gteq(@from))
end
query
end
end end
module Gitlab
module CycleAnalytics
class Events
def initialize(project:, from:)
@project = project
@from = from
@fetcher = EventsFetcher.new(project: project, from: from)
end
def issue_events
@fetcher.fetch_issues
end
end
end
end
module Gitlab
module CycleAnalytics
class EventsFetcher
include MetricsFetcher
def initialize(project:, from:)
@project = project
@from = from
end
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'))
#TODO ActiveRecord::Base.connection.execute(interval_query)
end
def attributes
[issue_metrics_table[:first_associated_with_milestone_at],
issue_metrics_table[:first_added_to_board_at]]
end
end
end
end
module Gitlab
module CycleAnalytics
module MetricsFetcher
include Gitlab::Database::Median
include Gitlab::Database::DateTime
DEPLOYMENT_METRIC_STAGES = %i[production staging]
private
def calculate_metric(name, start_time_attrs, end_time_attrs)
cte_table = Arel::Table.new("cte_table_for_#{name}")
# 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.
interval_query = Arel::Nodes::As.new(
cte_table,
subtract_datetimes(base_query_for(name), end_time_attrs, start_time_attrs, name.to_s))
median_datetime(cte_table, interval_query, name)
end
# Join table with a row for every <issue,merge_request> pair (where the merge request
# closes the given issue) with issue and merge request metrics included. The metrics
# are loaded with an inner join, so issues / merge requests without metrics are
# 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])).
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)).
where(issue_table[:created_at].gteq(@from))
# Load merge_requests
query = query.join(merge_request_table, Arel::Nodes::OuterJoin).
on(merge_request_table[:id].eq(mr_closing_issues_table[:merge_request_id])).
join(merge_request_metrics_table).
on(merge_request_table[:id].eq(merge_request_metrics_table[:merge_request_id]))
if DEPLOYMENT_METRIC_STAGES.include?(name)
# Limit to merge requests that have been deployed to production after `@from`
query.where(merge_request_metrics_table[:first_deployed_to_production_at].gteq(@from))
end
query
end
def merge_request_metrics_table
MergeRequest::Metrics.arel_table
end
def merge_request_table
MergeRequest.arel_table
end
def mr_closing_issues_table
MergeRequestsClosingIssues.arel_table
end
def issue_table
Issue.arel_table
end
def issue_metrics_table
Issue::Metrics.arel_table
end
end
end
end
require 'spec_helper'
describe Gitlab::CycleAnalytics::Events do
let(:project) { create(:project) }
let(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) }
subject { described_class.new(project: project, from: from_date) }
before do
setup(context)
end
describe '#issue' do
let!(:context) { create(:issue, project: project) }
xit 'does something' do
expect(subject.issue_events).to eq([])
end
end
def setup(context)
create(:milestone, project: project)
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