Commit a64c89bf authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'ci-move-shared-runners-minutes-to-ci-quota' into 'master'

Move (extra_)shared_runners_minutes to Ci::Minutes::Quota

See merge request gitlab-org/gitlab!47712
parents d81d4d41 63a5cf8c
......@@ -48,6 +48,10 @@ module Ci
total_minutes_used >= total_minutes
end
def total_minutes_used
@total_minutes_used ||= namespace.shared_runners_seconds.to_i / 60
end
private
def monthly_minutes_used_up?
......@@ -61,7 +65,6 @@ module Ci
purchased_minutes_used >= purchased_minutes
end
# TODO: maps to NamespaceStatistics#shared_runners_minutes(include_extra: false)
def monthly_minutes_used
total_minutes_used - purchased_minutes_used
end
......@@ -70,7 +73,6 @@ module Ci
total_minutes_used <= monthly_minutes
end
# TODO: maps to NamespaceStatistics#extra_shared_runners_minutes
def purchased_minutes_used
return 0 if no_minutes_purchased? || monthly_minutes_available?
......@@ -90,11 +92,6 @@ module Ci
@total_minutes ||= monthly_minutes + purchased_minutes
end
# TODO: maps to NamespaceStatistics#shared_runners_minutes(include_extra: true)
def total_minutes_used
@total_minutes_used ||= namespace.shared_runners_seconds.to_i / 60
end
# TODO: maps to Namespace#actual_shared_runners_minutes_limit(include_extra: false)
def monthly_minutes
@monthly_minutes ||= (namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i
......
......@@ -57,8 +57,7 @@ module EE
where("EXISTS (?)", matcher)
end
delegate :shared_runners_minutes, :shared_runners_seconds, :shared_runners_seconds_last_reset,
:extra_shared_runners_minutes, to: :namespace_statistics, allow_nil: true
delegate :shared_runners_seconds, :shared_runners_seconds_last_reset, to: :namespace_statistics, allow_nil: true
delegate :additional_purchased_storage_size, :additional_purchased_storage_size=,
:additional_purchased_storage_ends_on, :additional_purchased_storage_ends_on=,
......@@ -261,12 +260,6 @@ module EE
shared_runners_remaining_minutes_percent.to_i <= last_ci_minutes_usage_notification_level.to_i
end
def extra_shared_runners_minutes_used?
shared_runners_minutes_limit_enabled? &&
extra_shared_runners_minutes_limit &&
extra_shared_runners_minutes.to_i >= extra_shared_runners_minutes_limit
end
def any_project_with_shared_runners_enabled?
all_projects.with_shared_runners.any?
end
......@@ -437,7 +430,7 @@ module EE
end
def shared_runners_remaining_minutes
[actual_shared_runners_minutes_limit.to_f - shared_runners_minutes.to_f, 0].max
[actual_shared_runners_minutes_limit.to_f - ci_minutes_quota.total_minutes_used.to_f, 0].max
end
def total_repository_size_excess_calculation(repository_size_limit, project_level: true)
......
......@@ -6,19 +6,4 @@ class NamespaceStatistics < ApplicationRecord
validates :namespace, presence: true
scope :for_namespaces, -> (namespaces) { where(namespace: namespaces) }
def shared_runners_minutes(include_extra: true)
minutes = shared_runners_seconds.to_i / 60
include_extra ? minutes : minutes - extra_shared_runners_minutes
end
def extra_shared_runners_minutes
limit = namespace.actual_shared_runners_minutes_limit(include_extra: false)
extra_limit = namespace.extra_shared_runners_minutes_limit.to_i
return 0 if extra_limit == 0 || shared_runners_minutes <= limit
shared_runners_minutes - limit
end
end
......@@ -8,7 +8,7 @@ module EE
expose :runners do
expose :quota, if: -> (*) { project.shared_runners_minutes_limit_enabled? } do
expose :used do |runner|
project.shared_runners_limit_namespace.shared_runners_minutes.to_i
::Ci::Minutes::Quota.new(project.shared_runners_limit_namespace).total_minutes_used
end
expose :limit do |runner|
......
......@@ -255,4 +255,24 @@ RSpec.describe Ci::Minutes::Quota do
it { is_expected.to eq(result) }
end
end
describe '#total_minutes_used' do
subject { quota.total_minutes_used }
where(:expected_seconds, :expected_minutes) do
nil | 0
0 | 0
59 | 0
60 | 1
122 | 2
end
with_them do
before do
allow(namespace).to receive(:shared_runners_seconds).and_return(expected_seconds)
end
it { is_expected.to eq(expected_minutes) }
end
end
end
......@@ -18,8 +18,6 @@ RSpec.describe Namespace do
it { is_expected.to have_one(:namespace_limit) }
it { is_expected.to have_one(:elasticsearch_indexed_namespace) }
it { is_expected.to delegate_method(:extra_shared_runners_minutes).to(:namespace_statistics) }
it { is_expected.to delegate_method(:shared_runners_minutes).to(:namespace_statistics) }
it { is_expected.to delegate_method(:shared_runners_seconds).to(:namespace_statistics) }
it { is_expected.to delegate_method(:shared_runners_seconds_last_reset).to(:namespace_statistics) }
it { is_expected.to delegate_method(:trial?).to(:gitlab_subscription) }
......@@ -690,7 +688,9 @@ RSpec.describe Namespace do
end
def stub_minutes_used_and_limit(minutes_used, limit)
allow(namespace).to receive(:shared_runners_minutes).and_return(minutes_used)
seconds_used = minutes_used.present? ? minutes_used * 60 : minutes_used
allow(namespace).to receive(:shared_runners_seconds).and_return(seconds_used)
allow(namespace).to receive(:actual_shared_runners_minutes_limit).and_return(limit)
end
end
......
......@@ -6,91 +6,4 @@ RSpec.describe NamespaceStatistics do
it { is_expected.to belong_to(:namespace) }
it { is_expected.to validate_presence_of(:namespace) }
describe '#shared_runners_minutes' do
let(:namespace_statistics) { build(:namespace_statistics, shared_runners_seconds: 120) }
it { expect(namespace_statistics.shared_runners_minutes).to eq(2) }
end
describe '#extra_shared_runners_minutes' do
subject { namespace_statistics.extra_shared_runners_minutes }
let(:namespace) { create(:namespace, shared_runners_minutes_limit: 100) }
let(:namespace_statistics) { create(:namespace_statistics, namespace: namespace) }
context 'when limit is defined' do
before do
namespace.update_attribute(:extra_shared_runners_minutes_limit, 50)
end
context 'when usage is above the main quota' do
before do
namespace_statistics.update_attribute(:shared_runners_seconds, 101 * 60)
end
it { is_expected.to eq(1) }
end
context 'when usage is below the main quota' do
before do
namespace_statistics.update_attribute(:shared_runners_seconds, 99 * 60)
end
it { is_expected.to eq(0) }
end
end
context 'without limit' do
before do
namespace.update_attribute(:extra_shared_runners_minutes_limit, nil)
end
it { is_expected.to eq(0) }
end
context 'when limit is defined globally' do
before do
namespace.update_attribute(:shared_runners_minutes_limit, nil)
stub_application_setting(shared_runners_minutes: 100)
end
context 'when usage is above the main quota' do
before do
namespace_statistics.update_attribute(:shared_runners_seconds, 101 * 60)
end
context 'and extra CI minutes have been assigned' do
before do
namespace.update_attribute(:extra_shared_runners_minutes_limit, 50)
end
it { is_expected.to eq(1) }
end
context 'and extra CI minutes have not been assigned' do
before do
namespace.update_attribute(:extra_shared_runners_minutes_limit, nil)
end
it { is_expected.to eq(0) }
end
end
context 'when usage is below the main quota' do
before do
namespace_statistics.update_attribute(:shared_runners_seconds, 90 * 60)
end
context 'and extra CI minutes have been assigned' do
before do
namespace.update_attribute(:extra_shared_runners_minutes_limit, 50)
end
it { is_expected.to eq(0) }
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