Commit 5e46f8c1 authored by Mikolaj Wawrzyniak's avatar Mikolaj Wawrzyniak Committed by alinamihaila

Extract PermitDataCategories service

Since resolving service ping permited data categories logic
will be used by two seprate dependencies, to avoid code duplication
new entity should be extracted and reused by those dependencies.
parent cfb23600
# frozen_string_literal: true
module ServicePing
class PermitDataCategoriesService
STANDARD_CATEGORY = 'Standard'
SUBSCRIPTION_CATEGORY = 'Subscription'
OPERATIONAL_CATEGORY = 'Operational'
OPTIONAL_CATEGORY = 'Optional'
CATEGORIES = [
STANDARD_CATEGORY,
SUBSCRIPTION_CATEGORY,
OPERATIONAL_CATEGORY,
OPTIONAL_CATEGORY
].to_set.freeze
def execute
return [] unless product_intelligence_enabled?
CATEGORIES
end
private
def product_intelligence_enabled?
pings_enabled? && !User.single_user&.requires_usage_stats_consent?
end
def pings_enabled?
::Gitlab::CurrentSettings.usage_ping_enabled?
end
end
end
ServicePing::PermitDataCategoriesService.prepend_mod_with('ServicePing::PermitDataCategoriesService')
......@@ -5,11 +5,6 @@ module EE
module BuildPayloadService
extend ::Gitlab::Utils::Override
STANDARD_CATEGORY = 'Standard'
SUBSCRIPTION_CATEGORY = 'Subscription'
OPTIONAL_CATEGORY = 'Optional'
OPERATIONAL_CATEGORY = 'Operational'
override :execute
def execute
return super unless ::License.current.present?
......@@ -35,19 +30,14 @@ module EE
end
def permitted_categories
@permitted_categories ||= collect_permitted_categories
end
def collect_permitted_categories
categories = [STANDARD_CATEGORY, SUBSCRIPTION_CATEGORY]
categories << OPTIONAL_CATEGORY if ::Gitlab::CurrentSettings.usage_ping_enabled?
categories << OPERATIONAL_CATEGORY if ::License.current.usage_ping?
categories
@permitted_categories ||= ::ServicePing::PermitDataCategoriesService.new.execute
end
def metric_category(key, parent_keys)
key_path = parent_keys.dup.append(key).join('.')
metric_definitions[key_path]&.attributes&.fetch(:data_category, OPTIONAL_CATEGORY)
metric_definitions[key_path]
&.attributes
&.fetch(:data_category, ::ServicePing::PermitDataCategoriesService::OPTIONAL_CATEGORY)
end
def metric_definitions
......
# frozen_string_literal: true
module EE
module ServicePing
module PermitDataCategoriesService
extend ::Gitlab::Utils::Override
STANDARD_CATEGORY = ::ServicePing::PermitDataCategoriesService::STANDARD_CATEGORY
SUBSCRIPTION_CATEGORY = ::ServicePing::PermitDataCategoriesService::SUBSCRIPTION_CATEGORY
OPTIONAL_CATEGORY = ::ServicePing::PermitDataCategoriesService::OPTIONAL_CATEGORY
OPERATIONAL_CATEGORY = ::ServicePing::PermitDataCategoriesService::OPERATIONAL_CATEGORY
override :execute
def execute
return super unless ::License.current.present?
return [] unless product_intelligence_enabled?
optional_enabled = ::Gitlab::CurrentSettings.usage_ping_enabled?
operational_enabled = ::License.current.usage_ping?
[STANDARD_CATEGORY, SUBSCRIPTION_CATEGORY].tap do |categories|
categories << OPTIONAL_CATEGORY if optional_enabled
categories << OPERATIONAL_CATEGORY if operational_enabled
end.to_set
end
private
override :pings_enabled?
def pings_enabled?
::License.current&.usage_ping? || ::Gitlab::CurrentSettings.usage_ping_enabled?
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ServicePing::PermitDataCategoriesService do
describe '#execute' do
subject(:permitted_categories) { described_class.new.execute }
context 'with out current license', :without_license do
context 'when usage ping setting is set to true' do
before do
stub_config_setting(usage_ping_enabled: true)
end
it 'returns all categories' do
expect(permitted_categories).to match_array(%w[Standard Subscription Operational Optional])
end
end
context 'when usage ping setting is set to false' do
before do
stub_config_setting(usage_ping_enabled: false)
end
it 'returns no categories' do
expect(permitted_categories).to match_array([])
end
end
end
context 'with current license' do
context 'when usage ping setting is set to true' do
before do
stub_config_setting(usage_ping_enabled: true)
end
context 'and license has usage_ping_required_metrics_enabled set to true' do
before do
# License.current.usage_ping? == true
create_current_license(usage_ping_required_metrics_enabled: true)
end
it 'returns all categories' do
expect(permitted_categories).to match_array(%w[Standard Subscription Operational Optional])
end
context 'when User.single_user&.requires_usage_stats_consent? is required' do
before do
allow(User).to receive(:single_user).and_return(double(:user, requires_usage_stats_consent?: true))
end
it 'returns no categories' do
expect(permitted_categories).to match_array([])
end
end
end
context 'and license has usage_ping_required_metrics_enabled set to false' do
before do
# License.current.usage_ping? == true
create_current_license(usage_ping_required_metrics_enabled: false)
end
it 'returns all categories' do
expect(permitted_categories).to match_array(%w[Standard Subscription Optional])
end
end
end
context 'when usage ping setting is set to false' do
before do
stub_config_setting(usage_ping_enabled: false)
end
context 'and license has usage_ping_required_metrics_enabled set to true' do
before do
# License.current.usage_ping? == true
create_current_license(usage_ping_required_metrics_enabled: true)
end
it 'returns all categories' do
expect(permitted_categories).to match_array(%w[Standard Subscription Operational])
end
end
context 'and license has usage_ping_required_metrics_enabled set to false' do
before do
# License.current.usage_ping? == true
create_current_license(usage_ping_required_metrics_enabled: false)
end
it 'returns all categories' do
expect(permitted_categories).to match_array(%w[])
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ServicePing::PermitDataCategoriesService do
describe '#execute', :without_license do
subject(:permitted_categories) { described_class.new.execute }
context 'when usage ping setting is set to true' do
before do
allow(User).to receive(:single_user).and_return(double(:user, requires_usage_stats_consent?: false))
stub_config_setting(usage_ping_enabled: true)
end
it 'returns all categories' do
expect(permitted_categories).to match_array(%w[Standard Subscription Operational Optional])
end
end
context 'when usage ping setting is set to false' do
before do
allow(User).to receive(:single_user).and_return(double(:user, requires_usage_stats_consent?: false))
stub_config_setting(usage_ping_enabled: false)
end
it 'returns no categories' do
expect(permitted_categories).to match_array([])
end
end
context 'when User.single_user&.requires_usage_stats_consent? is required' do
before do
allow(User).to receive(:single_user).and_return(double(:user, requires_usage_stats_consent?: true))
stub_config_setting(usage_ping_enabled: true)
end
it 'returns no categories' do
expect(permitted_categories).to match_array([])
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