2016-08-25 05:02:27 +00:00
|
|
|
class CycleAnalytics
|
2016-09-20 09:36:54 +00:00
|
|
|
include Gitlab::Database::Median
|
2016-09-20 12:43:11 +00:00
|
|
|
include Gitlab::Database::DateTime
|
2016-08-26 10:48:52 +00:00
|
|
|
|
2016-10-11 10:58:56 +00:00
|
|
|
DEPLOYED_CHECK_METRICS = %i[production staging]
|
|
|
|
|
2016-08-26 10:48:52 +00:00
|
|
|
def initialize(project, from:)
|
2016-09-16 06:04:49 +00:00
|
|
|
@project = project
|
2016-08-26 10:48:52 +00:00
|
|
|
@from = from
|
2016-09-19 09:30:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
2016-09-20 19:17:37 +00:00
|
|
|
@summary ||= Summary.new(@project, from: @from)
|
2016-08-26 10:15:00 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 05:02:27 +00:00
|
|
|
def issue
|
2016-09-20 12:43:11 +00:00
|
|
|
calculate_metric(:issue,
|
2016-09-20 19:17:37 +00: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 05:02:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def plan
|
2016-09-20 12:43:11 +00:00
|
|
|
calculate_metric(:plan,
|
2016-09-20 19:17:37 +00: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 05:54:41 +00:00
|
|
|
end
|
2016-08-25 05:02:27 +00:00
|
|
|
|
2016-08-25 05:54:41 +00:00
|
|
|
def code
|
2016-09-20 12:43:11 +00:00
|
|
|
calculate_metric(:code,
|
2016-09-20 19:17:37 +00:00
|
|
|
Issue::Metrics.arel_table[:first_mentioned_in_commit_at],
|
|
|
|
MergeRequest.arel_table[:created_at])
|
2016-08-25 05:02:27 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 09:19:18 +00:00
|
|
|
def test
|
2016-09-20 12:43:11 +00:00
|
|
|
calculate_metric(:test,
|
2016-09-20 19:17:37 +00:00
|
|
|
MergeRequest::Metrics.arel_table[:latest_build_started_at],
|
|
|
|
MergeRequest::Metrics.arel_table[:latest_build_finished_at])
|
2016-08-25 09:19:18 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 06:25:07 +00:00
|
|
|
def review
|
2016-09-20 12:43:11 +00:00
|
|
|
calculate_metric(:review,
|
2016-09-20 19:17:37 +00:00
|
|
|
MergeRequest.arel_table[:created_at],
|
|
|
|
MergeRequest::Metrics.arel_table[:merged_at])
|
2016-08-25 06:25:07 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 08:39:48 +00:00
|
|
|
def staging
|
2016-09-20 12:43:11 +00:00
|
|
|
calculate_metric(:staging,
|
2016-09-20 19:17:37 +00:00
|
|
|
MergeRequest::Metrics.arel_table[:merged_at],
|
|
|
|
MergeRequest::Metrics.arel_table[:first_deployed_to_production_at])
|
2016-08-25 08:39:48 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 09:06:01 +00:00
|
|
|
def production
|
2016-09-20 12:43:11 +00:00
|
|
|
calculate_metric(:production,
|
2016-09-20 19:17:37 +00:00
|
|
|
Issue.arel_table[:created_at],
|
|
|
|
MergeRequest::Metrics.arel_table[:first_deployed_to_production_at])
|
2016-08-25 09:06:01 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 05:02:27 +00:00
|
|
|
private
|
|
|
|
|
2016-09-20 12:43:11 +00:00
|
|
|
def calculate_metric(name, start_time_attrs, end_time_attrs)
|
2016-09-16 06:04:49 +00:00
|
|
|
cte_table = Arel::Table.new("cte_table_for_#{name}")
|
2016-08-25 05:02:27 +00:00
|
|
|
|
2016-09-20 12:43:11 +00: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,
|
2016-10-11 10:58:56 +00:00
|
|
|
subtract_datetimes(base_query_for(name), end_time_attrs, start_time_attrs, name.to_s))
|
2016-09-20 12:43:11 +00:00
|
|
|
|
2016-09-20 19:17:37 +00:00
|
|
|
median_datetime(cte_table, interval_query, name)
|
2016-08-25 05:02:27 +00:00
|
|
|
end
|
|
|
|
|
2016-09-16 06:04:49 +00: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-10-11 10:58:56 +00:00
|
|
|
def base_query_for(name)
|
2016-09-20 19:17:37 +00:00
|
|
|
arel_table = MergeRequestsClosingIssues.arel_table
|
2016-09-16 06:04:49 +00:00
|
|
|
|
2016-09-20 05:44:40 +00:00
|
|
|
# Load issues
|
2016-09-20 19:17:37 +00: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 06:04:49 +00:00
|
|
|
|
2016-09-20 05:44:40 +00:00
|
|
|
# Load merge_requests
|
2016-09-20 19:17:37 +00: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 05:44:40 +00:00
|
|
|
|
2016-10-11 10:58:56 +00:00
|
|
|
if DEPLOYED_CHECK_METRICS.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
|
2016-09-20 07:03:37 +00:00
|
|
|
end
|
2016-08-25 05:02:27 +00:00
|
|
|
end
|