Skip to content

Commit

Permalink
Merge pull request #16368 from CartoDB/feature/sc-178449/notify-about…
Browse files Browse the repository at this point in the history
…-the-limit-of-4096-named-maps
  • Loading branch information
moicalcob committed Feb 9, 2022
2 parents 46b9122 + 1fb5fda commit 0456ec0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -80,6 +80,8 @@ Development
- Fix Auth URL generation while establishing a connection with Google Drive [#16357](https://github.com/CartoDB/cartodb/pull/16357)
- Fix adding license metadata to a dataset [#16356](https://github.com/CartoDB/cartodb/pull/16356)
- Fix notifications when organization seats limit is reached [#16359](https://github.com/CartoDB/cartodb/pull/16359)
- Notify Support when a user is reaching the named maps limit [#16368](https://github.com/CartoDB/cartodb/pull/16368)
- Remove old named maps when a user is reaching the named maps limit [#16368](https://github.com/CartoDB/cartodb/pull/16368)
- Fix privacy dropdown when user is editing a map [#16367](https://github.com/CartoDB/cartodb/pull/16367)
- Add a new rake to update a user username [#16370](https://github.com/CartoDB/cartodb/pull/16370)
- Add a check before destroying user tables in order to avoid deleting dependent maps [#16381](https://github.com/CartoDB/cartodb/pull/16381)
Expand Down
7 changes: 7 additions & 0 deletions app/mailers/reporter_mailer.rb
Expand Up @@ -8,4 +8,11 @@ def trending_maps_report(mail_to, trending_visualizations)

mail to: mail_to, subject: @subject
end

def named_maps_near_the_limit(message)
mail_to = Cartodb.get_config(:mailer, 'support_email')
mail_from = Cartodb.get_config(:mailer, 'internal_notifications_email')

mail from: mail_from, to: mail_to, subject: message if mail_to && mail_from
end
end
8 changes: 8 additions & 0 deletions app/views/reporter_mailer/named_maps_near_the_limit.html.erb
@@ -0,0 +1,8 @@
<tr>
<td>
<p style="font-size: 16px; line-height: 25px; font-family: 'Open Sans', Helvetica, Arial, sans-serif; color: #647083;">
There is one username that is reaching the named maps limit. Response Team should check if it's possible to
remove a few named maps!
</p>
</td>
</tr>
18 changes: 16 additions & 2 deletions lib/carto/named_maps/api.rb
Expand Up @@ -12,6 +12,7 @@ class Api
HTTP_REQUEST_TIMEOUT_SECONDS = 60
RETRY_TIME_SECONDS = 2
MAX_RETRY_ATTEMPTS = 3
MAX_NAMED_MAPS_TO_CLEAN = 100

attr_accessor :user, :visualization

Expand Down Expand Up @@ -44,7 +45,21 @@ def create

response_code = response.code
if response_code.to_s.match?(/^2/)
::JSON.parse(response.response_body).deep_symbolize_keys
response_body = ::JSON.parse(response.response_body).deep_symbolize_keys
if response_body.key?(:limit_message)
# Send message to support and clean some named_maps
ReporterMailer.named_maps_near_the_limit(response_body[:limit_message]).deliver_now
tables = Carto::UserTable.where(user_id: user.id, privacy: 0)
.limit(MAX_NAMED_MAPS_TO_CLEAN)
.order('updated_at')
named_maps_ids = tables.map { |t| "tpl_#{t.visualization.id.tr('-', '_')}" }
urls = named_maps_ids.map { |id| url(template_name: id) }
::Resque.enqueue(
::Resque::UserJobs::NamedMapsLimitsJobs::CleanNamedMaps,
{ urls: urls, request_params: request_params }
)
end
response_body
elsif response_code != 409 # Ignore when max number of templates is reached
log_response(response, 'create')
end
Expand Down Expand Up @@ -90,7 +105,6 @@ def update(retries: 0)
def destroy(retries: 0)
stats_aggregator.timing('carto-named-maps-api.destroy') do
url = url(template_name: @named_map_template.name)

response = http_client.delete(url, request_params)

response_code_string = response.code.to_s
Expand Down
30 changes: 30 additions & 0 deletions lib/resque/user_jobs.rb
Expand Up @@ -58,6 +58,36 @@ def self.perform(user_creation_id, common_data_url = nil, organization_owner_pro
end
end

module NamedMapsLimitsJobs
class CleanNamedMaps < BaseJob

RETRY_TIME_SECONDS = 2
MAX_RETRY_ATTEMPTS = 3

@queue = :users

def self.perform(options = {})
run_action(options, @queue, lambda { |job_options|
job_options.symbolize_keys[:urls].each { |url| delete(url, job_options.symbolize_keys[:request_params]) }
})
end

def self.delete(url, request_params, retries: 0)
http_client = Carto::Http::Client.get('named_maps')
response = http_client.delete(url, request_params.symbolize_keys)

response_code_string = response.code.to_s
if response_code_string.start_with?('2')
response.response_body
elsif (response_code_string.start_with?('5') || response.code == 429) && retries < MAX_RETRY_ATTEMPTS
sleep(RETRY_TIME_SECONDS**retries)
delete(url, request_params, retries: retries + 1)
end
end

end
end

module RateLimitsJobs
module SyncRedis
extend ::Resque::Metrics
Expand Down

0 comments on commit 0456ec0

Please sign in to comment.