Commit df6c9c33 authored by Timothy Andrew's avatar Timothy Andrew

Scope Cycle Analytics queries to a project

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