Commit 3219bfa5 authored by Grzegorz Bizon's avatar Grzegorz Bizon

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

Refactor ci minutes progress bar

See merge request gitlab-org/gitlab!30276
parents d635c709 051c4ff9
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module EE module EE
module NamespacesHelper module NamespacesHelper
def namespace_extra_shared_runner_limits_quota(namespace) def namespace_extra_shared_runner_limits_quota(namespace)
report = ::Ci::Minutes::Quota.new(namespace).purchased_minutes_report report = namespace.ci_minutes_quota.purchased_minutes_report
content_tag(:span, class: "shared_runners_limit_#{report.status}") do content_tag(:span, class: "shared_runners_limit_#{report.status}") do
"#{report.used} / #{report.limit}" "#{report.used} / #{report.limit}"
...@@ -11,7 +11,7 @@ module EE ...@@ -11,7 +11,7 @@ module EE
end end
def namespace_shared_runner_limits_quota(namespace) def namespace_shared_runner_limits_quota(namespace)
report = ::Ci::Minutes::Quota.new(namespace).monthly_minutes_report report = namespace.ci_minutes_quota.monthly_minutes_report
content_tag(:span, class: "shared_runners_limit_#{report.status}") do content_tag(:span, class: "shared_runners_limit_#{report.status}") do
"#{report.used} / #{report.limit}" "#{report.used} / #{report.limit}"
...@@ -26,12 +26,6 @@ module EE ...@@ -26,12 +26,6 @@ module EE
100 * namespace.extra_shared_runners_minutes.to_i / limit 100 * namespace.extra_shared_runners_minutes.to_i / limit
end end
def namespace_shared_runner_limits_percent_used(namespace)
return 0 unless namespace.shared_runners_minutes_limit_enabled?
100 * namespace.shared_runners_minutes(include_extra: false).to_i / namespace.actual_shared_runners_minutes_limit(include_extra: false)
end
def namespace_shared_runner_usage_progress_bar(percent) def namespace_shared_runner_usage_progress_bar(percent)
status = status =
if percent == 100 if percent == 100
...@@ -52,11 +46,33 @@ module EE ...@@ -52,11 +46,33 @@ module EE
end end
end end
def namespace_shared_runner_limits_progress_bar(namespace, extra: false) def namespace_extra_shared_runner_limits_progress_bar(namespace)
used = extra ? namespace_extra_shared_runner_limits_percent_used(namespace) : namespace_shared_runner_limits_percent_used(namespace) used = namespace_extra_shared_runner_limits_percent_used(namespace)
percent = [used, 100].min percent = [used, 100].min
namespace_shared_runner_usage_progress_bar(percent) namespace_shared_runner_usage_progress_bar(percent)
end end
def ci_minutes_progress_bar(percent)
status =
if percent >= 100
'danger'
elsif percent >= 80
'warning'
else
'success'
end
width = [percent, 100].min
options = {
class: "progress-bar bg-#{status}",
style: "width: #{width}%;"
}
content_tag :div, class: 'progress' do
content_tag :div, nil, options
end
end
end end
end end
...@@ -23,6 +23,13 @@ module Ci ...@@ -23,6 +23,13 @@ module Ci
end end
end end
def monthly_percent_used
return 0 unless namespace.shared_runners_minutes_limit_enabled?
return 0 if monthly_minutes.zero?
100 * monthly_minutes_used.to_i / monthly_minutes
end
# Status of any purchased minutes used. # Status of any purchased minutes used.
def purchased_minutes_report def purchased_minutes_report
status = purchased_minutes_used_up? ? :over_quota : :under_quota status = purchased_minutes_used_up? ? :over_quota : :under_quota
...@@ -31,16 +38,7 @@ module Ci ...@@ -31,16 +38,7 @@ module Ci
private private
# TODO: maps to NamespacesHelper#namespace_shared_runner_limits_percent_used
# TODO: consider including this into monthly_minutes_report
def monthly_percent_used
return 0 unless namespace.shared_runners_minutes_limit_enabled?
100 * monthly_minutes_used.to_i / monthly_minutes
end
# TODO: maps to NamespacesHelper#namespace_extra_shared_runner_limits_percent_used # TODO: maps to NamespacesHelper#namespace_extra_shared_runner_limits_percent_used
# TODO: consider including this into purchased_minutes_report
def purchased_percent_used def purchased_percent_used
return 0 if purchased_minutes.zero? return 0 if purchased_minutes.zero?
......
...@@ -182,6 +182,10 @@ module EE ...@@ -182,6 +182,10 @@ module EE
end end
end end
def ci_minutes_quota
@ci_minutes_quota ||= ::Ci::Minutes::Quota.new(self)
end
def shared_runner_minutes_supported? def shared_runner_minutes_supported?
!has_parent? !has_parent?
end end
......
...@@ -10,4 +10,4 @@ ...@@ -10,4 +10,4 @@
= 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 #{namespace_extra_shared_runner_limits_percent_used(namespace)}% used
= namespace_shared_runner_limits_progress_bar(namespace, extra: true) = namespace_extra_shared_runner_limits_progress_bar(namespace)
- namespace = locals.fetch(:namespace) - namespace = locals.fetch(:namespace)
- projects = locals.fetch(:projects) - projects = locals.fetch(:projects)
- minutes_quota = namespace.ci_minutes_quota
.pipeline-quota.container-fluid .pipeline-quota.container-fluid
.row .row
...@@ -27,13 +28,13 @@ ...@@ -27,13 +28,13 @@
.col-sm-6.right .col-sm-6.right
- if namespace.shared_runners_minutes_limit_enabled? - if namespace.shared_runners_minutes_limit_enabled?
#{namespace_shared_runner_limits_percent_used(namespace)}% used #{minutes_quota.monthly_percent_used}% used
- elsif !namespace.shared_runners_enabled? - elsif !namespace.shared_runners_enabled?
0% used 0% used
- else - else
= s_('UsageQuota|Unlimited') = s_('UsageQuota|Unlimited')
= namespace_shared_runner_limits_progress_bar(namespace) = ci_minutes_progress_bar(minutes_quota.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
......
...@@ -22,6 +22,32 @@ describe EE::NamespacesHelper do ...@@ -22,6 +22,32 @@ describe EE::NamespacesHelper do
user_group.add_owner(user) user_group.add_owner(user)
end end
describe '#ci_minutes_progress_bar' do
it 'shows a green bar if percent is 0' 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%/)
end
it 'shows a yellow bar if percent is 80' do
expect(helper.ci_minutes_progress_bar(80)).to match(/warning.*80%/)
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%/)
end
it 'shows a red bar if percent is 100' do
expect(helper.ci_minutes_progress_bar(100)).to match(/danger.*100%/)
end
it 'shows a red bar if percent is higher than 100 and caps the value to 100' do
expect(helper.ci_minutes_progress_bar(120)).to match(/danger.*100%/)
end
end
describe '#namespace_shared_runner_limits_quota' do describe '#namespace_shared_runner_limits_quota' do
context "when it's unlimited" do context "when it's unlimited" do
before do before do
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
require 'spec_helper' require 'spec_helper'
describe Ci::Minutes::Quota do describe Ci::Minutes::Quota do
using RSpec::Parameterized::TableSyntax
let_it_be(:namespace) do let_it_be(:namespace) do
create(:namespace, namespace_statistics: create(:namespace_statistics)) create(:namespace, namespace_statistics: create(:namespace_statistics))
end end
...@@ -165,4 +167,34 @@ describe Ci::Minutes::Quota do ...@@ -165,4 +167,34 @@ describe Ci::Minutes::Quota do
end end
end end
end end
describe '#monthly_percent_used' do
subject { quota.monthly_percent_used }
where(:limit_enabled, :monthly_limit, :purchased_limit, :minutes_used, :result, :title) do
false | 200 | 0 | 40 | 0 | 'limit not enabled'
true | 200 | 0 | 0 | 0 | 'monthly limit set and no usage'
true | 200 | 0 | 40 | 20 | 'monthly limit set and usage lower than 100%'
true | 200 | 0 | 200 | 100 | 'monthly limit set and usage at 100%'
true | 200 | 0 | 210 | 105 | 'monthly limit set and usage above 100%'
true | 0 | 0 | 0 | 0 | 'monthly limit not set and no usage'
true | 0 | 0 | 40 | 0 | 'monthly limit not set and some usage'
true | 200 | 100 | 0 | 0 | 'monthly and purchased limits set and no usage'
true | 200 | 100 | 40 | 20 | 'monthly and purchased limits set and low usage'
true | 200 | 100 | 210 | 100 | 'usage capped to 100% and overflows into purchased minutes'
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