Commit cb824d06 authored by Jason Goodman's avatar Jason Goodman Committed by Markus Koller

Add remaining storage size to namespace storage email notification

Include remaining storage size along with percentage in email body

Changelog: changed
parent 24a5b0a0
......@@ -20,11 +20,12 @@ module Emails
)
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
@usage_quotas_url = usage_quotas_url(namespace, anchor: 'storage-quota-tab')
@buy_storage_url = buy_storage_url(namespace)
@percentage_of_available_storage = percentage_of_available_storage
@size_of_available_storage = size_of_available_storage
mail(
bcc: recipients,
......
......@@ -7,7 +7,7 @@ module Emails
end
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
......@@ -11,12 +11,11 @@ module Namespaces
return unless namespace.root_storage_statistics
root_storage_size = ::Namespace::RootStorageSize.new(namespace)
usage_ratio = root_storage_size.usage_ratio
level = notification_level(usage_ratio)
level = notification_level(root_storage_size)
last_level = namespace.root_storage_statistics.notification_level.to_sym
if level != last_level
send_notification(level, namespace, usage_ratio)
send_notification(level, namespace, root_storage_size)
update_notification_level(level, namespace)
end
end
......@@ -25,8 +24,8 @@ module Namespaces
attr_reader :mailer
def notification_level(usage_ratio)
case usage_ratio
def notification_level(root_storage_size)
case root_storage_size.usage_ratio
when 0...0.7 then :storage_remaining
when 0.7...0.85 then :caution
when 0.85...0.95 then :warning
......@@ -35,7 +34,7 @@ module Namespaces
end
end
def send_notification(level, namespace, usage_ratio)
def send_notification(level, namespace, root_storage_size)
return if level == :storage_remaining
owner_emails = namespace.owners.map(&:email)
......@@ -43,8 +42,9 @@ module Namespaces
if level == :exceeded
mailer.notify_out_of_storage(namespace, owner_emails)
else
percentage = storage_remaining_percentage(usage_ratio)
mailer.notify_limit_warning(namespace, owner_emails, percentage)
percentage = remaining_storage_percentage(root_storage_size)
size = remaining_storage_size(root_storage_size)
mailer.notify_limit_warning(namespace, owner_emails, percentage, size)
end
end
......@@ -52,8 +52,12 @@ module Namespaces
namespace.root_storage_statistics.update!(notification_level: level)
end
def storage_remaining_percentage(usage_ratio)
(100 - usage_ratio * 100).floor
def remaining_storage_percentage(root_storage_size)
(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
......
%p
- 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
= s_('NamespaceStorage|We recommend that you buy additional storage to ensure your service is not interrupted.')
%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.') %>
......
......@@ -34,23 +34,23 @@ RSpec.describe Emails::NamespaceStorageUsageMailer do
describe '#notify_limit_warning' 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 bcc_to recipients
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)}"
end
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)
.to have_subject "Action required: Approximately 30% of namespace storage remains for #{namespace.name}"
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 "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
end
end
......
......@@ -40,17 +40,20 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do
end
end
where(:limit, :used, :last_notification_level, :expected_percent_remaining, :expected_level) do
100 | 70 | :storage_remaining | 30 | :caution
100 | 85 | :storage_remaining | 15 | :warning
100 | 95 | :storage_remaining | 5 | :danger
100 | 77 | :storage_remaining | 23 | :caution
1000 | 971 | :storage_remaining | 2 | :danger
100 | 85 | :caution | 15 | :warning
100 | 95 | :warning | 5 | :danger
100 | 99 | :exceeded | 1 | :danger
100 | 94 | :danger | 6 | :warning
100 | 84 | :warning | 16 | :caution
where(:limit, :used, :last_notification_level, :expected_percent, :expected_size, :expected_level) do
100 | 70 | :storage_remaining | 30 | 30.megabytes | :caution
100 | 85 | :storage_remaining | 15 | 15.megabytes | :warning
100 | 95 | :storage_remaining | 5 | 5.megabytes | :danger
100 | 77 | :storage_remaining | 23 | 23.megabytes | :caution
1000 | 971 | :storage_remaining | 2 | 29.megabytes | :danger
100 | 85 | :caution | 15 | 15.megabytes | :warning
100 | 95 | :warning | 5 | 5.megabytes | :danger
100 | 99 | :exceeded | 1 | 1.megabytes | :danger
100 | 94 | :danger | 6 | 6.megabytes | :warning
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
with_them do
......@@ -59,7 +62,7 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do
set_used_storage(group, megabytes: used)
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)
......@@ -169,7 +172,7 @@ RSpec.describe Namespaces::Storage::EmailNotificationService do
set_used_storage(namespace, megabytes: 851)
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)
end
......
......@@ -25,7 +25,7 @@ RSpec.describe Namespaces::RootStatisticsWorker, '#perform', :saas do
set_storage_size_limit(group, megabytes: 10)
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)
end
......
......@@ -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."
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 ""
msgid "NamespaceStorage|%{name_with_link} namespace has exceeded its namespace storage limit."
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 ""
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