Commit 3d5729a7 authored by Timothy Andrew's avatar Timothy Andrew

Add the "Production" cycle analytics section.

1. Rewrite the `Queries` module to work off a `data_point` hash, with
   `issue` and `merge_request` as keys. The "production" query needs
   both an issue and a merge request to make it's calculation, so it
   makes sense to keep things consistent and provide the same
   data (issue + merge request) for all queries.
parent d3fef0fb
class CycleAnalytics class CycleAnalytics
def issue def issue
issues = Issue.includes(:metrics).where("issue_metrics.id IS NOT NULL").references(:issue_metrics).to_a calculate_metric(Queries::issues,
start_time_fn = -> (issue) { issue.created_at } -> (data_point) { data_point[:issue].created_at },
calculate_metric(issues, start_time_fn, Queries::issue_first_associated_with_milestone_or_first_added_to_list_label_time) Queries::issue_first_associated_with_milestone_or_first_added_to_list_label_time)
end end
def plan def plan
issues = Issue.includes(:metrics).where("issue_metrics.id IS NOT NULL").references(:issue_metrics).to_a calculate_metric(Queries::issues,
calculate_metric(issues,
Queries::issue_first_associated_with_milestone_or_first_added_to_list_label_time, Queries::issue_first_associated_with_milestone_or_first_added_to_list_label_time,
Queries::issue_closing_merge_request_opened_time) Queries::issue_closing_merge_request_opened_time)
end end
def code def code
issues = Issue.all.to_a calculate_metric(Queries::merge_requests_closing_issues,
start_time_fn = -> (merge_request) { merge_request.created_at } -> (data_point) { data_point[:merge_request].created_at },
calculate_metric(Queries::merge_requests_closing_issues(issues),
start_time_fn,
Queries::mr_wip_flag_removed_or_assigned_to_user_other_than_author_time) Queries::mr_wip_flag_removed_or_assigned_to_user_other_than_author_time)
end end
def review def review
issues = Issue.all.to_a calculate_metric(Queries::merge_requests_closing_issues,
calculate_metric(Queries::merge_requests_closing_issues(issues),
Queries::mr_wip_flag_removed_or_assigned_to_user_other_than_author_time, Queries::mr_wip_flag_removed_or_assigned_to_user_other_than_author_time,
Queries::mr_first_closed_or_merged_at) Queries::mr_first_closed_or_merged_at)
end end
def staging def staging
issues = Issue.all.to_a calculate_metric(Queries::merge_requests_closing_issues,
calculate_metric(Queries::merge_requests_closing_issues(issues),
Queries::mr_merged_at, Queries::mr_merged_at,
Queries::mr_deployed_to_any_environment_at) Queries::mr_deployed_to_any_environment_at)
end end
def production
calculate_metric(Queries::merge_requests_closing_issues,
-> (data_point) { data_point[:issue].created_at },
Queries::mr_deployed_to_production_at)
end
private private
def calculate_metric(data, start_time_fn, end_time_fn) def calculate_metric(data, start_time_fn, end_time_fn)
......
class CycleAnalytics class CycleAnalytics
module Queries module Queries
class << self class << self
def merge_requests_closing_issues(issues) def issues
issues.map { |issue| issue.closed_by_merge_requests(nil, check_if_open: false) }.flatten Issue.all.to_a.map { |issue| { issue: issue } }
end
def merge_requests_closing_issues
issues.map do |data_point|
merge_requests = data_point[:issue].closed_by_merge_requests(nil, check_if_open: false)
merge_requests.map { |merge_request| { issue: data_point[:issue], merge_request: merge_request } }
end.flatten
end end
def issue_first_associated_with_milestone_or_first_added_to_list_label_time def issue_first_associated_with_milestone_or_first_added_to_list_label_time
lambda do |issue| lambda do |data_point|
issue = data_point[:issue]
if issue.metrics.present? if issue.metrics.present?
issue.metrics.first_associated_with_milestone_at.presence || issue.metrics.first_associated_with_milestone_at.presence ||
issue.metrics.first_added_to_board_at.presence issue.metrics.first_added_to_board_at.presence
...@@ -15,7 +23,8 @@ class CycleAnalytics ...@@ -15,7 +23,8 @@ class CycleAnalytics
end end
def mr_first_closed_or_merged_at def mr_first_closed_or_merged_at
lambda do |merge_request| lambda do |data_point|
merge_request = data_point[:merge_request]
if merge_request.metrics.present? if merge_request.metrics.present?
merge_request.metrics.merged_at.presence || merge_request.metrics.first_closed_at.presence merge_request.metrics.merged_at.presence || merge_request.metrics.first_closed_at.presence
end end
...@@ -23,7 +32,8 @@ class CycleAnalytics ...@@ -23,7 +32,8 @@ class CycleAnalytics
end end
def mr_merged_at def mr_merged_at
lambda do |merge_request| lambda do |data_point|
merge_request = data_point[:merge_request]
if merge_request.metrics.present? if merge_request.metrics.present?
merge_request.metrics.merged_at merge_request.metrics.merged_at
end end
...@@ -31,7 +41,8 @@ class CycleAnalytics ...@@ -31,7 +41,8 @@ class CycleAnalytics
end end
def mr_deployed_to_any_environment_at def mr_deployed_to_any_environment_at
lambda do |merge_request| lambda do |data_point|
merge_request = data_point[:merge_request]
if merge_request.metrics.present? if merge_request.metrics.present?
deployments = Deployment.where(ref: merge_request.target_branch).where("created_at > ?", merge_request.metrics.merged_at) deployments = Deployment.where(ref: merge_request.target_branch).where("created_at > ?", merge_request.metrics.merged_at)
deployment = deployments.order(:created_at).first deployment = deployments.order(:created_at).first
...@@ -40,15 +51,29 @@ class CycleAnalytics ...@@ -40,15 +51,29 @@ class CycleAnalytics
end end
end end
def mr_deployed_to_production_at
lambda do |data_point|
merge_request = data_point[:merge_request]
if merge_request.metrics.present?
deployments = Deployment.joins(:environment).where(ref: merge_request.target_branch, "environments.name" => "production").
where("deployments.created_at > ?", merge_request.metrics.merged_at)
deployment = deployments.order(:created_at).first
deployment.created_at if deployment
end
end
end
def issue_closing_merge_request_opened_time def issue_closing_merge_request_opened_time
lambda do |issue| lambda do |data_point|
issue = data_point[:issue]
merge_requests = issue.closed_by_merge_requests(nil, check_if_open: false) merge_requests = issue.closed_by_merge_requests(nil, check_if_open: false)
merge_requests.map(&:created_at).min if merge_requests.present? merge_requests.map(&:created_at).min if merge_requests.present?
end end
end end
def mr_wip_flag_removed_or_assigned_to_user_other_than_author_time def mr_wip_flag_removed_or_assigned_to_user_other_than_author_time
lambda do |merge_request| lambda do |data_point|
merge_request = data_point[:merge_request]
if merge_request.metrics.present? if merge_request.metrics.present?
merge_request.metrics.wip_flag_first_removed_at.presence || merge_request.metrics.wip_flag_first_removed_at.presence ||
merge_request.metrics.first_assigned_to_user_other_than_author.presence merge_request.metrics.first_assigned_to_user_other_than_author.presence
......
...@@ -33,3 +33,10 @@ ...@@ -33,3 +33,10 @@
= distance_of_time_in_words staging = distance_of_time_in_words staging
- else - else
= "<Not enough data>" = "<Not enough data>"
%li.list-group-item
Production:
- if production = @cycle_analytics.production.presence
= distance_of_time_in_words production
- else
= "<Not enough data>"
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