Commit 6028f32b authored by Fabio Pitino's avatar Fabio Pitino

Merge branch 'explicit-unlimited-minutes' into 'master'

Add Quota Presenter to separate Quota related display logic

See merge request gitlab-org/gitlab!68659
parents 808ede0a 1f3794ea
......@@ -9,39 +9,14 @@ module Ci
class Quota
include Gitlab::Utils::StrongMemoize
Report = Struct.new(:used, :limit, :status)
attr_reader :namespace
def initialize(namespace)
@namespace = namespace
end
def enabled?
namespace_root? && total_minutes.nonzero?
end
# Status of the monthly allowance being used.
def monthly_minutes_report
Report.new(monthly_minutes_used, minutes_limit, report_status)
end
def monthly_percent_used
return 0 unless enabled?
return 0 if monthly_minutes == 0
100 * monthly_minutes_used.to_i / monthly_minutes
end
# Status of any purchased minutes used.
def purchased_minutes_report
status = purchased_minutes_used_up? ? :over_quota : :under_quota
Report.new(purchased_minutes_used, purchased_minutes, status)
end
def purchased_percent_used
return 0 unless enabled?
return 0 if purchased_minutes == 0
100 * purchased_minutes_used.to_i / purchased_minutes
namespace_root? && !namespace_unlimited_minutes?
end
def minutes_used_up?
......@@ -58,14 +33,6 @@ module Ci
total_minutes.to_i - total_minutes_used
end
def display_shared_runners_data?
namespace_root? && any_project_enabled?
end
def display_minutes_available_data?
display_shared_runners_data? && total_minutes.nonzero?
end
def total_minutes
strong_memoize(:total_minutes) do
monthly_minutes + purchased_minutes
......@@ -78,54 +45,43 @@ module Ci
end
end
def any_project_enabled?
strong_memoize(:any_project_enabled) do
namespace.any_project_with_shared_runners_enabled?
def purchased_minutes
strong_memoize(:purchased_minutes) do
namespace.extra_shared_runners_minutes_limit.to_i
end
end
private
attr_reader :namespace
def minutes_limit
return _('Not supported') unless display_shared_runners_data?
if display_minutes_available_data?
monthly_minutes
else
_('Unlimited')
def namespace_root?
strong_memoize(:namespace_root) do
namespace.root?
end
end
def report_status
return :disabled unless enabled?
monthly_minutes_used_up? ? :over_quota : :under_quota
def namespace_unlimited_minutes?
total_minutes.to_i == 0
end
def total_minutes_remaining
[current_balance, 0].max
def monthly_minutes
strong_memoize(:monthly_minutes) do
(namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i
end
end
# === private to view ===
def monthly_minutes_used_up?
return false unless enabled?
monthly_minutes_used >= monthly_minutes
end
def purchased_minutes_used_up?
return false unless enabled?
any_minutes_purchased? && purchased_minutes_used >= purchased_minutes
end
def monthly_minutes_used
total_minutes_used - purchased_minutes_used
end
def monthly_minutes_available?
total_minutes_used <= monthly_minutes
def purchased_minutes_used_up?
return false unless enabled?
any_minutes_purchased? && purchased_minutes_used >= purchased_minutes
end
def purchased_minutes_used
......@@ -134,6 +90,12 @@ module Ci
total_minutes_used - monthly_minutes
end
private
def monthly_minutes_available?
total_minutes_used <= monthly_minutes
end
def no_minutes_purchased?
purchased_minutes == 0
end
......@@ -142,22 +104,9 @@ module Ci
purchased_minutes > 0
end
def monthly_minutes
strong_memoize(:monthly_minutes) do
(namespace.shared_runners_minutes_limit || ::Gitlab::CurrentSettings.shared_runners_minutes).to_i
end
end
def purchased_minutes
strong_memoize(:purchased_minutes) do
namespace.extra_shared_runners_minutes_limit.to_i
end
end
def namespace_root?
strong_memoize(:namespace_root) do
namespace.root?
end
# === private to model ===
def total_minutes_remaining
[current_balance, 0].max
end
end
end
......
# frozen_string_literal: true
module Ci
module Minutes
class QuotaPresenter < Gitlab::View::Presenter::Simple
include Gitlab::Utils::StrongMemoize
presents Quota, as: :quota
Report = Struct.new(:used, :limit, :status)
# Status of the monthly allowance being used.
def monthly_minutes_report
Report.new(quota.monthly_minutes_used, minutes_limit, report_status)
end
def monthly_percent_used
return 0 unless quota.enabled?
return 0 if quota.monthly_minutes == 0
100 * quota.monthly_minutes_used.to_i / quota.monthly_minutes
end
# Status of any purchased minutes used.
def purchased_minutes_report
status = quota.purchased_minutes_used_up? ? :over_quota : :under_quota
Report.new(quota.purchased_minutes_used, quota.purchased_minutes, status)
end
def purchased_percent_used
return 0 unless quota.enabled?
return 0 if quota.purchased_minutes == 0
100 * quota.purchased_minutes_used.to_i / quota.purchased_minutes
end
def display_minutes_available_data?
display_shared_runners_data? && !quota.namespace_unlimited_minutes?
end
def display_shared_runners_data?
quota.namespace_root? && any_project_enabled?
end
def any_project_enabled?
strong_memoize(:any_project_enabled) do
quota.namespace.any_project_with_shared_runners_enabled?
end
end
private
def report_status
return :disabled unless quota.enabled?
quota.monthly_minutes_used_up? ? :over_quota : :under_quota
end
def minutes_limit
return _('Not supported') unless display_shared_runners_data?
if display_minutes_available_data?
quota.monthly_minutes
else
_('Unlimited')
end
end
end
end
end
......@@ -2,11 +2,12 @@
- namespace = local_assigns.fetch(:namespace)
- minutes_quota = namespace.ci_minutes_quota
- minutes_quota_presenter = Ci::Minutes::QuotaPresenter.new(minutes_quota)
- return unless minutes_quota.enabled?
- if minutes_quota.display_shared_runners_data?
- if minutes_quota_presenter.display_shared_runners_data?
%li
%span.light= _('Additional minutes:')
%strong
= ci_minutes_report(minutes_quota.purchased_minutes_report)
= ci_minutes_report(minutes_quota_presenter.purchased_minutes_report)
- namespace = local_assigns.fetch(:namespace)
- minutes_quota = namespace.ci_minutes_quota
- minutes_quota_presenter = Ci::Minutes::QuotaPresenter.new(minutes_quota)
- if minutes_quota.display_shared_runners_data?
- if minutes_quota_presenter.display_shared_runners_data?
%li
%span.light= _('Pipeline minutes quota:')
%strong
= ci_minutes_report(minutes_quota.monthly_minutes_report)
= ci_minutes_report(minutes_quota_presenter.monthly_minutes_report)
= link_to sprite_icon('question-o'), help_page_path('user/admin_area/settings/continuous_integration', anchor: 'shared-runners-pipeline-minutes-quota'), target: '_blank'
- return unless Gitlab.com?
- minutes_quota = namespace.ci_minutes_quota
- return unless minutes_quota.display_minutes_available_data? && minutes_quota.purchased_minutes_report.limit > 0
- minutes_quota_presenter = Ci::Minutes::QuotaPresenter.new(minutes_quota)
- return unless minutes_quota_presenter.display_minutes_available_data? && minutes_quota_presenter.purchased_minutes_report.limit > 0
.row
.col-sm-6
%strong
= _("Additional minutes")
%div
= ci_minutes_report(minutes_quota.purchased_minutes_report)
= ci_minutes_report(minutes_quota_presenter.purchased_minutes_report)
minutes
= link_to sprite_icon('question-o'), help_page_path('subscriptions/gitlab_com/index', anchor: 'purchase-additional-ci-minutes'), target: '_blank', rel: 'noopener noreferrer'
.col-sm-6.right
#{minutes_quota.purchased_percent_used}% used
= ci_minutes_progress_bar(minutes_quota.purchased_percent_used)
#{minutes_quota_presenter.purchased_percent_used}% used
= ci_minutes_progress_bar(minutes_quota_presenter.purchased_percent_used)
- namespace = locals.fetch(:namespace)
- projects = locals.fetch(:projects)
- minutes_quota = namespace.ci_minutes_quota
- minutes_quota_presenter = Ci::Minutes::QuotaPresenter.new(minutes_quota)
.pipeline-quota.container-fluid
.row
......@@ -21,19 +23,19 @@
- else
= s_('UsageQuota|Current period usage')
%div
= ci_minutes_report(minutes_quota.monthly_minutes_report)
= ci_minutes_report(minutes_quota_presenter.monthly_minutes_report)
minutes
= 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.display_minutes_available_data?
#{minutes_quota.monthly_percent_used}% used
- elsif !minutes_quota.any_project_enabled?
- if minutes_quota_presenter.display_minutes_available_data?
#{minutes_quota_presenter.monthly_percent_used}% used
- elsif !minutes_quota_presenter.any_project_enabled?
0% used
- else
= s_('UsageQuota|Unlimited')
= ci_minutes_progress_bar(minutes_quota.monthly_percent_used)
= ci_minutes_progress_bar(minutes_quota_presenter.monthly_percent_used)
= render 'namespaces/pipelines_quota/extra_shared_runners_minutes_quota', namespace: namespace
......@@ -48,7 +50,7 @@
= _('Minutes')
%tbody
- if !minutes_quota.any_project_enabled?
- if !minutes_quota_presenter.any_project_enabled?
%tr
%td{ colspan: 2 }
.nothing-here-block
......
......@@ -51,9 +51,10 @@ RSpec.describe EE::NamespacesHelper do
describe '#ci_minutes_report' do
let(:quota) { Ci::Minutes::Quota.new(user_group) }
let(:quota_presenter) { Ci::Minutes::QuotaPresenter.new(quota) }
describe 'rendering monthly minutes report' do
let(:report) { quota.monthly_minutes_report }
let(:report) { quota_presenter.monthly_minutes_report }
context "when ci minutes quota is not enabled" do
before do
......@@ -62,7 +63,7 @@ RSpec.describe EE::NamespacesHelper do
context 'and the namespace is eligible for unlimited' do
before do
allow(quota).to receive(:namespace_eligible?).and_return(true)
allow(quota).to receive(:namespace_root?).and_return(true)
allow(user_group).to receive(:any_project_with_shared_runners_enabled?).and_return(true)
end
......@@ -79,7 +80,7 @@ RSpec.describe EE::NamespacesHelper do
context 'and the namespace is not eligible for unlimited' do
before do
allow(quota).to receive(:namespace_eligible?).and_return(false)
allow(quota).to receive(:namespace_root?).and_return(false)
end
it 'returns Not supported for the limit section' do
......@@ -103,7 +104,7 @@ RSpec.describe EE::NamespacesHelper do
end
describe 'rendering purchased minutes report' do
let(:report) { quota.purchased_minutes_report }
let(:report) { Ci::Minutes::QuotaPresenter.new(quota).purchased_minutes_report }
context 'when extra minutes are assigned' do
it 'returns the proper values for used and limit sections' do
......
This diff is collapsed.
This diff is collapsed.
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