2016-08-25 01:02:27 -04:00
|
|
|
class CycleAnalytics
|
2016-09-20 05:36:54 -04:00
|
|
|
include Gitlab::Database::Median
|
2016-09-20 08:43:11 -04:00
|
|
|
include Gitlab::Database::DateTime
|
2016-08-26 06:48:52 -04:00
|
|
|
|
|
|
|
def initialize(project, from:)
|
2016-09-16 02:04:49 -04:00
|
|
|
@project = project
|
2016-08-26 06:48:52 -04:00
|
|
|
@from = from
|
2016-09-19 05:30:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
2016-09-20 15:17:37 -04:00
|
|
|
@summary ||= Summary.new(@project, from: @from)
|
2016-08-26 06:15:00 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 01:02:27 -04:00
|
|
|
def issue
|
2016-09-20 08:43:11 -04:00
|
|
|
calculate_metric(:issue,
|
2016-09-20 15:17:37 -04:00
|
|
|
Issue.arel_table[:created_at],
|
|
|
|
[Issue::Metrics.arel_table[:first_associated_with_milestone_at],
|
|
|
|
Issue::Metrics.arel_table[:first_added_to_board_at]])
|
2016-08-25 01:02:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def plan
|
2016-09-20 08:43:11 -04:00
|
|
|
calculate_metric(:plan,
|
2016-09-20 15:17:37 -04:00
|
|
|
[Issue::Metrics.arel_table[:first_associated_with_milestone_at],
|
|
|
|
Issue::Metrics.arel_table[:first_added_to_board_at]],
|
|
|
|
Issue::Metrics.arel_table[:first_mentioned_in_commit_at])
|
2016-08-25 01:54:41 -04:00
|
|
|
end
|
2016-08-25 01:02:27 -04:00
|
|
|
|
2016-08-25 01:54:41 -04:00
|
|
|
def code
|
2016-09-20 08:43:11 -04:00
|
|
|
calculate_metric(:code,
|
2016-09-20 15:17:37 -04:00
|
|
|
Issue::Metrics.arel_table[:first_mentioned_in_commit_at],
|
|
|
|
MergeRequest.arel_table[:created_at])
|
2016-08-25 01:02:27 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 05:19:18 -04:00
|
|
|
def test
|
2016-09-20 08:43:11 -04:00
|
|
|
calculate_metric(:test,
|
2016-09-20 15:17:37 -04:00
|
|
|
MergeRequest::Metrics.arel_table[:latest_build_started_at],
|
|
|
|
MergeRequest::Metrics.arel_table[:latest_build_finished_at])
|
2016-08-25 05:19:18 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 02:25:07 -04:00
|
|
|
def review
|
2016-09-20 08:43:11 -04:00
|
|
|
calculate_metric(:review,
|
2016-09-20 15:17:37 -04:00
|
|
|
MergeRequest.arel_table[:created_at],
|
|
|
|
MergeRequest::Metrics.arel_table[:merged_at])
|
2016-08-25 02:25:07 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 04:39:48 -04:00
|
|
|
def staging
|
2016-09-20 08:43:11 -04:00
|
|
|
calculate_metric(:staging,
|
2016-09-20 15:17:37 -04:00
|
|
|
MergeRequest::Metrics.arel_table[:merged_at],
|
|
|
|
MergeRequest::Metrics.arel_table[:first_deployed_to_production_at])
|
2016-08-25 04:39:48 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 05:06:01 -04:00
|
|
|
def production
|
2016-09-20 08:43:11 -04:00
|
|
|
calculate_metric(:production,
|
2016-09-20 15:17:37 -04:00
|
|
|
Issue.arel_table[:created_at],
|
|
|
|
MergeRequest::Metrics.arel_table[:first_deployed_to_production_at])
|
2016-08-25 05:06:01 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 01:02:27 -04:00
|
|
|
private
|
|
|
|
|
2016-09-20 08:43:11 -04:00
|
|
|
def calculate_metric(name, start_time_attrs, end_time_attrs)
|
2016-09-16 02:04:49 -04:00
|
|
|
cte_table = Arel::Table.new("cte_table_for_#{name}")
|
2016-08-25 01:02:27 -04:00
|
|
|
|
2016-09-20 08:43:11 -04:00
|
|
|
# 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, end_time_attrs, start_time_attrs, name.to_s))
|
|
|
|
|
2016-09-20 15:17:37 -04:00
|
|
|
median_datetime(cte_table, interval_query, name)
|
2016-08-25 01:02:27 -04:00
|
|
|
end
|
|
|
|
|
2016-09-16 02:04:49 -04:00
|
|
|
# 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.
|
2016-09-20 01:44:40 -04:00
|
|
|
def base_query
|
2016-09-20 15:17:37 -04:00
|
|
|
arel_table = MergeRequestsClosingIssues.arel_table
|
2016-09-16 02:04:49 -04:00
|
|
|
|
2016-09-20 01:44:40 -04:00
|
|
|
# Load issues
|
2016-09-20 15:17:37 -04:00
|
|
|
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))
|
2016-09-16 02:04:49 -04:00
|
|
|
|
2016-09-20 01:44:40 -04:00
|
|
|
# Load merge_requests
|
2016-09-20 15:17:37 -04:00
|
|
|
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]))
|
2016-09-20 01:44:40 -04:00
|
|
|
|
|
|
|
# Limit to merge requests that have been deployed to production after `@from`
|
2016-09-20 15:17:37 -04:00
|
|
|
query.where(MergeRequest::Metrics.arel_table[:first_deployed_to_production_at].gteq(@from))
|
2016-09-20 03:03:37 -04:00
|
|
|
end
|
2016-08-25 01:02:27 -04:00
|
|
|
end
|