diff options
11 files changed, 104 insertions, 38 deletions
diff --git a/app/models/cycle_analytics/group_level.rb b/app/models/cycle_analytics/group_level.rb index 58bd2eb0d9a..c5aa1d9d964 100644 --- a/app/models/cycle_analytics/group_level.rb +++ b/app/models/cycle_analytics/group_level.rb @@ -12,7 +12,8 @@ module CycleAnalytics def summary @summary ||= ::Gitlab::CycleAnalytics::GroupStageSummary.new(options[:group], from: options[:from], - current_user: options[:current_user]).data + current_user: options[:current_user], + options: options).data end def permissions(user: nil) diff --git a/changelogs/unreleased/adjust-group-level-analytics-to-accept-multiple-ids.yml b/changelogs/unreleased/adjust-group-level-analytics-to-accept-multiple-ids.yml new file mode 100644 index 00000000000..a90e73a1410 --- /dev/null +++ b/changelogs/unreleased/adjust-group-level-analytics-to-accept-multiple-ids.yml @@ -0,0 +1,5 @@ +--- +title: Adjust group level analytics to accept multiple ids +merge_request: 30468 +author: +type: added diff --git a/lib/gitlab/cycle_analytics/base_data_extraction.rb b/lib/gitlab/cycle_analytics/base_data_extraction.rb new file mode 100644 index 00000000000..c72323461b3 --- /dev/null +++ b/lib/gitlab/cycle_analytics/base_data_extraction.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Gitlab + module CycleAnalytics + module BaseDataExtraction + private + + def projects + group ? extract_projects(options) : [project] + end + + def group + @group ||= options.fetch(:group, nil) + end + + def extract_projects(options) + projects = Project.inside_path(group.full_path) + projects = projects.where(id: options[:projects]) if options[:projects] + projects + end + end + end +end diff --git a/lib/gitlab/cycle_analytics/base_event_fetcher.rb b/lib/gitlab/cycle_analytics/base_event_fetcher.rb index 96aa799e864..0f5186e06e7 100644 --- a/lib/gitlab/cycle_analytics/base_event_fetcher.rb +++ b/lib/gitlab/cycle_analytics/base_event_fetcher.rb @@ -4,6 +4,7 @@ module Gitlab module CycleAnalytics class BaseEventFetcher include BaseQuery + include BaseDataExtraction attr_reader :projections, :query, :stage, :order, :options @@ -73,18 +74,6 @@ module Gitlab def serialization_context {} end - - def projects - group ? Project.inside_path(group.full_path) : [project] - end - - def group - @group ||= options.fetch(:group, nil) - end - - def project - @project ||= options.fetch(:project, nil) - end end end end diff --git a/lib/gitlab/cycle_analytics/base_stage.rb b/lib/gitlab/cycle_analytics/base_stage.rb index 678a891e941..e641bed704b 100644 --- a/lib/gitlab/cycle_analytics/base_stage.rb +++ b/lib/gitlab/cycle_analytics/base_stage.rb @@ -4,6 +4,7 @@ module Gitlab module CycleAnalytics class BaseStage include BaseQuery + include BaseDataExtraction attr_reader :options @@ -77,18 +78,6 @@ module Gitlab def event_options options.merge(start_time_attrs: start_time_attrs, end_time_attrs: end_time_attrs) end - - def projects - group ? Project.inside_path(group.full_path) : [project] - end - - def group - @group ||= options.fetch(:group, nil) - end - - def project - @project ||= options.fetch(:project, nil) - end end end end diff --git a/lib/gitlab/cycle_analytics/group_stage_summary.rb b/lib/gitlab/cycle_analytics/group_stage_summary.rb index 7b5c74e1a1b..f867d511f65 100644 --- a/lib/gitlab/cycle_analytics/group_stage_summary.rb +++ b/lib/gitlab/cycle_analytics/group_stage_summary.rb @@ -3,15 +3,16 @@ module Gitlab module CycleAnalytics class GroupStageSummary - def initialize(group, from:, current_user:) + def initialize(group, from:, current_user:, options:) @group = group @from = from @current_user = current_user + @options = options end def data - [serialize(Summary::Group::Issue.new(group: @group, from: @from, current_user: @current_user)), - serialize(Summary::Group::Deploy.new(group: @group, from: @from))] + [serialize(Summary::Group::Issue.new(group: @group, from: @from, current_user: @current_user, options: @options)), + serialize(Summary::Group::Deploy.new(group: @group, from: @from, options: @options))] end private diff --git a/lib/gitlab/cycle_analytics/summary/group/base.rb b/lib/gitlab/cycle_analytics/summary/group/base.rb index 7f18b61d309..d147879ace4 100644 --- a/lib/gitlab/cycle_analytics/summary/group/base.rb +++ b/lib/gitlab/cycle_analytics/summary/group/base.rb @@ -5,9 +5,10 @@ module Gitlab module Summary module Group class Base - def initialize(group:, from:) + def initialize(group:, from:, options:) @group = group @from = from + @options = options end def title diff --git a/lib/gitlab/cycle_analytics/summary/group/deploy.rb b/lib/gitlab/cycle_analytics/summary/group/deploy.rb index d8fcd8f2ce4..ec0b23e770d 100644 --- a/lib/gitlab/cycle_analytics/summary/group/deploy.rb +++ b/lib/gitlab/cycle_analytics/summary/group/deploy.rb @@ -10,15 +10,19 @@ module Gitlab end def value - @value ||= Deployment.joins(:project) - .where(projects: { id: projects }) - .where("deployments.created_at > ?", @from) - .success - .count + @value ||= find_deployments end private + def find_deployments + deployments = Deployment.joins(:project) + .where(projects: { id: projects }) + .where("deployments.created_at > ?", @from) + deployments = deployments.where(projects: { id: @options[:projects] }) if @options[:projects] + deployments.success.count + end + def projects Project.inside_path(@group.full_path).ids end diff --git a/lib/gitlab/cycle_analytics/summary/group/issue.rb b/lib/gitlab/cycle_analytics/summary/group/issue.rb index a5188056cb7..6d9a31ad43e 100644 --- a/lib/gitlab/cycle_analytics/summary/group/issue.rb +++ b/lib/gitlab/cycle_analytics/summary/group/issue.rb @@ -5,10 +5,11 @@ module Gitlab module Summary module Group class Issue < Group::Base - def initialize(group:, from:, current_user:) + def initialize(group:, from:, current_user:, options:) @group = group @from = from @current_user = current_user + @options = options end def title @@ -16,7 +17,15 @@ module Gitlab end def value - @value ||= IssuesFinder.new(@current_user, group_id: @group.id, include_subgroups: true).execute.created_after(@from).count + @value ||= find_issues + end + + private + + def find_issues + issues = IssuesFinder.new(@current_user, group_id: @group.id, include_subgroups: true).execute + issues = issues.where(projects: { id: @options[:projects] }) if @options[:projects] + issues.created_after(@from).count end end end diff --git a/spec/lib/gitlab/cycle_analytics/group_stage_summary_spec.rb b/spec/lib/gitlab/cycle_analytics/group_stage_summary_spec.rb index 8e552b23283..d3cf3f93693 100644 --- a/spec/lib/gitlab/cycle_analytics/group_stage_summary_spec.rb +++ b/spec/lib/gitlab/cycle_analytics/group_stage_summary_spec.rb @@ -7,7 +7,7 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do let(:project_2) { create(:project, :repository, namespace: group) } let(:from) { 1.day.ago } let(:user) { create(:user, :admin) } - subject { described_class.new(group, from: Time.now, current_user: user).data } + subject { described_class.new(group, from: Time.now, current_user: user, options: {}).data } describe "#new_issues" do it "finds the number of issues created after the 'from date'" do @@ -20,7 +20,7 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do end it "doesn't find issues from other projects" do - Timecop.freeze(5.days.from_now) { create(:issue, project: create(:project, namespace: create(:group))) } + Timecop.freeze(5.days.from_now) { create(:issue, project: create(:project)) } Timecop.freeze(5.days.from_now) { create(:issue, project: project) } Timecop.freeze(5.days.from_now) { create(:issue, project: project_2) } @@ -34,6 +34,16 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do expect(subject.first[:value]).to eq(3) end + + it "finds issues from projects specified in options" do + Timecop.freeze(5.days.from_now) { create(:issue, project: create(:project, namespace: group)) } + Timecop.freeze(5.days.from_now) { create(:issue, project: project) } + Timecop.freeze(5.days.from_now) { create(:issue, project: project_2) } + + subject = described_class.new(group, from: Time.now, current_user: user, options: { projects: [project.id, project_2.id] }).data + + expect(subject.first[:value]).to eq(2) + end end describe "#deploys" do @@ -61,5 +71,16 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do expect(subject.second[:value]).to eq(1) end + + it "shows deploys from projects specified in options" do + Timecop.freeze(5.days.from_now) do + create(:deployment, :success, project: project) + create(:deployment, :success, project: project_2) + create(:deployment, :success, project: create(:project, :repository, namespace: group, name: 'not_applicable')) + end + subject = described_class.new(group, from: Time.now, current_user: user, options: { projects: [project.id, project_2.id] }).data + + expect(subject.second[:value]).to eq(2) + end end end diff --git a/spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb b/spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb index 64ac9df52b2..b92e27c4169 100644 --- a/spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb +++ b/spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb @@ -71,6 +71,29 @@ describe Gitlab::CycleAnalytics::IssueStage do end end + context 'when only part of projects is chosen' do + let(:stage) { described_class.new(options: { from: 2.days.ago, current_user: user, group: group, projects: [project_2.id] }) } + + describe '#group_median' do + around do |example| + Timecop.freeze { example.run } + end + + it 'counts median from issues with metrics' do + expect(stage.group_median).to eq(ISSUES_MEDIAN) + end + end + + describe '#events' do + it 'exposes merge requests that close issues' do + result = stage.events + + expect(result.count).to eq(1) + expect(result.map { |event| event[:title] }).to contain_exactly(issue_2_1.title) + end + end + end + context 'when subgroup is given' do let(:subgroup) { create(:group, parent: group) } let(:project_4) { create(:project, group: subgroup) } |