BigW Consortium Gitlab

Commit df6c9c33 by Timothy Andrew

Scope Cycle Analytics queries to a project

parent a81de9ab
class Projects::CycleAnalyticsController < Projects::ApplicationController
def show
@cycle_analytics = CycleAnalytics.new
@cycle_analytics = CycleAnalytics.new(@project)
end
end
class CycleAnalytics
def initialize(project)
@project = project
end
def issue
calculate_metric(Queries::issues,
calculate_metric(Queries::issues(@project),
-> (data_point) { data_point[:issue].created_at },
[Queries::issue_first_associated_with_milestone_at, Queries::issue_first_added_to_list_label_at])
end
def plan
calculate_metric(Queries::issues,
calculate_metric(Queries::issues(@project),
[Queries::issue_first_associated_with_milestone_at, Queries::issue_first_added_to_list_label_at],
Queries::issue_closing_merge_request_opened_at)
end
def code
calculate_metric(Queries::merge_requests_closing_issues,
calculate_metric(Queries::merge_requests_closing_issues(@project),
-> (data_point) { data_point[:merge_request].created_at },
[Queries::merge_request_first_assigned_to_user_other_than_author_at, Queries::merge_request_wip_flag_first_removed_at])
end
def test
calculate_metric(Queries::merge_requests_closing_issues,
calculate_metric(Queries::merge_requests_closing_issues(@project),
Queries::merge_request_build_started_at,
Queries::merge_request_build_finished_at)
end
def review
calculate_metric(Queries::merge_requests_closing_issues,
calculate_metric(Queries::merge_requests_closing_issues(@project),
[Queries::merge_request_first_assigned_to_user_other_than_author_at, Queries::merge_request_wip_flag_first_removed_at],
[Queries::merge_request_first_closed_at, Queries::merge_request_merged_at])
end
def staging
calculate_metric(Queries::merge_requests_closing_issues,
calculate_metric(Queries::merge_requests_closing_issues(@project),
Queries::merge_request_merged_at,
Queries::merge_request_deployed_to_any_environment_at)
end
def production
calculate_metric(Queries::merge_requests_closing_issues,
calculate_metric(Queries::merge_requests_closing_issues(@project),
-> (data_point) { data_point[:issue].created_at },
Queries::merge_request_deployed_to_production_at)
end
......
class CycleAnalytics
module Queries
class << self
def issues
Issue.all.to_a.map { |issue| { issue: issue } }
def issues(project)
project.issues.map { |issue| { issue: issue } }
end
def merge_requests_closing_issues
issues.map do |data_point|
def merge_requests_closing_issues(project)
issues(project).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
......
......@@ -2,7 +2,7 @@ require 'spec_helper'
describe 'CycleAnalytics#issue', models: true do
let(:project) { create(:project) }
subject { CycleAnalytics.new }
subject { CycleAnalytics.new(project) }
context "when calculating the median of times between:
start: issue created_at
......@@ -26,6 +26,16 @@ describe 'CycleAnalytics#issue', models: true do
median_start_time, median_end_time = start_and_end_times[2]
expect(subject.issue).to eq(median_end_time - median_start_time)
end
it "does not include issues from other projects" do
5.times do
milestone = create(:milestone, project: project)
issue = create(:issue)
issue.update(milestone: milestone)
end
expect(subject.issue).to be_nil
end
end
context "when a label is added to the issue" do
......@@ -47,14 +57,9 @@ describe 'CycleAnalytics#issue', models: true do
it "does not make a calculation for regular labels" do
5.times do
start_time = Time.now
end_time = rand(1..10).days.from_now
regular_label = create(:label)
issue = Timecop.freeze(start_time) { create(:issue, project: project) }
Timecop.freeze(end_time) { issue.update(label_ids: [regular_label.id]) }
[start_time, end_time]
issue = create(:issue, project: project)
issue.update(label_ids: [regular_label.id])
end
expect(subject.issue).to be_nil
......
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