Commit 710b3c9d authored by Avielle Wolfe's avatar Avielle Wolfe Committed by Mark Chao

Refactor minutes UI methods

Some of the logic in `Ci::Minutes::Quota` is only used to determine
whether or not to show CI minutes information in the UI. This commit
refactors and renames that logic to make it more clear that it is being
used only for UI purposes.
parent f08c1f7a
...@@ -16,9 +16,7 @@ module Ci ...@@ -16,9 +16,7 @@ module Ci
end end
def enabled? def enabled?
strong_memoize(:enabled) do namespace_root? && total_minutes.nonzero?
namespace_eligible? && total_minutes.nonzero?
end
end end
# Status of the monthly allowance being used. # Status of the monthly allowance being used.
...@@ -50,28 +48,34 @@ module Ci ...@@ -50,28 +48,34 @@ module Ci
enabled? && total_minutes_used >= total_minutes enabled? && total_minutes_used >= total_minutes
end end
def total_minutes def percent_total_minutes_remaining
@total_minutes ||= monthly_minutes + purchased_minutes return 0 if total_minutes == 0
100 * total_minutes_remaining.to_i / total_minutes
end end
def total_minutes_used def current_balance
@total_minutes_used ||= namespace.shared_runners_seconds.to_i / 60 total_minutes.to_i - total_minutes_used
end end
def percent_total_minutes_remaining def display_shared_runners_data?
return 0 if total_minutes == 0 namespace_root? && any_project_enabled?
end
100 * total_minutes_remaining.to_i / total_minutes def display_minutes_available_data?
display_shared_runners_data? && total_minutes.nonzero?
end end
def namespace_eligible? def total_minutes
strong_memoize(:namespace_eligible) do strong_memoize(:total_minutes) do
namespace.root? monthly_minutes + purchased_minutes
end end
end end
def current_balance def total_minutes_used
total_minutes.to_i - total_minutes_used strong_memoize(:total_minutes_used) do
namespace.shared_runners_seconds.to_i / 60
end
end end
def any_project_enabled? def any_project_enabled?
...@@ -82,13 +86,15 @@ module Ci ...@@ -82,13 +86,15 @@ module Ci
private private
attr_reader :namespace
def minutes_limit def minutes_limit
return monthly_minutes if enabled? && any_project_enabled? return _('Not supported') unless display_shared_runners_data?
if namespace_eligible? && any_project_enabled? if display_minutes_available_data?
_('Unlimited') monthly_minutes
else else
_('Not supported') _('Unlimited')
end end
end end
...@@ -137,14 +143,22 @@ module Ci ...@@ -137,14 +143,22 @@ module Ci
end end
def monthly_minutes def monthly_minutes
@monthly_minutes ||= (namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i strong_memoize(:monthly_minutes) do
(namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i
end
end end
def purchased_minutes def purchased_minutes
@purchased_minutes ||= namespace.extra_shared_runners_minutes_limit.to_i strong_memoize(:purchased_minutes) do
namespace.extra_shared_runners_minutes_limit.to_i
end
end end
attr_reader :namespace def namespace_root?
strong_memoize(:namespace_root) do
namespace.root?
end
end
end end
end end
end end
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
- return unless minutes_quota.enabled? - return unless minutes_quota.enabled?
- if minutes_quota.namespace_eligible? && minutes_quota.any_project_enabled? - if minutes_quota.display_shared_runners_data?
%li %li
%span.light= _('Additional minutes:') %span.light= _('Additional minutes:')
%strong %strong
......
- namespace = local_assigns.fetch(:namespace) - namespace = local_assigns.fetch(:namespace)
- minutes_quota = namespace.ci_minutes_quota - minutes_quota = namespace.ci_minutes_quota
- if minutes_quota.namespace_eligible? && minutes_quota.any_project_enabled? - if minutes_quota.display_shared_runners_data?
%li %li
%span.light= _('Pipeline minutes quota:') %span.light= _('Pipeline minutes quota:')
%strong %strong
......
- return unless Gitlab.com? - return unless Gitlab.com?
- minutes_quota = namespace.ci_minutes_quota - minutes_quota = namespace.ci_minutes_quota
- return unless minutes_quota.enabled? && minutes_quota.purchased_minutes_report.limit > 0 && minutes_quota.any_project_enabled? - return unless minutes_quota.display_minutes_available_data? && minutes_quota.purchased_minutes_report.limit > 0
.row .row
.col-sm-6 .col-sm-6
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
= link_to sprite_icon('question-o'), help_page_path('user/admin_area/settings/continuous_integration', anchor: 'shared-runners-pipeline-minutes-quota'), target: '_blank', 'aria-label': _('Shared runners help link') = link_to sprite_icon('question-o'), help_page_path('user/admin_area/settings/continuous_integration', anchor: 'shared-runners-pipeline-minutes-quota'), target: '_blank', 'aria-label': _('Shared runners help link')
.col-sm-6.right .col-sm-6.right
- if minutes_quota.enabled? && minutes_quota.any_project_enabled? - if minutes_quota.display_minutes_available_data?
#{minutes_quota.monthly_percent_used}% used #{minutes_quota.monthly_percent_used}% used
- elsif !minutes_quota.any_project_enabled? - elsif !minutes_quota.any_project_enabled?
0% used 0% used
......
...@@ -5,7 +5,7 @@ RSpec.describe Ci::Minutes::Quota do ...@@ -5,7 +5,7 @@ RSpec.describe Ci::Minutes::Quota do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
let_it_be_with_reload(:namespace) do let_it_be_with_reload(:namespace) do
create(:namespace, namespace_statistics: create(:namespace_statistics)) create(:group, namespace_statistics: create(:namespace_statistics))
end end
let(:quota) { described_class.new(namespace) } let(:quota) { described_class.new(namespace) }
...@@ -67,7 +67,7 @@ RSpec.describe Ci::Minutes::Quota do ...@@ -67,7 +67,7 @@ RSpec.describe Ci::Minutes::Quota do
context 'when the quota is not enabled' do context 'when the quota is not enabled' do
before do before do
allow(quota).to receive(:enabled?).and_return(false) allow(quota).to receive(:enabled?).and_return(false)
allow(quota).to receive(:namespace_eligible?).and_return(namespace_eligible) allow(namespace).to receive(:root?).and_return(namespace_eligible)
allow(namespace).to receive(:any_project_with_shared_runners_enabled?).and_return(true) allow(namespace).to receive(:any_project_with_shared_runners_enabled?).and_return(true)
end end
...@@ -394,36 +394,6 @@ RSpec.describe Ci::Minutes::Quota do ...@@ -394,36 +394,6 @@ RSpec.describe Ci::Minutes::Quota do
end end
end end
describe '#namespace_eligible?' do
subject { quota.namespace_eligible? }
context 'when namespace is a subgroup' do
it 'is false' do
allow(namespace).to receive(:root?).and_return(false)
expect(subject).to be_falsey
end
end
context 'when namespace is root' do
it 'is true' do
expect(subject).to be_truthy
end
end
it 'does not trigger additional queries when called multiple times' do
# memoizes the result
quota.namespace_eligible?
# count
actual = ActiveRecord::QueryRecorder.new do
quota.namespace_eligible?
end
expect(actual.count).to eq(0)
end
end
describe '#any_project_enabled?' do describe '#any_project_enabled?' do
let_it_be(:project) { create(:project, namespace: namespace) } let_it_be(:project) { create(:project, namespace: namespace) }
...@@ -459,4 +429,66 @@ RSpec.describe Ci::Minutes::Quota do ...@@ -459,4 +429,66 @@ RSpec.describe Ci::Minutes::Quota do
expect(actual.count).to eq(0) expect(actual.count).to eq(0)
end end
end end
describe '#display_shared_runners_data?' do
let_it_be(:project) { create(:project, namespace: namespace, shared_runners_enabled: true) }
subject { quota.display_shared_runners_data? }
context 'when the namespace is root and it has a project with shared runners enabled' do
it { is_expected.to be_truthy }
end
context 'when the namespace is not root' do
let(:namespace) { create(:group, :nested) }
it { is_expected.to be_falsey }
end
context 'when the namespaces has no project with shared runners enabled' do
before do
project.update!(shared_runners_enabled: false)
end
it { is_expected.to be_falsey }
end
end
describe '#display_minutes_available_data?' do
let_it_be(:project) { create(:project, namespace: namespace, shared_runners_enabled: true) }
subject { quota.display_minutes_available_data? }
context 'when the namespace is root and it has a project with shared runners enabled' do
context 'when there is a minutes limit' do
before do
namespace.update!(shared_runners_minutes_limit: 200)
end
it { is_expected.to be_truthy }
end
context 'when there is no minutes limit' do
before do
namespace.update!(shared_runners_minutes_limit: 0)
end
it { is_expected.to be_falsey }
end
end
context 'when the namespace is not root' do
let(:namespace) { create(:group, :nested) }
it { is_expected.to be_falsey }
end
context 'when the namespaces has no project with shared runners enabled' do
before do
project.update!(shared_runners_enabled: false)
end
it { is_expected.to be_falsey }
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