Commit b490ba64 authored by Fabio Pitino's avatar Fabio Pitino Committed by Grzegorz Bizon

Extract Ci::Minutes::Quota class

Move some of the logic from NamespacesHelper into
new class and implement related methods from
Namespace and NamespaceStatistics.
parent 5f353263
......@@ -2,62 +2,17 @@
module EE
module NamespacesHelper
def namespace_extra_shared_runner_limits_quota(namespace)
report = namespace.ci_minutes_quota.purchased_minutes_report
content_tag(:span, class: "shared_runners_limit_#{report.status}") do
"#{report.used} / #{report.limit}"
end
end
def namespace_shared_runner_limits_quota(namespace)
report = namespace.ci_minutes_quota.monthly_minutes_report
content_tag(:span, class: "shared_runners_limit_#{report.status}") do
"#{report.used} / #{report.limit}"
end
end
def namespace_extra_shared_runner_limits_percent_used(namespace)
limit = namespace.extra_shared_runners_minutes_limit.to_i
return 0 if limit.zero?
100 * namespace.extra_shared_runners_minutes.to_i / limit
end
def namespace_shared_runner_usage_progress_bar(percent)
status =
if percent == 100
'danger'
elsif percent >= 80
'warning'
else
'success'
end
options = {
class: "progress-bar bg-#{status}",
style: "width: #{percent}%;"
}
content_tag :div, class: 'progress' do
content_tag :div, nil, options
def ci_minutes_report(quota_report)
content_tag(:span, class: "shared_runners_limit_#{quota_report.status}") do
"#{quota_report.used} / #{quota_report.limit}"
end
end
def namespace_extra_shared_runner_limits_progress_bar(namespace)
used = namespace_extra_shared_runner_limits_percent_used(namespace)
percent = [used, 100].min
namespace_shared_runner_usage_progress_bar(percent)
end
def ci_minutes_progress_bar(percent)
status =
if percent >= 100
if percent >= 95
'danger'
elsif percent >= 80
elsif percent >= 70
'warning'
else
'success'
......
......@@ -36,15 +36,15 @@ module Ci
Report.new(purchased_minutes_used, purchased_minutes, status)
end
private
# TODO: maps to NamespacesHelper#namespace_extra_shared_runner_limits_percent_used
def purchased_percent_used
return 0 unless namespace.shared_runners_minutes_limit_enabled?
return 0 if purchased_minutes.zero?
100 * purchased_minutes_used.to_i / purchased_minutes
end
private
# TODO: maps to Namespace#shared_runners_minutes_used?
def monthly_minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? &&
......
- namespace = local_assigns.fetch(:namespace)
- minutes_quota = namespace.ci_minutes_quota
- if namespace.shared_runners_enabled?
%li
%span.light Pipeline minutes quota:
%strong
= namespace_shared_runner_limits_quota(namespace)
= ci_minutes_report(minutes_quota.monthly_minutes_report)
= link_to icon('question-circle'), help_page_path("user/admin_area/settings/continuous_integration", anchor: "shared-runners-build-minutes-quota"), target: '_blank'
- return unless Gitlab.com? && namespace.shared_runners_minutes_limit_enabled?
- minutes_quota = namespace.ci_minutes_quota
.row
.col-sm-6
%strong
= _("Additional minutes")
%div
= namespace_extra_shared_runner_limits_quota(namespace)
= ci_minutes_report(minutes_quota.purchased_minutes_report)
minutes
= link_to icon('question-circle'), help_page_path('subscriptions/index', anchor: 'extra-shared-runners-pipeline-minutes'), target: '_blank', rel: 'noopener noreferrer'
.col-sm-6.right
#{namespace_extra_shared_runner_limits_percent_used(namespace)}% used
= namespace_extra_shared_runner_limits_progress_bar(namespace)
#{minutes_quota.purchased_percent_used}% used
= ci_minutes_progress_bar(minutes_quota.purchased_percent_used)
......@@ -22,7 +22,7 @@
- else
= s_('UsageQuota|Current period usage')
%div
= namespace_shared_runner_limits_quota(namespace)
= ci_minutes_report(minutes_quota.monthly_minutes_report)
minutes
= link_to icon('question-circle'), help_page_path('user/admin_area/settings/continuous_integration', anchor: 'shared-runners-build-minutes-quota'), target: '_blank', 'aria-label': _('Shared runners help link')
......
......@@ -27,20 +27,20 @@ describe EE::NamespacesHelper do
expect(helper.ci_minutes_progress_bar(0)).to match(/success.*0%/)
end
it 'shows a green bar if percent is lower than 80' do
expect(helper.ci_minutes_progress_bar(50)).to match(/success.*50%/)
it 'shows a green bar if percent is lower than 70' do
expect(helper.ci_minutes_progress_bar(69)).to match(/success.*69%/)
end
it 'shows a yellow bar if percent is 80' do
expect(helper.ci_minutes_progress_bar(80)).to match(/warning.*80%/)
it 'shows a yellow bar if percent is 70' do
expect(helper.ci_minutes_progress_bar(70)).to match(/warning.*70%/)
end
it 'shows a yellow bar if percent is higher than 80 and lower than 100' do
expect(helper.ci_minutes_progress_bar(90)).to match(/warning.*90%/)
it 'shows a yellow bar if percent is higher than 70 and lower than 95' do
expect(helper.ci_minutes_progress_bar(94)).to match(/warning.*94%/)
end
it 'shows a red bar if percent is 100' do
expect(helper.ci_minutes_progress_bar(100)).to match(/danger.*100%/)
it 'shows a red bar if percent is 95' do
expect(helper.ci_minutes_progress_bar(95)).to match(/danger.*95%/)
end
it 'shows a red bar if percent is higher than 100 and caps the value to 100' do
......@@ -48,50 +48,58 @@ describe EE::NamespacesHelper do
end
end
describe '#namespace_shared_runner_limits_quota' do
context "when it's unlimited" do
before do
allow(user_group).to receive(:shared_runners_minutes_limit_enabled?).and_return(false)
end
describe '#ci_minutes_report' do
let(:quota) { Ci::Minutes::Quota.new(user_group) }
it 'returns Unlimited for the limit section' do
expect(helper.namespace_shared_runner_limits_quota(user_group)).to match(%r{0 / Unlimited})
end
describe 'rendering monthly minutes report' do
let(:report) { quota.monthly_minutes_report }
it 'returns the proper value for the used section' do
allow(user_group).to receive(:shared_runners_seconds).and_return(100 * 60)
context "when it's unlimited" do
before do
allow(user_group).to receive(:shared_runners_minutes_limit_enabled?).and_return(false)
end
expect(helper.namespace_shared_runner_limits_quota(user_group)).to match(%r{100 / Unlimited})
end
end
it 'returns Unlimited for the limit section' do
expect(helper.ci_minutes_report(report)).to match(%r{0 / Unlimited})
end
context "when it's limited" do
before do
allow(user_group).to receive(:shared_runners_minutes_limit_enabled?).and_return(true)
allow(user_group).to receive(:shared_runners_seconds).and_return(100 * 60)
it 'returns the proper value for the used section' do
allow(user_group).to receive(:shared_runners_seconds).and_return(100 * 60)
user_group.update!(shared_runners_minutes_limit: 500)
expect(helper.ci_minutes_report(report)).to match(%r{100 / Unlimited})
end
end
it 'returns the proper values for used and limit sections' do
expect(helper.namespace_shared_runner_limits_quota(user_group)).to match(%r{100 / 500})
context "when it's limited" do
before do
allow(user_group).to receive(:shared_runners_minutes_limit_enabled?).and_return(true)
allow(user_group).to receive(:shared_runners_seconds).and_return(100 * 60)
user_group.update!(shared_runners_minutes_limit: 500)
end
it 'returns the proper values for used and limit sections' do
expect(helper.ci_minutes_report(report)).to match(%r{100 / 500})
end
end
end
end
describe '#namespace_extra_shared_runner_limits_quota' do
context 'when extra minutes are assigned' do
it 'returns the proper values for used and limit sections' do
allow(user_group).to receive(:shared_runners_seconds).and_return(50 * 60)
user_group.update!(extra_shared_runners_minutes_limit: 100)
describe 'rendering purchased minutes report' do
let(:report) { quota.purchased_minutes_report }
expect(helper.namespace_extra_shared_runner_limits_quota(user_group)).to match(%r{50 / 100})
context 'when extra minutes are assigned' do
it 'returns the proper values for used and limit sections' do
allow(user_group).to receive(:shared_runners_seconds).and_return(50 * 60)
user_group.update!(extra_shared_runners_minutes_limit: 100)
expect(helper.ci_minutes_report(report)).to match(%r{50 / 100})
end
end
end
context 'when extra minutes are not assigned' do
it 'returns the proper values for used and limit sections' do
expect(helper.namespace_extra_shared_runner_limits_quota(user_group)).to match(%r{0 / 0})
context 'when extra minutes are not assigned' do
it 'returns the proper values for used and limit sections' do
expect(helper.ci_minutes_report(report)).to match(%r{0 / 0})
end
end
end
end
......
......@@ -197,4 +197,34 @@ describe Ci::Minutes::Quota do
end
end
end
describe '#purchased_percent_used' do
subject { quota.purchased_percent_used }
where(:limit_enabled, :monthly_limit, :purchased_limit, :minutes_used, :result, :title) do
false | 0 | 0 | 40 | 0 | 'limit not enabled'
true | 0 | 200 | 40 | 20 | 'monthly limit not set and purchased limit set and low usage'
true | 200 | 0 | 40 | 0 | 'monthly limit set and purchased limit not set and usage below monthly'
true | 200 | 0 | 240 | 0 | 'monthly limit set and purchased limit not set and usage above monthly'
true | 200 | 200 | 0 | 0 | 'monthly and purchased limits set and no usage'
true | 200 | 200 | 40 | 0 | 'monthly and purchased limits set and usage below monthly'
true | 200 | 200 | 200 | 0 | 'monthly and purchased limits set and monthly minutes maxed out'
true | 200 | 200 | 300 | 50 | 'monthly and purchased limits set and some purchased minutes used'
true | 200 | 200 | 400 | 100 | 'monthly and purchased limits set and all minutes used'
true | 200 | 200 | 430 | 115 | 'monthly and purchased limits set and usage beyond all limits'
end
with_them do
before do
allow(namespace).to receive(:shared_runners_minutes_limit_enabled?).and_return(limit_enabled)
namespace.shared_runners_minutes_limit = monthly_limit
namespace.extra_shared_runners_minutes_limit = purchased_limit
namespace.namespace_statistics.shared_runners_seconds = minutes_used.minutes
end
it 'returns the percentage' do
is_expected.to eq result
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