2016-08-25 05:02:27 +00:00
|
|
|
class CycleAnalytics
|
2016-09-16 06:04:49 +00:00
|
|
|
include DatabaseMedian
|
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
|
|
|
@summary = Summary.new(project, from: from)
|
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
|
|
|
@summary
|
2016-08-26 10:15:00 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 05:02:27 +00:00
|
|
|
def issue
|
2016-09-16 06:04:49 +00:00
|
|
|
calculate_metric!(:issue,
|
|
|
|
TableReferences.issues[:created_at],
|
|
|
|
[TableReferences.issue_metrics[:first_associated_with_milestone_at],
|
2016-09-20 05:44:40 +00:00
|
|
|
TableReferences.issue_metrics[:first_added_to_board_at]])
|
2016-08-25 05:02:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def plan
|
2016-09-16 06:04:49 +00:00
|
|
|
calculate_metric!(:plan,
|
|
|
|
[TableReferences.issue_metrics[:first_associated_with_milestone_at],
|
|
|
|
TableReferences.issue_metrics[:first_added_to_board_at]],
|
2016-09-20 05:44:40 +00:00
|
|
|
TableReferences.issue_metrics[: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-16 06:04:49 +00:00
|
|
|
calculate_metric!(:code,
|
|
|
|
TableReferences.issue_metrics[:first_mentioned_in_commit_at],
|
2016-09-20 05:44:40 +00:00
|
|
|
TableReferences.merge_requests[:created_at])
|
2016-08-25 05:02:27 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 09:19:18 +00:00
|
|
|
def test
|
2016-09-16 06:04:49 +00:00
|
|
|
calculate_metric!(:test,
|
|
|
|
TableReferences.merge_request_metrics[:latest_build_started_at],
|
2016-09-20 05:44:40 +00:00
|
|
|
TableReferences.merge_request_metrics[: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-16 06:04:49 +00:00
|
|
|
calculate_metric!(:review,
|
|
|
|
TableReferences.merge_requests[:created_at],
|
2016-09-20 05:44:40 +00:00
|
|
|
TableReferences.merge_request_metrics[:merged_at])
|
2016-08-25 06:25:07 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 08:39:48 +00:00
|
|
|
def staging
|
2016-09-16 06:04:49 +00:00
|
|
|
calculate_metric!(:staging,
|
|
|
|
TableReferences.merge_request_metrics[:merged_at],
|
2016-09-20 05:44:40 +00:00
|
|
|
TableReferences.merge_request_metrics[: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-16 06:04:49 +00:00
|
|
|
calculate_metric!(:production,
|
|
|
|
TableReferences.issues[:created_at],
|
2016-09-20 05:44:40 +00:00
|
|
|
TableReferences.merge_request_metrics[: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 05:44:40 +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-16 06:04:49 +00:00
|
|
|
# Add a `SELECT` for (end_time - start-time), and add an alias for it.
|
|
|
|
# Note: We use COALESCE to pick up the first non-null column for end_time / start_time.
|
|
|
|
query = Arel::Nodes::As.new(
|
|
|
|
cte_table,
|
2016-09-20 05:44:40 +00:00
|
|
|
base_query.project(
|
2016-09-16 06:04:49 +00:00
|
|
|
Arel::Nodes::Subtraction.new(
|
|
|
|
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs)),
|
|
|
|
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(start_time_attrs))
|
|
|
|
).as(name.to_s)))
|
2016-08-25 05:02:27 +00:00
|
|
|
|
2016-09-16 06:04:49 +00:00
|
|
|
query = median_datetime(cte_table, query, name)
|
|
|
|
median = ActiveRecord::Base.connection.execute(query.to_sql).first['median']
|
|
|
|
median.to_f if median.present?
|
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-09-20 05:44:40 +00:00
|
|
|
def base_query
|
2016-09-16 06:04:49 +00:00
|
|
|
arel_table = TableReferences.merge_requests_closing_issues
|
|
|
|
|
2016-09-20 05:44:40 +00:00
|
|
|
# Load issues
|
2016-09-16 06:04:49 +00:00
|
|
|
query = arel_table.join(TableReferences.issues).on(TableReferences.issues[:id].eq(arel_table[:issue_id])).
|
|
|
|
join(TableReferences.issue_metrics).on(TableReferences.issues[:id].eq(TableReferences.issue_metrics[:issue_id])).
|
|
|
|
where(TableReferences.issues[:project_id].eq(@project.id)).
|
|
|
|
where(TableReferences.issues[:deleted_at].eq(nil)).
|
|
|
|
where(TableReferences.issues[:created_at].gteq(@from))
|
|
|
|
|
2016-09-20 05:44:40 +00:00
|
|
|
# Load merge_requests
|
|
|
|
query = query.join(TableReferences.merge_requests, Arel::Nodes::OuterJoin).on(TableReferences.merge_requests[:id].eq(arel_table[:merge_request_id])).
|
|
|
|
join(TableReferences.merge_request_metrics).on(TableReferences.merge_requests[:id].eq(TableReferences.merge_request_metrics[:merge_request_id]))
|
|
|
|
|
|
|
|
# Limit to merge requests that have been deployed to production after `@from`
|
|
|
|
query.where(TableReferences.merge_request_metrics[:first_deployed_to_production_at].gteq(@from))
|
2016-08-25 05:02:27 +00:00
|
|
|
end
|
|
|
|
end
|