Commit 3be7421e authored by Robert May's avatar Robert May

Cache storage alert banner

Caches the storage alert on a short TTL.
parent 760d6c5f
......@@ -10,11 +10,13 @@ module EE
end
def display_namespace_storage_limit_alert?(namespace)
@display_namespace_storage_limit_alert && !usage_quota_page?(namespace)
@display_namespace_storage_limit_alert &&
!usage_quota_page?(namespace) &&
can?(current_user, :admin_namespace, namespace.root_ancestor)
end
def namespace_storage_alert(namespace)
return {} if current_user.nil?
return {} unless can?(current_user, :admin_namespace, namespace.root_ancestor)
payload = check_storage_size_service(namespace).execute.payload
......@@ -54,6 +56,10 @@ module EE
EE::SUBSCRIPTIONS_MORE_STORAGE_URL
end
def number_of_hidden_storage_alert_banners
cookies.count { |key, value| key.starts_with?("hide_storage_limit_alert") && value == "true" }
end
private
def usage_quota_page?(namespace)
......
......@@ -3,31 +3,33 @@
- return unless namespace.present?
- return unless display_namespace_storage_limit_alert?(namespace)
- payload = namespace_storage_alert(namespace)
- return if payload.empty?
= cache([namespace, number_of_hidden_storage_alert_banners], expires_in: 1.minute) do
- payload = namespace_storage_alert(namespace)
- unless payload.empty?
- alert_level = payload[:alert_level]
- root_namespace = payload[:root_namespace]
- style = namespace_storage_alert_style(alert_level)
- icon = namespace_storage_alert_icon(alert_level)
- purchase_link = purchase_storage_url if purchase_storage_link_enabled?(namespace)
- usage_link = usage_quotas_path(root_namespace, anchor: 'storage-quota-tab')
- show_storage_banner_actions = purchase_link || usage_link
- alert_level = payload[:alert_level]
- root_namespace = payload[:root_namespace]
.gl-py-5
.gl-alert.js-namespace-storage-alert{ class: "gl-alert-#{style}", role: 'alert' }
= sprite_icon(icon, css_class: "gl-icon gl-alert-icon")
.gl-alert-title
%h4.gl-alert-title= payload[:usage_message]
- style = namespace_storage_alert_style(alert_level)
- icon = namespace_storage_alert_icon(alert_level)
- purchase_link = purchase_storage_url if purchase_storage_link_enabled?(namespace)
- usage_link = usage_quotas_path(root_namespace, anchor: 'storage-quota-tab')
- show_storage_banner_actions = purchase_link || usage_link
- if alert_level != :error
%button.js-namespace-storage-alert-dismiss.gl-alert-dismiss.gl-cursor-pointer{ type: 'button', 'aria-label' => _('Dismiss'), data: { id: root_namespace.id, level: alert_level } }
= sprite_icon('close', css_class: 'gl-icon')
.gl-py-5
.gl-alert.js-namespace-storage-alert{ class: "gl-alert-#{style}", role: 'alert' }
= sprite_icon(icon, css_class: "gl-icon gl-alert-icon")
.gl-alert-title
%h4.gl-alert-title= payload[:usage_message]
- if alert_level != :error
%button.js-namespace-storage-alert-dismiss.gl-alert-dismiss.gl-cursor-pointer{ type: 'button', 'aria-label' => _('Dismiss'), data: { id: root_namespace.id, level: alert_level } }
= sprite_icon('close', css_class: 'gl-icon')
.gl-alert-body
= payload[:explanation_message]
- if show_storage_banner_actions
.gl-alert-actions
- if purchase_link
= link_to(_('Purchase more storage'), purchase_link, class: "btn gl-alert-action btn-md gl-button btn-success")
- if usage_link
= link_to(_('View usage details'), usage_link, class: "btn gl-alert-action btn-md gl-button btn-default")
.gl-alert-body
= payload[:explanation_message]
- if show_storage_banner_actions
.gl-alert-actions
- if purchase_link
= link_to(_('Purchase more storage'), purchase_link, class: "btn gl-alert-action btn-md gl-button btn-success")
- if usage_link
= link_to(_('View usage details'), usage_link, class: "btn gl-alert-action btn-md gl-button btn-default")
......@@ -21,6 +21,9 @@ RSpec.describe EE::NamespaceStorageLimitAlertHelper do
before do
assign(:display_namespace_storage_limit_alert, display_namespace_storage_limit_alert)
allow(helper).to receive(:current_user).and_return(admin)
allow(helper).to receive(:can?).with(anything, :admin_namespace, namespace.root_ancestor).and_return(false)
allow(helper).to receive(:can?).with(admin, :admin_namespace, namespace.root_ancestor).and_return(true)
end
context 'when display_namespace_storage_limit_alert is true' do
......@@ -43,6 +46,12 @@ RSpec.describe EE::NamespaceStorageLimitAlertHelper do
expect(helper.display_namespace_storage_limit_alert?(namespace)).to eq(true)
end
it 'returns false when user is not an admin' do
allow(helper).to receive(:can?).with(admin, :admin_namespace, namespace.root_ancestor).and_return(false)
expect(helper.display_namespace_storage_limit_alert?(namespace)).to eq(false)
end
end
context 'when display_namespace_storage_limit_alert is false' do
......@@ -90,6 +99,9 @@ RSpec.describe EE::NamespaceStorageLimitAlertHelper do
allow_next_instance_of(service_class_name, namespace, admin) do |service|
expect(service).to receive(:execute).and_return(ServiceResponse.success(payload: payload))
end
allow(helper).to receive(:can?).with(nil, :admin_namespace, namespace.root_ancestor).and_return(false)
allow(helper).to receive(:can?).with(admin, :admin_namespace, namespace.root_ancestor).and_return(true)
end
context 'when payload is not empty and no cookie is set' do
......@@ -104,6 +116,14 @@ RSpec.describe EE::NamespaceStorageLimitAlertHelper do
it { is_expected.to eq({}) }
end
context 'when current_user is not an admin of the namespace' do
before do
allow(helper).to receive(:can?).with(admin, :admin_namespace, namespace.root_ancestor).and_return(false)
end
it { is_expected.to eq({}) }
end
context 'when payload is empty' do
let(:payload) { {} }
......@@ -179,4 +199,31 @@ RSpec.describe EE::NamespaceStorageLimitAlertHelper do
it { is_expected.to eq(result) }
end
end
describe '#number_of_hidden_storage_alert_banners' do
subject { helper.number_of_hidden_storage_alert_banners }
let_it_be(:namespace) { create(:namespace) }
context 'when a cookie is set' do
before do
helper.request.cookies["hide_storage_limit_alert_#{namespace.id}_info"] = 'true'
end
it { is_expected.to eq(1) }
end
context 'when two cookies are set' do
before do
helper.request.cookies["hide_storage_limit_alert_#{namespace.id}_info"] = 'true'
helper.request.cookies["hide_storage_limit_alert_#{namespace.id}_danger"] = 'true'
end
it { is_expected.to eq(2) }
end
context 'when no cookies are set' do
it { is_expected.to eq(0) }
end
end
end
......@@ -18,6 +18,7 @@ RSpec.shared_examples 'namespace storage limit alert' do
end
allow(controller).to receive(:current_user).and_return(user)
namespace.add_owner(user)
end
render_views
......
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