Commit 76df8c7d authored by Markus Koller's avatar Markus Koller

Merge branch 'add-remaining-storage-size-to-email-notification' into 'master'

Include Remaining Storage Size in Namespace Storage Email Notification

See merge request gitlab-org/gitlab!84416
parents a143ede0 cb824d06
...@@ -20,11 +20,12 @@ module Emails ...@@ -20,11 +20,12 @@ module Emails
) )
end end
def notify_limit_warning(namespace, recipients, percentage_of_available_storage) def notify_limit_warning(namespace, recipients, percentage_of_available_storage, size_of_available_storage)
@namespace = namespace @namespace = namespace
@usage_quotas_url = usage_quotas_url(namespace, anchor: 'storage-quota-tab') @usage_quotas_url = usage_quotas_url(namespace, anchor: 'storage-quota-tab')
@buy_storage_url = buy_storage_url(namespace) @buy_storage_url = buy_storage_url(namespace)
@percentage_of_available_storage = percentage_of_available_storage @percentage_of_available_storage = percentage_of_available_storage
@size_of_available_storage = size_of_available_storage
mail( mail(
bcc: recipients, bcc: recipients,
......
...@@ -7,7 +7,7 @@ module Emails ...@@ -7,7 +7,7 @@ module Emails
end end
def limit_warning def limit_warning
::Emails::NamespaceStorageUsageMailer.notify_limit_warning(Group.last, %w(bob@example.com), 25) ::Emails::NamespaceStorageUsageMailer.notify_limit_warning(Group.last, %w(bob@example.com), 25, 1.25.gigabytes)
end end
end end
end end
...@@ -11,12 +11,11 @@ module Namespaces ...@@ -11,12 +11,11 @@ module Namespaces
return unless namespace.root_storage_statistics return unless namespace.root_storage_statistics
root_storage_size = ::Namespace::RootStorageSize.new(namespace) root_storage_size = ::Namespace::RootStorageSize.new(namespace)
usage_ratio = root_storage_size.usage_ratio level = notification_level(root_storage_size)
level = notification_level(usage_ratio)
last_level = namespace.root_storage_statistics.notification_level.to_sym last_level = namespace.root_storage_statistics.notification_level.to_sym
if level != last_level if level != last_level
send_notification(level, namespace, usage_ratio) send_notification(level, namespace, root_storage_size)
update_notification_level(level, namespace) update_notification_level(level, namespace)
end end
end end
...@@ -25,8 +24,8 @@ module Namespaces ...@@ -25,8 +24,8 @@ module Namespaces
attr_reader :mailer attr_reader :mailer
def notification_level(usage_ratio) def notification_level(root_storage_size)
case usage_ratio case root_storage_size.usage_ratio
when 0...0.7 then :storage_remaining when 0...0.7 then :storage_remaining
when 0.7...0.85 then :caution when 0.7...0.85 then :caution
when 0.85...0.95 then :warning when 0.85...0.95 then :warning
...@@ -35,7 +34,7 @@ module Namespaces ...@@ -35,7 +34,7 @@ module Namespaces
end end
end end
def send_notification(level, namespace, usage_ratio) def send_notification(level, namespace, root_storage_size)
return if level == :storage_remaining return if level == :storage_remaining
owner_emails = namespace.owners.map(&:email) owner_emails = namespace.owners.map(&:email)
...@@ -43,8 +42,9 @@ module Namespaces ...@@ -43,8 +42,9 @@ module Namespaces
if level == :exceeded if level == :exceeded
mailer.notify_out_of_storage(namespace, owner_emails) mailer.notify_out_of_storage(namespace, owner_emails)
else else
percentage = storage_remaining_percentage(usage_ratio) percentage = remaining_storage_percentage(root_storage_size)
mailer.notify_limit_warning(namespace, owner_emails, percentage) size = remaining_storage_size(root_storage_size)
mailer.notify_limit_warning(namespace, owner_emails, percentage, size)
end end
end end
...@@ -52,8 +52,12 @@ module Namespaces ...@@ -52,8 +52,12 @@ module Namespaces
namespace.root_storage_statistics.update!(notification_level: level) namespace.root_storage_statistics.update!(notification_level: level)
end end
def storage_remaining_percentage(usage_ratio) def remaining_storage_percentage(root_storage_size)
(100 - usage_ratio * 100).floor (100 - root_storage_size.usage_ratio * 100).floor
end
def remaining_storage_size(root_storage_size)
root_storage_size.limit - root_storage_size.current_size
end end
end end
end end
......
%p %p
- name_link = link_to @namespace.name, @usage_quotas_url - name_link = link_to @namespace.name, @usage_quotas_url
= html_escape(s_('NamespaceStorage|%{name_with_link} namespace has approximately %{percent} namespace storage space remaining.')) % { name_with_link: name_link.html_safe, percent: "#{@percentage_of_available_storage}%" } = html_escape(s_('NamespaceStorage|%{name_with_link} namespace has approximately %{percent} (%{size}) namespace storage space remaining.')) % { name_with_link: name_link.html_safe, percent: "#{@percentage_of_available_storage}%", size: number_to_human_size(@size_of_available_storage) }
%p %p
= s_('NamespaceStorage|We recommend that you buy additional storage to ensure your service is not interrupted.') = s_('NamespaceStorage|We recommend that you buy additional storage to ensure your service is not interrupted.')
%p %p
......
<%= s_('NamespaceStorage|%{name}(%{url}) namespace has approximately %{percent} namespace storage space remaining.') % { name: @namespace.name, url: @usage_quotas_url, percent: "#{@percentage_of_available_storage}%" }%> <%= s_('NamespaceStorage|%{name}(%{url}) namespace has approximately %{percent} (%{size}) namespace storage space remaining.') % { name: @namespace.name, url: @usage_quotas_url, percent: "#{@percentage_of_available_storage}%", size: number_to_human_size(@size_of_available_storage) }%>
<%= s_('NamespaceStorage|We recommend that you buy additional storage to ensure your service is not interrupted.') %> <%= s_('NamespaceStorage|We recommend that you buy additional storage to ensure your service is not interrupted.') %>
......
...@@ -34,23 +34,23 @@ RSpec.describe Emails::NamespaceStorageUsageMailer do ...@@ -34,23 +34,23 @@ RSpec.describe Emails::NamespaceStorageUsageMailer do
describe '#notify_limit_warning' do describe '#notify_limit_warning' do
it 'creates an email message for a group' do it 'creates an email message for a group' do
mail = described_class.notify_limit_warning(group, recipients, 25) mail = described_class.notify_limit_warning(group, recipients, 25, 1.25.gigabytes)
expect(mail).to have_subject "Action required: Approximately 25% of namespace storage remains for #{group.name}" expect(mail).to have_subject "Action required: Approximately 25% of namespace storage remains for #{group.name}"
expect(mail).to bcc_to recipients expect(mail).to bcc_to recipients
expect(mail).to have_body_text "#{usage_quotas_url(group, anchor: 'storage-quota-tab')}" expect(mail).to have_body_text "#{usage_quotas_url(group, anchor: 'storage-quota-tab')}"
expect(mail).to have_body_text "has approximately 25% namespace storage space remaining" expect(mail).to have_body_text "has approximately 25% (1.25 GB) namespace storage space remaining"
expect(mail).to have_body_text "#{buy_storage_subscriptions_url(selected_group: group.id)}" expect(mail).to have_body_text "#{buy_storage_subscriptions_url(selected_group: group.id)}"
end end
it 'creates an email message for a namespace' do it 'creates an email message for a namespace' do
mail = described_class.notify_limit_warning(namespace, recipients, 30) mail = described_class.notify_limit_warning(namespace, recipients, 30, 500.megabytes)
expect(mail) expect(mail)
.to have_subject "Action required: Approximately 30% of namespace storage remains for #{namespace.name}" .to have_subject "Action required: Approximately 30% of namespace storage remains for #{namespace.name}"
expect(mail).to bcc_to recipients expect(mail).to bcc_to recipients
expect(mail).to have_body_text "#{usage_quotas_url(namespace, anchor: 'storage-quota-tab')}" expect(mail).to have_body_text "#{usage_quotas_url(namespace, anchor: 'storage-quota-tab')}"
expect(mail).to have_body_text "has approximately 30% namespace storage space remaining" expect(mail).to have_body_text "has approximately 30% (500 MB) namespace storage space remaining"
expect(mail).to have_body_text EE::SUBSCRIPTIONS_MORE_STORAGE_URL expect(mail).to have_body_text EE::SUBSCRIPTIONS_MORE_STORAGE_URL
end end
end end
......
...@@ -40,17 +40,20 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do ...@@ -40,17 +40,20 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do
end end
end end
where(:limit, :used, :last_notification_level, :expected_percent_remaining, :expected_level) do where(:limit, :used, :last_notification_level, :expected_percent, :expected_size, :expected_level) do
100 | 70 | :storage_remaining | 30 | :caution 100 | 70 | :storage_remaining | 30 | 30.megabytes | :caution
100 | 85 | :storage_remaining | 15 | :warning 100 | 85 | :storage_remaining | 15 | 15.megabytes | :warning
100 | 95 | :storage_remaining | 5 | :danger 100 | 95 | :storage_remaining | 5 | 5.megabytes | :danger
100 | 77 | :storage_remaining | 23 | :caution 100 | 77 | :storage_remaining | 23 | 23.megabytes | :caution
1000 | 971 | :storage_remaining | 2 | :danger 1000 | 971 | :storage_remaining | 2 | 29.megabytes | :danger
100 | 85 | :caution | 15 | :warning 100 | 85 | :caution | 15 | 15.megabytes | :warning
100 | 95 | :warning | 5 | :danger 100 | 95 | :warning | 5 | 5.megabytes | :danger
100 | 99 | :exceeded | 1 | :danger 100 | 99 | :exceeded | 1 | 1.megabytes | :danger
100 | 94 | :danger | 6 | :warning 100 | 94 | :danger | 6 | 6.megabytes | :warning
100 | 84 | :warning | 16 | :caution 100 | 84 | :warning | 16 | 16.megabytes | :caution
8192 | 6144 | :storage_remaining | 25 | 2.gigabytes | :caution
5120 | 3840 | :storage_remaining | 25 | 1.25.gigabytes | :caution
5120 | 5118 | :warning | 0 | 2.megabytes | :danger
end end
with_them do with_them do
...@@ -59,7 +62,7 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do ...@@ -59,7 +62,7 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do
set_used_storage(group, megabytes: used) set_used_storage(group, megabytes: used)
set_notification_level(last_notification_level) set_notification_level(last_notification_level)
expect(mailer).to receive(:notify_limit_warning).with(group, [owner.email], expected_percent_remaining) expect(mailer).to receive(:notify_limit_warning).with(group, [owner.email], expected_percent, expected_size)
service.execute(group) service.execute(group)
...@@ -169,7 +172,7 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do ...@@ -169,7 +172,7 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do
set_used_storage(namespace, megabytes: 851) set_used_storage(namespace, megabytes: 851)
owner = namespace.owner owner = namespace.owner
expect(mailer).to receive(:notify_limit_warning).with(namespace, [owner.email], 14) expect(mailer).to receive(:notify_limit_warning).with(namespace, [owner.email], 14, 149.megabytes)
service.execute(namespace) service.execute(namespace)
end end
......
...@@ -25,7 +25,7 @@ RSpec.describe Namespaces::RootStatisticsWorker, '#perform', :saas do ...@@ -25,7 +25,7 @@ RSpec.describe Namespaces::RootStatisticsWorker, '#perform', :saas do
set_storage_size_limit(group, megabytes: 10) set_storage_size_limit(group, megabytes: 10)
project.statistics.update!(repository_size: 9.megabytes) project.statistics.update!(repository_size: 9.megabytes)
expect(mailer).to receive(:notify_limit_warning).with(group, [owner.email], 10) expect(mailer).to receive(:notify_limit_warning).with(group, [owner.email], 10, 1.megabyte)
worker.perform(group.id) worker.perform(group.id)
end end
......
...@@ -24735,13 +24735,13 @@ msgstr "" ...@@ -24735,13 +24735,13 @@ msgstr ""
msgid "NamespaceStorageSize|push to your repository, create pipelines, create issues or add comments. To reduce storage capacity, delete unused repositories, artifacts, wikis, issues, and pipelines." msgid "NamespaceStorageSize|push to your repository, create pipelines, create issues or add comments. To reduce storage capacity, delete unused repositories, artifacts, wikis, issues, and pipelines."
msgstr "" msgstr ""
msgid "NamespaceStorage|%{name_with_link} namespace has approximately %{percent} namespace storage space remaining." msgid "NamespaceStorage|%{name_with_link} namespace has approximately %{percent} (%{size}) namespace storage space remaining."
msgstr "" msgstr ""
msgid "NamespaceStorage|%{name_with_link} namespace has exceeded its namespace storage limit." msgid "NamespaceStorage|%{name_with_link} namespace has exceeded its namespace storage limit."
msgstr "" msgstr ""
msgid "NamespaceStorage|%{name}(%{url}) namespace has approximately %{percent} namespace storage space remaining." msgid "NamespaceStorage|%{name}(%{url}) namespace has approximately %{percent} (%{size}) namespace storage space remaining."
msgstr "" msgstr ""
msgid "NamespaceStorage|%{name}(%{url}) namespace has exceeded its namespace storage limit." msgid "NamespaceStorage|%{name}(%{url}) namespace has exceeded its namespace storage limit."
......
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