Commit 5032df86 authored by Stan Hu's avatar Stan Hu

Merge branch 'adjust-group-level-analytics-to-accept-multiple-project-ids' into 'master'

Adjust group level analytics to accept multiple projects ids

See merge request gitlab-org/gitlab-ce!30744
parents dfe0a56e 822129b6
...@@ -11,9 +11,7 @@ module CycleAnalytics ...@@ -11,9 +11,7 @@ module CycleAnalytics
end end
def summary def summary
@summary ||= ::Gitlab::CycleAnalytics::GroupStageSummary.new(group, @summary ||= ::Gitlab::CycleAnalytics::GroupStageSummary.new(group, options: options).data
from: options[:from],
current_user: options[:current_user]).data
end end
def permissions(*) def permissions(*)
......
---
title: Adjust group level analytics to accept multiple ids
merge_request: 30744
author:
type: added
...@@ -4,6 +4,7 @@ module Gitlab ...@@ -4,6 +4,7 @@ module Gitlab
module CycleAnalytics module CycleAnalytics
class BaseEventFetcher class BaseEventFetcher
include BaseQuery include BaseQuery
include GroupProjectsProvider
attr_reader :projections, :query, :stage, :order, :options attr_reader :projections, :query, :stage, :order, :options
...@@ -73,18 +74,6 @@ module Gitlab ...@@ -73,18 +74,6 @@ module Gitlab
def serialization_context def serialization_context
{} {}
end 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 end
end end
...@@ -4,6 +4,7 @@ module Gitlab ...@@ -4,6 +4,7 @@ module Gitlab
module CycleAnalytics module CycleAnalytics
class BaseStage class BaseStage
include BaseQuery include BaseQuery
include GroupProjectsProvider
attr_reader :options attr_reader :options
...@@ -77,18 +78,6 @@ module Gitlab ...@@ -77,18 +78,6 @@ module Gitlab
def event_options def event_options
options.merge(start_time_attrs: start_time_attrs, end_time_attrs: end_time_attrs) options.merge(start_time_attrs: start_time_attrs, end_time_attrs: end_time_attrs)
end 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 end
end end
# frozen_string_literal: true
module Gitlab
module CycleAnalytics
module GroupProjectsProvider
def projects
group ? projects_for_group : [project]
end
def group
@group ||= options.fetch(:group, nil)
end
def project
@project ||= options.fetch(:project, nil)
end
private
def projects_for_group
projects = Project.inside_path(group.full_path)
projects = projects.where(id: options[:projects]) if options[:projects]
projects
end
end
end
end
...@@ -3,15 +3,18 @@ ...@@ -3,15 +3,18 @@
module Gitlab module Gitlab
module CycleAnalytics module CycleAnalytics
class GroupStageSummary class GroupStageSummary
def initialize(group, from:, current_user:) attr_reader :group, :from, :current_user, :options
def initialize(group, options:)
@group = group @group = group
@from = from @from = options[:from]
@current_user = current_user @current_user = options[:current_user]
@options = options
end end
def data def data
[serialize(Summary::Group::Issue.new(group: @group, from: @from, current_user: @current_user)), [serialize(Summary::Group::Issue.new(group: group, from: from, current_user: current_user, options: options)),
serialize(Summary::Group::Deploy.new(group: @group, from: @from))] serialize(Summary::Group::Deploy.new(group: group, from: from, options: options))]
end end
private private
......
...@@ -5,9 +5,12 @@ module Gitlab ...@@ -5,9 +5,12 @@ module Gitlab
module Summary module Summary
module Group module Group
class Base class Base
def initialize(group:, from:) attr_reader :group, :from, :options
def initialize(group:, from:, options:)
@group = group @group = group
@from = from @from = from
@options = options
end end
def title def title
......
...@@ -5,22 +5,23 @@ module Gitlab ...@@ -5,22 +5,23 @@ module Gitlab
module Summary module Summary
module Group module Group
class Deploy < Group::Base class Deploy < Group::Base
include GroupProjectsProvider
def title def title
n_('Deploy', 'Deploys', value) n_('Deploy', 'Deploys', value)
end end
def value def value
@value ||= Deployment.joins(:project) @value ||= find_deployments
.where(projects: { id: projects })
.where("deployments.created_at > ?", @from)
.success
.count
end end
private private
def projects def find_deployments
Project.inside_path(@group.full_path).ids deployments = Deployment.joins(:project).merge(Project.inside_path(group.full_path))
deployments = deployments.where(projects: { id: options[:projects] }) if options[:projects]
deployments = deployments.where("deployments.created_at > ?", from)
deployments.success.count
end end
end end
end end
......
...@@ -5,10 +5,13 @@ module Gitlab ...@@ -5,10 +5,13 @@ module Gitlab
module Summary module Summary
module Group module Group
class Issue < Group::Base class Issue < Group::Base
def initialize(group:, from:, current_user:) attr_reader :group, :from, :current_user, :options
def initialize(group:, from:, current_user:, options:)
@group = group @group = group
@from = from @from = from
@current_user = current_user @current_user = current_user
@options = options
end end
def title def title
...@@ -16,7 +19,15 @@ module Gitlab ...@@ -16,7 +19,15 @@ module Gitlab
end end
def value def value
@value ||= IssuesFinder.new(@current_user, group_id: @group.id, include_subgroups: true, created_after: @from).execute.count @value ||= find_issues
end
private
def find_issues
issues = IssuesFinder.new(current_user, group_id: group.id, include_subgroups: true, created_after: from).execute
issues = issues.where(projects: { id: options[:projects] }) if options[:projects]
issues.count
end end
end end
end end
......
...@@ -8,7 +8,7 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do ...@@ -8,7 +8,7 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do
let(:from) { 1.day.ago } let(:from) { 1.day.ago }
let(:user) { create(:user, :admin) } let(:user) { create(:user, :admin) }
subject { described_class.new(group, from: Time.now, current_user: user).data } subject { described_class.new(group, options: { from: Time.now, current_user: user }).data }
describe "#new_issues" do describe "#new_issues" do
context 'with from date' do context 'with from date' do
...@@ -32,6 +32,18 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do ...@@ -32,6 +32,18 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do
expect(subject.first[:value]).to eq(3) expect(subject.first[:value]).to eq(3)
end end
end end
context 'with projects specified in options' do
before do
Timecop.freeze(5.days.from_now) { create(:issue, project: create(:project, namespace: group)) }
end
subject { described_class.new(group, options: { from: Time.now, current_user: user, projects: [project.id, project_2.id] }).data }
it 'finds issues from those projects' do
expect(subject.first[:value]).to eq(2)
end
end
end end
context 'with other projects' do context 'with other projects' do
...@@ -71,6 +83,20 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do ...@@ -71,6 +83,20 @@ describe Gitlab::CycleAnalytics::GroupStageSummary do
expect(subject.second[:value]).to eq(3) expect(subject.second[:value]).to eq(3)
end end
end end
context 'with projects specified in options' do
before do
Timecop.freeze(5.days.from_now) do
create(:deployment, :success, project: create(:project, :repository, namespace: group, name: 'not_applicable'))
end
end
subject { described_class.new(group, options: { from: Time.now, current_user: user, projects: [project.id, project_2.id] }).data }
it 'shows deploys from those projects' do
expect(subject.second[:value]).to eq(2)
end
end
end end
context 'with other projects' do context 'with other projects' do
......
...@@ -71,6 +71,29 @@ describe Gitlab::CycleAnalytics::IssueStage do ...@@ -71,6 +71,29 @@ describe Gitlab::CycleAnalytics::IssueStage do
end end
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
subject { stage.events }
it 'exposes merge requests that close issues' do
expect(subject.count).to eq(1)
expect(subject.map { |event| event[:title] }).to contain_exactly(issue_2_1.title)
end
end
end
context 'when subgroup is given' do context 'when subgroup is given' do
let(:subgroup) { create(:group, parent: group) } let(:subgroup) { create(:group, parent: group) }
let(:project_4) { create(:project, group: subgroup) } let(:project_4) { create(:project, group: subgroup) }
......
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