Commit 9d79e46f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'refactor-ci-minutes-progress-bar-2' into 'master'

Refactor ci minutes progress bar - pt2

See merge request gitlab-org/gitlab!30467
parents 8989fc1b b490ba64
...@@ -2,62 +2,17 @@ ...@@ -2,62 +2,17 @@
module EE module EE
module NamespacesHelper module NamespacesHelper
def namespace_extra_shared_runner_limits_quota(namespace) def ci_minutes_report(quota_report)
report = namespace.ci_minutes_quota.purchased_minutes_report content_tag(:span, class: "shared_runners_limit_#{quota_report.status}") do
"#{quota_report.used} / #{quota_report.limit}"
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
end end
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) def ci_minutes_progress_bar(percent)
status = status =
if percent >= 100 if percent >= 95
'danger' 'danger'
elsif percent >= 80 elsif percent >= 70
'warning' 'warning'
else else
'success' 'success'
......
...@@ -36,15 +36,15 @@ module Ci ...@@ -36,15 +36,15 @@ module Ci
Report.new(purchased_minutes_used, purchased_minutes, status) Report.new(purchased_minutes_used, purchased_minutes, status)
end end
private
# TODO: maps to NamespacesHelper#namespace_extra_shared_runner_limits_percent_used
def purchased_percent_used def purchased_percent_used
return 0 unless namespace.shared_runners_minutes_limit_enabled?
return 0 if purchased_minutes.zero? return 0 if purchased_minutes.zero?
100 * purchased_minutes_used.to_i / purchased_minutes 100 * purchased_minutes_used.to_i / purchased_minutes
end end
private
# TODO: maps to Namespace#shared_runners_minutes_used? # TODO: maps to Namespace#shared_runners_minutes_used?
def monthly_minutes_used_up? def monthly_minutes_used_up?
namespace.shared_runners_minutes_limit_enabled? && namespace.shared_runners_minutes_limit_enabled? &&
......
- namespace = local_assigns.fetch(:namespace) - namespace = local_assigns.fetch(:namespace)
- minutes_quota = namespace.ci_minutes_quota
- if namespace.shared_runners_enabled? - if namespace.shared_runners_enabled?
%li %li
%span.light Pipeline minutes quota: %span.light Pipeline minutes quota:
%strong %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' = 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? - return unless Gitlab.com? && namespace.shared_runners_minutes_limit_enabled?
- minutes_quota = namespace.ci_minutes_quota
.row .row
.col-sm-6 .col-sm-6
%strong %strong
= _("Additional minutes") = _("Additional minutes")
%div %div
= namespace_extra_shared_runner_limits_quota(namespace) = ci_minutes_report(minutes_quota.purchased_minutes_report)
minutes minutes
= link_to icon('question-circle'), help_page_path('subscriptions/index', anchor: 'extra-shared-runners-pipeline-minutes'), target: '_blank', rel: 'noopener noreferrer' = 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 .col-sm-6.right
#{namespace_extra_shared_runner_limits_percent_used(namespace)}% used #{minutes_quota.purchased_percent_used}% used
= namespace_extra_shared_runner_limits_progress_bar(namespace) = ci_minutes_progress_bar(minutes_quota.purchased_percent_used)
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
- else - else
= s_('UsageQuota|Current period usage') = s_('UsageQuota|Current period usage')
%div %div
= namespace_shared_runner_limits_quota(namespace) = ci_minutes_report(minutes_quota.monthly_minutes_report)
minutes 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') = 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 ...@@ -27,20 +27,20 @@ describe EE::NamespacesHelper do
expect(helper.ci_minutes_progress_bar(0)).to match(/success.*0%/) expect(helper.ci_minutes_progress_bar(0)).to match(/success.*0%/)
end end
it 'shows a green bar if percent is lower than 80' do it 'shows a green bar if percent is lower than 70' do
expect(helper.ci_minutes_progress_bar(50)).to match(/success.*50%/) expect(helper.ci_minutes_progress_bar(69)).to match(/success.*69%/)
end end
it 'shows a yellow bar if percent is 80' do it 'shows a yellow bar if percent is 70' do
expect(helper.ci_minutes_progress_bar(80)).to match(/warning.*80%/) expect(helper.ci_minutes_progress_bar(70)).to match(/warning.*70%/)
end end
it 'shows a yellow bar if percent is higher than 80 and lower than 100' do it 'shows a yellow bar if percent is higher than 70 and lower than 95' do
expect(helper.ci_minutes_progress_bar(90)).to match(/warning.*90%/) expect(helper.ci_minutes_progress_bar(94)).to match(/warning.*94%/)
end end
it 'shows a red bar if percent is 100' do it 'shows a red bar if percent is 95' do
expect(helper.ci_minutes_progress_bar(100)).to match(/danger.*100%/) expect(helper.ci_minutes_progress_bar(95)).to match(/danger.*95%/)
end end
it 'shows a red bar if percent is higher than 100 and caps the value to 100' do it 'shows a red bar if percent is higher than 100 and caps the value to 100' do
...@@ -48,20 +48,25 @@ describe EE::NamespacesHelper do ...@@ -48,20 +48,25 @@ describe EE::NamespacesHelper do
end end
end end
describe '#namespace_shared_runner_limits_quota' do describe '#ci_minutes_report' do
let(:quota) { Ci::Minutes::Quota.new(user_group) }
describe 'rendering monthly minutes report' do
let(:report) { quota.monthly_minutes_report }
context "when it's unlimited" do context "when it's unlimited" do
before do before do
allow(user_group).to receive(:shared_runners_minutes_limit_enabled?).and_return(false) allow(user_group).to receive(:shared_runners_minutes_limit_enabled?).and_return(false)
end end
it 'returns Unlimited for the limit section' do it 'returns Unlimited for the limit section' do
expect(helper.namespace_shared_runner_limits_quota(user_group)).to match(%r{0 / Unlimited}) expect(helper.ci_minutes_report(report)).to match(%r{0 / Unlimited})
end end
it 'returns the proper value for the used section' do it 'returns the proper value for the used section' do
allow(user_group).to receive(:shared_runners_seconds).and_return(100 * 60) allow(user_group).to receive(:shared_runners_seconds).and_return(100 * 60)
expect(helper.namespace_shared_runner_limits_quota(user_group)).to match(%r{100 / Unlimited}) expect(helper.ci_minutes_report(report)).to match(%r{100 / Unlimited})
end end
end end
...@@ -74,24 +79,27 @@ describe EE::NamespacesHelper do ...@@ -74,24 +79,27 @@ describe EE::NamespacesHelper do
end end
it 'returns the proper values for used and limit sections' do 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}) expect(helper.ci_minutes_report(report)).to match(%r{100 / 500})
end end
end end
end end
describe '#namespace_extra_shared_runner_limits_quota' do describe 'rendering purchased minutes report' do
let(:report) { 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
allow(user_group).to receive(:shared_runners_seconds).and_return(50 * 60) allow(user_group).to receive(:shared_runners_seconds).and_return(50 * 60)
user_group.update!(extra_shared_runners_minutes_limit: 100) user_group.update!(extra_shared_runners_minutes_limit: 100)
expect(helper.namespace_extra_shared_runner_limits_quota(user_group)).to match(%r{50 / 100}) expect(helper.ci_minutes_report(report)).to match(%r{50 / 100})
end end
end end
context 'when extra minutes are not assigned' do context 'when extra minutes are not assigned' do
it 'returns the proper values for used and limit sections' 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}) expect(helper.ci_minutes_report(report)).to match(%r{0 / 0})
end
end end
end end
end end
......
...@@ -197,4 +197,34 @@ describe Ci::Minutes::Quota do ...@@ -197,4 +197,34 @@ describe Ci::Minutes::Quota do
end end
end 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 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