Commit e56f0120 authored by Aakriti Gupta's avatar Aakriti Gupta Committed by Dmytro Zaporozhets

Re-factor VSA group summary and move to ee/

VSA at the group level is an EE feature,
so all it's code should live under ee/
parent e5b265d8
# frozen_string_literal: true
module CycleAnalytics
class GroupLevel
include LevelBase
attr_reader :options, :group
def initialize(group:, options:)
@group = group
@options = options.merge(group: group)
end
def summary
@summary ||= ::Gitlab::CycleAnalytics::GroupStageSummary.new(group, options: options).data
end
def permissions(*)
STAGES.each_with_object({}) do |stage, obj|
obj[stage] = true
end
end
def stats
@stats ||= STAGES.map do |stage_name|
self[stage_name].as_json(serializer: GroupAnalyticsStageSerializer)
end
end
end
end
...@@ -13,7 +13,7 @@ module Analytics ...@@ -13,7 +13,7 @@ module Analytics
def show def show
return render_403 unless can?(current_user, :read_group_cycle_analytics, @group) return render_403 unless can?(current_user, :read_group_cycle_analytics, @group)
group_level = ::CycleAnalytics::GroupLevel.new(group: @group, options: options(group_params)) group_level = GroupLevel.new(group: @group, options: options(group_params))
render json: group_level.summary render json: group_level.summary
end end
......
# frozen_string_literal: true
module Analytics
module CycleAnalytics
class GroupLevel
include ::CycleAnalytics::LevelBase
attr_reader :options, :group
def initialize(group:, options:)
@group = group
@options = options.merge(group: group)
end
def summary
@summary ||=
Gitlab::Analytics::CycleAnalytics::GroupStageSummary
.new(group, options: options)
.data
end
def permissions(*)
STAGES.each_with_object({}) do |stage, obj|
obj[stage] = true
end
end
def stats
@stats ||= STAGES.map do |stage_name|
self[stage_name].as_json(serializer: GroupAnalyticsStageSerializer)
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
class GroupStageSummary
attr_reader :group, :current_user, :options
def initialize(group, options:)
@group = group
@current_user = options[:current_user]
@options = options
end
def data
[issue_stats,
deploy_stats,
deployment_frequency_stats]
end
private
def issue_stats
serialize(
Summary::Group::Issue.new(
group: group, current_user: current_user, options: options)
)
end
def deployments_summary
@deployments_summary ||=
Summary::Group::Deploy.new(group: group, options: options)
end
def deploy_stats
serialize deployments_summary
end
def deployment_frequency_stats
serialize(
Summary::Group::DeploymentFrequency.new(
deployments: deployments_summary.value,
group: group,
options: options),
with_unit: true
)
end
def serialize(summary_object, with_unit: false)
AnalyticsSummarySerializer.new.represent(
summary_object, with_unit: with_unit)
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
module Summary
module Group
class Base
attr_reader :group, :options
def initialize(group:, options:)
@group = group
@options = options
end
def title
raise NotImplementedError.new("Expected #{self.name} to implement title")
end
def value
raise NotImplementedError.new("Expected #{self.name} to implement value")
end
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
module Summary
module Group
class Deploy < Group::Base
include Gitlab::CycleAnalytics::GroupProjectsProvider
def title
n_('Deploy', 'Deploys', value)
end
def value
@value ||= find_deployments
end
private
# rubocop: disable CodeReuse/ActiveRecord
def find_deployments
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 > ?", options[:from])
deployments = deployments.where("deployments.created_at < ?", options[:to]) if options[:to]
deployments.success.count
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
module Summary
module Group
class DeploymentFrequency < Group::Base
include Gitlab::CycleAnalytics::GroupProjectsProvider
include Gitlab::CycleAnalytics::SummaryHelper
def initialize(deployments:, group:, options:)
@deployments = deployments
super(group: group, options: options)
end
def title
_('Deployment Frequency')
end
def value
@value ||=
frequency(@deployments, options[:from], options[:to] || Time.now)
end
def unit
_('per day')
end
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Analytics
module CycleAnalytics
module Summary
module Group
class Issue < Group::Base
attr_reader :group, :current_user, :options
def initialize(group:, current_user:, options:)
@group = group
@current_user = current_user
@options = options
end
def title
n_('New Issue', 'New Issues', value)
end
def value
@value ||= find_issues
end
private
# rubocop: disable CodeReuse/ActiveRecord
def find_issues
issues = IssuesFinder.new(current_user, finder_params).execute
issues = issues.where(projects: { id: options[:projects] }) if options[:projects]
issues.count
end
# rubocop: enable CodeReuse/ActiveRecord
def finder_params
{
group_id: group.id,
include_subgroups: true,
created_after: options[:from],
created_before: options[:to]
}.compact
end
end
end
end
end
end
end
...@@ -26,7 +26,7 @@ describe Analytics::CycleAnalytics::SummaryController do ...@@ -26,7 +26,7 @@ describe Analytics::CycleAnalytics::SummaryController do
end end
it 'omits `projects` parameter if it is not given' do it 'omits `projects` parameter if it is not given' do
expect(CycleAnalytics::GroupLevel).to receive(:new).with(group: group, options: hash_excluding(:projects)).and_call_original expect(Analytics::CycleAnalytics::GroupLevel).to receive(:new).with(group: group, options: hash_excluding(:projects)).and_call_original
subject subject
...@@ -36,7 +36,7 @@ describe Analytics::CycleAnalytics::SummaryController do ...@@ -36,7 +36,7 @@ describe Analytics::CycleAnalytics::SummaryController do
it 'contains `projects` parameter' do it 'contains `projects` parameter' do
params[:project_ids] = [-1] params[:project_ids] = [-1]
expect(CycleAnalytics::GroupLevel).to receive(:new).with(group: group, options: hash_including(projects: ['-1'])).and_call_original expect(Analytics::CycleAnalytics::GroupLevel).to receive(:new).with(group: group, options: hash_including(projects: ['-1'])).and_call_original
subject subject
......
# frozen_string_literal: true # frozen_string_literal: true
require 'spec_helper' require 'spec_helper'
describe Gitlab::CycleAnalytics::GroupStageSummary do describe Gitlab::Analytics::CycleAnalytics::GroupStageSummary do
let(:group) { create(:group) } let(:group) { create(:group) }
let(:project) { create(:project, :repository, namespace: group) } let(:project) { create(:project, :repository, namespace: group) }
let(:project_2) { create(:project, :repository, namespace: group) } let(:project_2) { create(:project, :repository, namespace: group) }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe CycleAnalytics::GroupLevel do describe Analytics::CycleAnalytics::GroupLevel do
let_it_be(:group) { create(:group)} let_it_be(:group) { create(:group)}
let_it_be(:project) { create(:project, :repository, namespace: group) } let_it_be(:project) { create(:project, :repository, namespace: group) }
let_it_be(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
......
# frozen_string_literal: true
module Gitlab
module CycleAnalytics
class GroupStageSummary
attr_reader :group, :current_user, :options
def initialize(group, options:)
@group = group
@current_user = options[:current_user]
@options = options
end
def data
[issue_stats,
deploy_stats,
deployment_frequency_stats]
end
private
def issue_stats
serialize(
Summary::Group::Issue.new(
group: group, current_user: current_user, options: options)
)
end
def deployments_summary
@deployments_summary ||=
Summary::Group::Deploy.new(group: group, options: options)
end
def deploy_stats
serialize deployments_summary
end
def deployment_frequency_stats
serialize(
Summary::Group::DeploymentFrequency.new(
deployments: deployments_summary.value,
group: group,
options: options),
with_unit: true
)
end
def serialize(summary_object, with_unit: false)
AnalyticsSummarySerializer.new.represent(
summary_object, with_unit: with_unit)
end
end
end
end
# frozen_string_literal: true
module Gitlab
module CycleAnalytics
module Summary
module Group
class Base
attr_reader :group, :options
def initialize(group:, options:)
@group = group
@options = options
end
def title
raise NotImplementedError.new("Expected #{self.name} to implement title")
end
def value
raise NotImplementedError.new("Expected #{self.name} to implement value")
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module CycleAnalytics
module Summary
module Group
class Deploy < Group::Base
include GroupProjectsProvider
def title
n_('Deploy', 'Deploys', value)
end
def value
@value ||= find_deployments
end
private
def find_deployments
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 > ?", options[:from])
deployments = deployments.where("deployments.created_at < ?", options[:to]) if options[:to]
deployments.success.count
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module CycleAnalytics
module Summary
module Group
class DeploymentFrequency < Group::Base
include GroupProjectsProvider
include SummaryHelper
def initialize(deployments:, group:, options:)
@deployments = deployments
super(group: group, options: options)
end
def title
_('Deployment Frequency')
end
def value
@value ||=
frequency(@deployments, options[:from], options[:to] || Time.now)
end
def unit
_('per day')
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module CycleAnalytics
module Summary
module Group
class Issue < Group::Base
attr_reader :group, :current_user, :options
def initialize(group:, current_user:, options:)
@group = group
@current_user = current_user
@options = options
end
def title
n_('New Issue', 'New Issues', value)
end
def value
@value ||= find_issues
end
private
def find_issues
issues = IssuesFinder.new(current_user, finder_params).execute
issues = issues.where(projects: { id: options[:projects] }) if options[:projects]
issues.count
end
def finder_params
{
group_id: group.id,
include_subgroups: true,
created_after: options[:from],
created_before: options[:to]
}.compact
end
end
end
end
end
end
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