Commit f08c1f7a authored by Avielle Wolfe's avatar Avielle Wolfe Committed by Mark Chao

Merge `#actual_minutes_used_up?` to `#enabled?`

Changes were recently made to `Ci::Minutes::Quota` that added methods
that avoided checking `Project#shared_runners_enabled` pending the work
done in this MR. They can now be merged into the existing methods, since
`Quota#enabled?` is no longer checking against `#shared_runners_enabled`
parent 41ea3080
......@@ -16,8 +16,10 @@ module Ci
end
def enabled?
strong_memoize(:enabled) do
namespace_eligible? && total_minutes.nonzero?
end
end
# Status of the monthly allowance being used.
def monthly_minutes_report
......@@ -25,7 +27,7 @@ module Ci
end
def monthly_percent_used
return 0 unless enabled? && any_project_enabled?
return 0 unless enabled?
return 0 if monthly_minutes == 0
100 * monthly_minutes_used.to_i / monthly_minutes
......@@ -48,13 +50,6 @@ module Ci
enabled? && total_minutes_used >= total_minutes
end
# TODO: merge this with minutes_used_up? in
# https://gitlab.com/gitlab-org/gitlab/-/issues/332933.
# This method is agnostic from Project#shared_runners_enabled
def actual_minutes_used_up?
limit_enabled? && total_minutes_used >= total_minutes
end
def total_minutes
@total_minutes ||= monthly_minutes + purchased_minutes
end
......@@ -79,21 +74,13 @@ module Ci
total_minutes.to_i - total_minutes_used
end
private
def any_project_enabled?
strong_memoize(:any_project_enabled) do
namespace.any_project_with_shared_runners_enabled?
end
end
# TODO: rename to `enabled?`
# https://gitlab.com/gitlab-org/gitlab/-/issues/332933
def limit_enabled?
strong_memoize(:limit_enabled) do
namespace.root? && !!total_minutes.nonzero?
end
end
private
def minutes_limit
return monthly_minutes if enabled? && any_project_enabled?
......
......@@ -28,7 +28,7 @@ module EE
def minutes_exceeded?(project)
::Ci::Runner.any_shared_runners_with_enabled_cost_factor?(project) &&
project.ci_minutes_quota.actual_minutes_used_up?
project.ci_minutes_quota.minutes_used_up?
end
end
end
......
......@@ -27,7 +27,7 @@ module Ci
def update_pending_builds!
return unless ::Feature.enabled?(:ci_pending_builds_maintain_ci_minutes_data, @root_namespace, type: :development, default_enabled: :yaml)
minutes_exceeded = @root_namespace.ci_minutes_quota.actual_minutes_used_up?
minutes_exceeded = @root_namespace.ci_minutes_quota.minutes_used_up?
all_namespaces = @root_namespace.self_and_descendant_ids
::Ci::PendingBuild.where(namespace: all_namespaces).update_all(minutes_exceeded: minutes_exceeded)
......
......@@ -5,7 +5,7 @@
- return unless minutes_quota.enabled?
- if minutes_quota.namespace_eligible?
- if minutes_quota.namespace_eligible? && minutes_quota.any_project_enabled?
%li
%span.light= _('Additional minutes:')
%strong
......
- namespace = local_assigns.fetch(:namespace)
- minutes_quota = namespace.ci_minutes_quota
- if namespace.any_project_with_shared_runners_enabled?
- if minutes_quota.namespace_eligible? && minutes_quota.any_project_enabled?
%li
%span.light= _('Pipeline minutes quota:')
%strong
......
- return unless Gitlab.com?
- minutes_quota = namespace.ci_minutes_quota
- return unless minutes_quota.enabled? && minutes_quota.purchased_minutes_report.limit > 0
- return unless minutes_quota.enabled? && minutes_quota.purchased_minutes_report.limit > 0 && minutes_quota.any_project_enabled?
.row
.col-sm-6
......
......@@ -26,9 +26,9 @@
= 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
- if minutes_quota.enabled?
- if minutes_quota.enabled? && minutes_quota.any_project_enabled?
#{minutes_quota.monthly_percent_used}% used
- elsif !namespace.any_project_with_shared_runners_enabled?
- elsif !minutes_quota.any_project_enabled?
0% used
- else
= s_('UsageQuota|Unlimited')
......@@ -48,7 +48,7 @@
= _('Minutes')
%tbody
- if !namespace.any_project_with_shared_runners_enabled?
- if !minutes_quota.any_project_enabled?
%tr
%td{ colspan: 2 }
.nothing-here-block
......
......@@ -329,25 +329,6 @@ RSpec.describe Ci::Minutes::Quota do
end
end
describe '#actual_minutes_used_up?' do
subject { quota.actual_minutes_used_up? }
where(:minutes_used, :minutes_limit, :result, :title) do
100 | 0 | false | 'limit not enabled'
99 | 100 | false | 'total minutes not used'
101 | 100 | true | 'total minutes used'
end
with_them do
before do
allow(namespace).to receive(:shared_runners_seconds).and_return(minutes_used.minutes)
namespace.shared_runners_minutes_limit = minutes_limit
end
it { is_expected.to eq(result) }
end
end
describe '#total_minutes' do
subject { quota.total_minutes }
......@@ -430,7 +411,7 @@ RSpec.describe Ci::Minutes::Quota do
end
end
it 'does not trigger N+1 queries when called multiple times' do
it 'does not trigger additional queries when called multiple times' do
# memoizes the result
quota.namespace_eligible?
......@@ -442,4 +423,40 @@ RSpec.describe Ci::Minutes::Quota do
expect(actual.count).to eq(0)
end
end
describe '#any_project_enabled?' do
let_it_be(:project) { create(:project, namespace: namespace) }
context 'when namespace has any project with shared runners enabled' do
before do
project.update!(shared_runners_enabled: true)
end
it 'returns true' do
expect(quota.any_project_enabled?).to be_truthy
end
end
context 'when namespace has no projects with shared runners enabled' do
before do
project.update!(shared_runners_enabled: false)
end
it 'returns false' do
expect(quota.any_project_enabled?).to be_falsey
end
end
it 'does not trigger additional queries when called multiple times' do
# memoizes the result
quota.any_project_enabled?
# count
actual = ActiveRecord::QueryRecorder.new do
quota.any_project_enabled?
end
expect(actual.count).to eq(0)
end
end
end
......@@ -50,7 +50,7 @@ RSpec.describe Ci::PendingBuild do
context 'when ci minutes are not available' do
before do
allow_next_instance_of(::Ci::Minutes::Quota) do |instance|
allow(instance).to receive(:actual_minutes_used_up?).and_return(true)
allow(instance).to receive(:minutes_used_up?).and_return(true)
end
end
......
......@@ -30,7 +30,7 @@ RSpec.describe Ci::Minutes::RefreshCachedDataService do
context 'when user purchases more ci minutes for a given namespace' do
before do
allow_next_instance_of(::Ci::Minutes::Quota) do |instance|
allow(instance).to receive(:actual_minutes_used_up?).and_return(false)
allow(instance).to receive(:minutes_used_up?).and_return(false)
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