Commit 1f3794ea authored by Allison Browne's avatar Allison Browne Committed by Fabio Pitino

Add Quota Presenter to separate Quota related display logic

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